-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrations_old.go
364 lines (328 loc) · 10.9 KB
/
migrations_old.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package main
import (
"fmt"
"github.com/iver-wharf/wharf-api/v5/pkg/model/database"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)
// This entire file contains our migration logic written before the introduction
// of Gormigrate to wharf-api, which was added in v5.1.0.
//
// We need to keep this for backward compatibility, but can discard it after
// reaching wharf-api v6.0.0.
func migrateBeforeGormigrate(db *gorm.DB) error {
// since v5.1.0, we updated the Sqlite driver. This included a bug where a
// database column cannot have the same name as the database table.
// Therefore, we had to rename Token.Token -> Token.Value.
if db.Migrator().HasColumn(&database.Token{}, "Token") {
if err := db.Migrator().RenameColumn(&database.Token{}, "Token", "Value"); err != nil {
return err
}
}
if err := migrateConstraints(db); err != nil {
return err
}
// since v5.0.0, all columns that were not nil'able in the GORM models
// has been migrated to not be nullable in the database either.
if err := migrateWharfColumnsToNotNull(db); err != nil {
return err
}
oldColumns := []columnToDrop{
// since v3.1.0, the token.provider_id column was removed as it induced a
// circular dependency between the token and provider tables
{&database.Token{}, "provider_id"},
// Since v5.0.0, the Provider.upload_url column was removed as it was
// unused.
{&database.Provider{}, "upload_url"},
}
if err := dropOldColumns(db, oldColumns); err != nil {
return err
}
// In v4.2.0 the index param_idx_build_id for artifact was
// changed to artifact_idx_build_id to match the name of the
// table.
oldIndices := []indexToDrop{
{"artifact", "param_idx_build_id"},
}
return dropOldIndices(db, oldIndices)
}
func migrateConstraints(db *gorm.DB) error {
// since v4.2.1, drop these constraints to refresh the constraint behavior.
// Previously it was RESTRICT, now it's CASCADE.
if err := dropOldConstraints(db, []constraintToDrop{
{"artifact", "fk_artifact_build"},
{"log", "fk_log_build"},
{"build", "fk_build_project"},
{"log", "fk_log_build"},
{"branch", "fk_project_branches"},
{"build_param", "fk_build_params"},
}); err != nil {
return err
}
// since v3.1.0, new constraints with other names are added by GORM
// new constraints are named something like "fk_artifact_build" instead
oldConstraints := []constraintToDrop{
{"artifact", "artifact_build_id_build_build_id_foreign"},
{"branch", "branch_project_id_project_project_id_foreign"},
{"branch", "branch_token_id_token_token_id_foreign"},
{"build", "build_project_id_project_project_id_foreign"},
{"build_param", "build_param_build_id_build_build_id_foreign"},
{"log", "log_build_id_build_build_id_foreign"},
{"project", "project_provider_id_provider_provider_id_foreign"},
{"project", "project_token_id_token_token_id_foreign"},
{"provider", "provider_token_id_token_token_id_foreign"},
{"token", "token_provider_id_provider_provider_id_foreign"},
}
return dropOldConstraints(db, oldConstraints)
}
type constraintToDrop struct {
table string
name string
}
func dropOldConstraints(db *gorm.DB, constraints []constraintToDrop) error {
log.Debug().WithInt("constraints", len(constraints)).Message("Dropping old constraints.")
for _, constraint := range constraints {
if err := dropOldConstraint(db, constraint.table, constraint.name); err != nil {
return err
}
}
return nil
}
func dropOldConstraint(db *gorm.DB, table string, constraintName string) error {
if db.Migrator().HasConstraint(table, constraintName) {
log.Info().
WithString("table", table).
WithString("constraint", constraintName).
Message("Dropping old constraint.")
if err := db.Migrator().DropConstraint(table, constraintName); err != nil {
return fmt.Errorf("drop old constraint: %w", err)
}
} else {
log.Debug().
WithString("table", table).
WithString("constraint", constraintName).
Message("No old constraint to remove.")
}
return nil
}
type columnToDrop struct {
model any
columnName string
}
func dropOldColumns(db *gorm.DB, columns []columnToDrop) error {
log.Debug().WithInt("columns", len(columns)).Message("Dropping old columns.")
for _, dbColumn := range columns {
if err := dropOldColumn(db, dbColumn.model, dbColumn.columnName); err != nil {
return err
}
}
return nil
}
func dropOldColumn(db *gorm.DB, model any, columnName string) error {
if db.Migrator().HasColumn(model, columnName) {
log.Info().
WithString("column", columnName).
WithString("model", fmt.Sprintf("%T", model)).
Message("Dropping old column.")
if err := db.Migrator().DropColumn(model, columnName); err != nil {
return fmt.Errorf("drop old column: %w", err)
}
} else {
log.Debug().
WithString("column", columnName).
WithString("model", fmt.Sprintf("%T", model)).
Message("No old column to remove.")
}
return nil
}
type indexToDrop struct {
table string
name string
}
func dropOldIndices(db *gorm.DB, indices []indexToDrop) error {
log.Debug().WithInt("indices", len(indices)).Message("Dropping old indices.")
for _, dbIndex := range indices {
if err := dropOldIndex(db, dbIndex.table, dbIndex.name); err != nil {
return err
}
}
return nil
}
func dropOldIndex(db *gorm.DB, table string, indexName string) error {
if db.Migrator().HasIndex(table, indexName) {
log.Info().
WithString("table", table).
WithString("index", indexName).
Message("Dropping old index.")
if err := db.Migrator().DropIndex(table, indexName); err != nil {
return fmt.Errorf("drop old index: %w", err)
}
} else {
log.Debug().
WithString("table", table).
WithString("index", indexName).
Message("No old index to remove.")
}
return nil
}
func migrateWharfColumnsToNotNull(db *gorm.DB) error {
if err := migrateColumnsToNotNull(db, &database.Token{},
database.TokenFields.UserName,
); err != nil {
return fmt.Errorf("migrating columns to not null for token: %w", err)
}
if err := migrateColumnsToNotNull(db, &database.Project{},
database.ProjectFields.GroupName,
database.ProjectFields.Description,
database.ProjectFields.AvatarURL,
database.ProjectFields.GitURL,
database.ProjectFields.BuildDefinition,
); err != nil {
return fmt.Errorf("migrating columns to not null for project: %w", err)
}
if err := migrateColumnsToNotNull(db, &database.BuildParam{},
database.BuildParamFields.Value,
); err != nil {
return fmt.Errorf("migrating columns to not null for build_param: %w", err)
}
if err := migrateColumnsToNotNull(db, &database.Param{},
database.ParamFields.Value,
database.ParamFields.DefaultValue,
); err != nil {
return fmt.Errorf("migrating columns to not null for param: %w", err)
}
if err := migrateColumnsToNotNull(db, &database.Artifact{},
database.ArtifactFields.FileName,
); err != nil {
return fmt.Errorf("migrating columns to not null for artifact: %w", err)
}
if err := migrateColumnsToNotNull(db, &database.TestResultSummary{},
database.TestResultSummaryFields.FileName,
); err != nil {
return fmt.Errorf("migrating columns to not null for test_result_summary: %w", err)
}
return nil
}
func migrateColumnsToNotNull(db *gorm.DB, model any, fieldNames ...string) error {
if !db.Migrator().HasTable(model) {
log.Debug().WithStringf("model", "%T", model).Message("Skipping changing column to not null as the table does not exist.")
return nil
}
actualTypes, err := db.Migrator().ColumnTypes(model)
if err != nil {
return err
}
actualTypesPerDBName := map[string]gorm.ColumnType{}
for _, columnType := range actualTypes {
actualTypesPerDBName[columnType.Name()] = columnType
}
stmt := db.Model(model).Statement
if err := stmt.Parse(model); err != nil {
return err
}
return db.Transaction(func(tx *gorm.DB) error {
for _, fieldName := range fieldNames {
wantedField := stmt.Schema.LookUpField(fieldName)
if wantedField == nil {
return fmt.Errorf("unknown field when changing to not null: %q", fieldName)
}
actualType, ok := actualTypesPerDBName[wantedField.DBName]
if !ok {
log.Warn().
WithStringf("model", "%T", model).
WithString("field", fieldName).
Message("Cannot change column to not null as the column does not exist in the table.")
continue
}
if err := migrateColumnToNotNull(tx, model, wantedField, actualType); err != nil {
return err
}
}
return nil
})
}
func migrateColumnToNotNull(db *gorm.DB, model any, want *schema.Field, actual gorm.ColumnType) error {
if want.PrimaryKey {
return fmt.Errorf("cannot operate changing the column to not null for a primary key: %q", want.Name)
}
if !want.NotNull {
return fmt.Errorf("struct field is not declared to be not null: %q", want.Name)
}
if nullable, ok := actual.Nullable(); !ok || !nullable {
// Already not null
return nil
}
defaultValue, err := sqlDefaultValue(want.DataType)
if err != nil {
return fmt.Errorf("get default value: %w", err)
}
defaultValueSQLString, err := sqlDefaultValueSQLString(want.DataType)
if err != nil {
return fmt.Errorf("get default value as SQL string: %w", err)
}
if err := db.Transaction(func(tx *gorm.DB) error {
if err := tx.Model(model).Where(want.DBName, nil).Update(want.DBName, defaultValue).Error; err != nil {
return fmt.Errorf("set all to default value where %q is null: %w", want.DBName, err)
}
switch DBDriver(tx.Dialector.Name()) {
case DBDriverPostgres:
tableName := want.Schema.Table
if err := tx.
Exec(
fmt.Sprintf(`ALTER TABLE "%s" ALTER COLUMN "%s" SET DEFAULT %s`,
tableName, want.DBName, defaultValueSQLString),
).Error; err != nil {
return fmt.Errorf("set default to %q: %w", defaultValueSQLString, err)
}
if err := tx.
Exec(
fmt.Sprintf(`ALTER TABLE "%s" ALTER COLUMN "%s" SET NOT NULL`,
tableName, want.DBName),
).Error; err != nil {
return fmt.Errorf("set not null: %w", err)
}
return nil
case DBDriverSqlite:
// Sqlite migrator recreates the table with the field updated
return db.Migrator().AlterColumn(model, want.Name)
default:
return fmt.Errorf("migrating column to not null has not been implemented for this DB driver: %q", tx.Dialector.Name())
}
}); err != nil {
return err
}
log.Info().
WithStringf("model", "%T", model).
WithString("field", want.Name).
WithString("column", want.DBName).
Message("Changed column to not null.")
return nil
}
func sqlDefaultValue(dataType schema.DataType) (any, error) {
switch dataType {
case schema.Bool:
return false, nil
case schema.Int:
return int(0), nil
case schema.Uint:
return uint(0), nil
case schema.Float:
return float32(0), nil
case schema.String:
return "", nil
default:
return nil, fmt.Errorf("unsupported data type: %q", dataType)
}
}
func sqlDefaultValueSQLString(dataType schema.DataType) (string, error) {
switch dataType {
case schema.Bool:
return "false", nil
case schema.Int, schema.Uint, schema.Float:
return "0", nil
case schema.String:
return "''", nil
default:
return "", fmt.Errorf("unsupported data type: %q", dataType)
}
}