-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathalter.go
101 lines (84 loc) · 2.75 KB
/
alter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright (c) 2022 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.
package main
import (
"fmt"
"github.com/go-vela/types/constants"
"github.com/sirupsen/logrus"
)
const (
AlterReposBuildLimit = `
ALTER TABLE repos
ADD COLUMN IF NOT EXISTS
build_limit INTEGER
DEFAULT 10;
`
AlterReposPreviousName = `
ALTER TABLE repos
ADD COLUMN IF NOT EXISTS
previous_name VARCHAR(100);
`
AlterSecretsCreatedAt = `
ALTER TABLE secrets
ADD COLUMN IF NOT EXISTS
created_at INTEGER;
`
AlterSecretsCreatedBy = `
ALTER TABLE secrets
ADD COLUMN IF NOT EXISTS
created_by VARCHAR(250);
`
AlterSecretsUpdatedAt = `
ALTER TABLE secrets
ADD COLUMN IF NOT EXISTS
updated_at INTEGER;
`
AlterSecretsUpdatedBy = `
ALTER TABLE secrets
ADD COLUMN IF NOT EXISTS
updated_by VARCHAR(250);
`
)
// Alter will run a series of ALTER statements against the database
// to modify the tables that require new or modified columns.
func (d *db) Alter() error {
logrus.Debug("executing alter from provided configuration")
logrus.Infof("altering %s table to add build_limit column", constants.TableRepo)
// alter the repos table to add the build_limit column
err := d.Gorm.Exec(AlterReposBuildLimit).Error
if err != nil {
return fmt.Errorf("unable to alter %s table: %v", constants.TableRepo, err)
}
logrus.Infof("altering %s table to add previous_name column", constants.TableRepo)
// alter the repos table to add the previous_name column
err = d.Gorm.Exec(AlterReposPreviousName).Error
if err != nil {
return fmt.Errorf("unable to alter %s table: %v", constants.TableRepo, err)
}
logrus.Infof("altering %s table to add created_at column", constants.TableSecret)
// alter the secrets table to add the created_at column
err = d.Gorm.Exec(AlterSecretsCreatedAt).Error
if err != nil {
return fmt.Errorf("unable to alter %s table: %v", constants.TableSecret, err)
}
logrus.Infof("altering %s table to add created_by column", constants.TableSecret)
// alter the secrets table to add the created_by column
err = d.Gorm.Exec(AlterSecretsCreatedBy).Error
if err != nil {
return fmt.Errorf("unable to alter %s table: %v", constants.TableSecret, err)
}
logrus.Infof("altering %s table to add updated_at column", constants.TableSecret)
// alter the secrets table to add the updated_at column
err = d.Gorm.Exec(AlterSecretsUpdatedAt).Error
if err != nil {
return fmt.Errorf("unable to alter %s table: %v", constants.TableSecret, err)
}
logrus.Infof("altering %s table to add updated_by column", constants.TableSecret)
// alter the secrets table to add the updated_by column
err = d.Gorm.Exec(AlterSecretsUpdatedBy).Error
if err != nil {
return fmt.Errorf("unable to alter %s table: %v", constants.TableSecret, err)
}
return nil
}