MCPcopy
hub / github.com/github/gh-ost / TestMigratorValidateStatement

Function TestMigratorValidateStatement

go/logic/migrator_test.go:272–324  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

270}
271
272func TestMigratorValidateStatement(t *testing.T) {
273 t.Run("add-column", func(t *testing.T) {
274 migrationContext := base.NewMigrationContext()
275 migrator := NewMigrator(migrationContext, "1.2.3")
276 require.Nil(t, migrator.parser.ParseAlterStatement(`ALTER TABLE test ADD test_new VARCHAR(64) NOT NULL`))
277
278 require.Nil(t, migrator.validateAlterStatement())
279 require.Len(t, migrator.migrationContext.DroppedColumnsMap, 0)
280 })
281
282 t.Run("drop-column", func(t *testing.T) {
283 migrationContext := base.NewMigrationContext()
284 migrator := NewMigrator(migrationContext, "1.2.3")
285 require.Nil(t, migrator.parser.ParseAlterStatement(`ALTER TABLE test DROP abc`))
286
287 require.Nil(t, migrator.validateAlterStatement())
288 require.Len(t, migrator.migrationContext.DroppedColumnsMap, 1)
289 _, exists := migrator.migrationContext.DroppedColumnsMap["abc"]
290 require.True(t, exists)
291 })
292
293 t.Run("rename-column", func(t *testing.T) {
294 migrationContext := base.NewMigrationContext()
295 migrator := NewMigrator(migrationContext, "1.2.3")
296 require.Nil(t, migrator.parser.ParseAlterStatement(`ALTER TABLE test CHANGE test123 test1234 bigint unsigned`))
297
298 err := migrator.validateAlterStatement()
299 require.Error(t, err)
300 require.True(t, strings.HasPrefix(err.Error(), "gh-ost believes the ALTER statement renames columns"))
301 require.Len(t, migrator.migrationContext.DroppedColumnsMap, 0)
302 })
303
304 t.Run("rename-column-approved", func(t *testing.T) {
305 migrationContext := base.NewMigrationContext()
306 migrator := NewMigrator(migrationContext, "1.2.3")
307 migrator.migrationContext.ApproveRenamedColumns = true
308 require.Nil(t, migrator.parser.ParseAlterStatement(`ALTER TABLE test CHANGE test123 test1234 bigint unsigned`))
309
310 require.Nil(t, migrator.validateAlterStatement())
311 require.Len(t, migrator.migrationContext.DroppedColumnsMap, 0)
312 })
313
314 t.Run("rename-table", func(t *testing.T) {
315 migrationContext := base.NewMigrationContext()
316 migrator := NewMigrator(migrationContext, "1.2.3")
317 require.Nil(t, migrator.parser.ParseAlterStatement(`ALTER TABLE test RENAME TO test_new`))
318
319 err := migrator.validateAlterStatement()
320 require.Error(t, err)
321 require.True(t, errors.Is(err, ErrMigratorUnsupportedRenameAlter))
322 require.Len(t, migrator.migrationContext.DroppedColumnsMap, 0)
323 })
324}
325
326func TestMigratorCreateFlagFiles(t *testing.T) {
327 tmpdir, err := os.MkdirTemp("", t.Name())

Callers

nothing calls this directly

Calls 6

NewMigrationContextFunction · 0.92
NewMigratorFunction · 0.85
ParseAlterStatementMethod · 0.80
ErrorMethod · 0.65
LenMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…