-
Notifications
You must be signed in to change notification settings - Fork 405
/
12_fix_annotations_migrations_app.go
77 lines (62 loc) · 1.88 KB
/
12_fix_annotations_migrations_app.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
package migrations
import (
"context"
"github.com/fnproject/fn/api/datastore/sql/migratex"
"github.com/jmoiron/sqlx"
)
func up12(ctx context.Context, tx *sqlx.Tx) error {
// clear out any old null values
// on mysql this may result in some in-flight changes being missed and an error on the alter table below
_, err := tx.ExecContext(ctx, `UPDATE apps set annotations=(CASE WHEN annotations IS NULL THEN '' ELSE annotations END);`)
if err != nil {
return err
}
switch tx.DriverName() {
case "mysql":
// this implicitly commits but its the last command so should be safe.
_, err := tx.ExecContext(ctx, "ALTER TABLE apps MODIFY annotations TEXT NOT NULL;")
return err
case "postgres", "pgx":
_, err := tx.ExecContext(ctx, "ALTER TABLE apps ALTER COLUMN annotations DROP NOT NULL;")
return err
default: // nuclear option, replace the table using sqlite safe DDL
_, err := tx.ExecContext(ctx, "ALTER TABLE apps RENAME TO old_apps;")
if err != nil {
return err
}
newTable := `CREATE TABLE apps (
id varchar(256),
name varchar(256) NOT NULL PRIMARY KEY,
config text NOT NULL,
annotations text NOT NULL,
created_at varchar(256),
updated_at varchar(256)
);`
_, err = tx.ExecContext(ctx, newTable)
if err != nil {
return err
}
insertQuery := `INSERT INTO apps(id,name,config,annotations,created_at,updated_at)
SELECT id,name,config,annotations,created_at,updated_at FROM old_apps;`
_, err = tx.ExecContext(ctx, insertQuery)
if err != nil {
return err
}
_, err = tx.ExecContext(ctx, "DROP TABLE old_apps;")
if err != nil {
return err
}
return err
}
}
func down12(ctx context.Context, tx *sqlx.Tx) error {
// annotations are in an indeterminate state so we leave this change as it is
return nil
}
func init() {
Migrations = append(Migrations, &migratex.MigFields{
VersionFunc: vfunc(12),
UpFunc: up12,
DownFunc: down12,
})
}