-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathalter.go
72 lines (59 loc) · 2.02 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
// 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 AlterBuildsTableAddEventAction = `
ALTER TABLE builds
ADD COLUMN IF NOT EXISTS
event_action VARCHAR(250);
`
const AlterBuildsTableAddPipelineID = `
ALTER TABLE builds
ADD COLUMN IF NOT EXISTS
pipeline_id INTEGER;
`
const AlterHooksTableAddEventAction = `
ALTER TABLE hooks
ADD COLUMN IF NOT EXISTS
event_action VARCHAR(250);
`
const AlterHooksTableAddWebhookID = `
ALTER TABLE hooks
ADD COLUMN IF NOT EXISTS
webhook_id INTEGER;
`
// 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 event_action column", constants.TableBuild)
// alter builds table to add event_action column
err := d.Gorm.Exec(AlterBuildsTableAddEventAction).Error
if err != nil {
return fmt.Errorf("unable to alter %s table: %v", constants.TableBuild, err)
}
logrus.Infof("altering %s table to add pipeline_id column", constants.TableBuild)
// alter builds table to add pipeline_id column
err = d.Gorm.Exec(AlterBuildsTableAddPipelineID).Error
if err != nil {
return fmt.Errorf("unable to alter %s table: %v", constants.TableBuild, err)
}
logrus.Infof("altering %s table to add webhook_id column", constants.TableHook)
// alter hooks table to add webhook_id column
err = d.Gorm.Exec(AlterHooksTableAddWebhookID).Error
if err != nil {
return fmt.Errorf("unable to alter %s table: %v", constants.TableHook, err)
}
logrus.Infof("altering %s table to add event_action column", constants.TableHook)
// alter hooks table to add event_action column
err = d.Gorm.Exec(AlterHooksTableAddEventAction).Error
if err != nil {
return fmt.Errorf("unable to alter %s table: %v", constants.TableHook, err)
}
return nil
}