-
Notifications
You must be signed in to change notification settings - Fork 239
/
011_susbtitutes_for.go
55 lines (51 loc) · 1.54 KB
/
011_susbtitutes_for.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 SubstitutesForMigrationKey = 11
// Register this migration
func init() {
registerMigration(SubstitutesForMigrationKey, substitutesForPropertyMigration)
}
var substitutesForPropertyMigration = &Migration{
Id: SubstitutesForMigrationKey,
Up: func(ctx context.Context, tx *sql.Tx) error {
sql := `
ALTER TABLE operatorbundle
ADD COLUMN substitutesfor 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, bundlepath TEXT, version TEXT, skiprange TEXT, replaces TEXT, skips TEXT)`
backupTargetTable := `INSERT INTO operatorbundle_backup SELECT name, csv, bundle, bundlepath, version, skiprange, replaces, skips 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
},
}