-
Notifications
You must be signed in to change notification settings - Fork 1
/
migrate_pg.go
155 lines (131 loc) · 3.34 KB
/
migrate_pg.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package gontentful
import (
"fmt"
"github.com/jmoiron/sqlx"
)
const (
newSchemaNameTpl = "%s_new"
oldSchemaNameTpl = "%s_old"
migrateSchemaTpl = `DROP SCHEMA IF EXISTS %[2]s CASCADE;
CREATE SCHEMA IF NOT EXISTS %[1]s;
ALTER SCHEMA %[1]s RENAME TO %[2]s;
ALTER SCHEMA %[3]s RENAME TO %[1]s;`
copyTableTpl = `INSERT INTO %[1]s.%[3]s SELECT * FROM %[2]s.%[3]s;`
)
func MigratePGSQL(databaseURL string, newSchemaName string, locales []*Locale, types []*ContentType, cmaTypes []*ContentType, entries []*Entry, syncToken string, createFunctions bool, incrementalMigration bool) error {
var err error
if !incrementalMigration {
// 0) drop newSchema if exists
drop := NewPGDrop(newSchemaName)
err = drop.Exec(databaseURL)
if err != nil {
return err
}
}
// 1) re-create schema
schema := NewPGSQLSchema(newSchemaName, locales, "", cmaTypes, 0)
schema.DropTables = incrementalMigration
err = schema.Exec(databaseURL)
if err != nil {
return err
}
// 2) sync data & save token
sync := NewPGSyncSchema(newSchemaName, locales, types, entries, true)
err = sync.Exec(databaseURL)
if err != nil {
return err
}
err = SaveSyncToken(databaseURL, newSchemaName, syncToken)
if err != nil {
return err
}
// 3) create references
refs := NewPGReferences(schema)
err = refs.Exec(databaseURL)
if err != nil {
return err
}
// 4) create functions
if createFunctions {
funcs := NewPGFunctions(schema)
err = funcs.Exec(databaseURL)
if err != nil {
return err
}
}
// 5) refresh materialized views
if createFunctions {
matViews := NewPGMatViews(schema)
err = matViews.Exec(databaseURL, newSchemaName)
if err != nil {
return err
}
}
return nil
}
func MigrateGamesPGSQL(databaseURL string, newSchemaName string, contentSchemaName string, locales []*Locale, types []*ContentType, cmaTypes []*ContentType, entries []*Entry, syncToken string) error {
// 0) drop newSchema if exists
drop := NewPGDrop(newSchemaName)
err := drop.Exec(databaseURL)
if err != nil {
return err
}
// 1) re-create schema
schema := NewPGSQLSchema(newSchemaName, locales, "", cmaTypes, 0)
schema.ContentSchema = contentSchemaName
err = schema.Exec(databaseURL)
if err != nil {
return err
}
// 2) sync data & save token
sync := NewPGSyncSchema(newSchemaName, locales, types, entries, true)
err = sync.Exec(databaseURL)
if err != nil {
return err
}
err = SaveSyncToken(databaseURL, newSchemaName, syncToken)
if err != nil {
return err
}
// 3) create references
refs := NewPGReferences(schema)
err = refs.Exec(databaseURL)
if err != nil {
return err
}
// 4) create functions
funcs := NewPGFunctions(schema)
err = funcs.Exec(databaseURL)
if err != nil {
return err
}
// 5) refresh materialized views
matViews := NewPGMatViews(schema)
err = matViews.Exec(databaseURL, newSchemaName)
if err != nil {
return err
}
return nil
}
func SwapSchemas(databaseURL string, schemaName string, oldSchemaName string, newSchemaName string) error {
// rename (swap schemas)
db, err := sqlx.Connect("postgres", databaseURL)
if err != nil {
return err
}
defer db.Close()
txn, err := db.Beginx()
if err != nil {
return err
}
defer txn.Rollback()
_, err = txn.Exec(fmt.Sprintf(migrateSchemaTpl, schemaName, oldSchemaName, newSchemaName))
if err != nil {
return err
}
err = txn.Commit()
if err != nil {
return err
}
return nil
}