-
Notifications
You must be signed in to change notification settings - Fork 239
/
002_bundle_path.go
55 lines (51 loc) · 1.37 KB
/
002_bundle_path.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
package migrations
import (
"context"
"database/sql"
)
const BundlePathMigrationKey = 2
// Register this migration
func init() {
registerMigration(BundlePathMigrationKey, bundlePathMigration)
}
var bundlePathMigration = &Migration{
Id: BundlePathMigrationKey,
Up: func(ctx context.Context, tx *sql.Tx) error {
sql := `
ALTER TABLE operatorbundle
ADD COLUMN bundlepath TEXT;
`
_, err := tx.ExecContext(ctx, sql)
return err
},
Down: func(ctx context.Context, tx *sql.Tx) error {
foreignKeyOff := `PRAGMA foreign_keys = 0`
createTempTable := `CREATE TABLE operatorbundle_backup (name TEXT,csv TEXT,bundle TEXT)`
backupTargetTable := `INSERT INTO operatorbundle_backup SELECT name,csv,bundle FROM operatorbundle`
dropTargetTable := `DROP TABLE operatorbundle`
renameBackUpTable := `ALTER TABLE operatorbundle_backup RENAME TO operatorbundle;`
foreignKeyOn := `PRAGMA foreign_keys = 1`
_, err := tx.ExecContext(ctx, foreignKeyOff)
if err != nil {
return err
}
_, err = tx.ExecContext(ctx, createTempTable)
if err != nil {
return err
}
_, err = tx.ExecContext(ctx, backupTargetTable)
if err != nil {
return err
}
_, err = tx.ExecContext(ctx, dropTargetTable)
if err != nil {
return err
}
_, err = tx.ExecContext(ctx, renameBackUpTable)
if err != nil {
return err
}
_, err = tx.ExecContext(ctx, foreignKeyOn)
return err
},
}