Skip to content

Instantly share code, notes, and snippets.

@wild-endeavor
Created March 22, 2023 00:09
Show Gist options
  • Save wild-endeavor/cf2336ebf059a087334a8d8e7efe35b1 to your computer and use it in GitHub Desktop.
Save wild-endeavor/cf2336ebf059a087334a8d8e7efe35b1 to your computer and use it in GitHub Desktop.
test for db migrations (flyteadmin #542)
// Before running, create databases legacy and noop;
func TestNoopMigrations(t *testing.T) {
gLogger := gormLogger.New(log.New(os.Stdout, "\r\n", log.LstdFlags), gormLogger.Config{
SlowThreshold: 200 * time.Millisecond,
LogLevel: gormLogger.Info,
IgnoreRecordNotFoundError: false,
Colorful: true,
})
gormConfig := &gorm.Config{
Logger: gLogger,
DisableForeignKeyConstraintWhenMigrating: true,
}
var gormDb *gorm.DB
pgConfig := database.PostgresConfig{
Host: "localhost",
Port: 30001,
DbName: "migratecopy4",
User: "postgres",
Password: "postgres",
ExtraOptions: "",
Debug: false,
}
ctx := context.Background()
postgresDsn := database.PostgresDsn(ctx, pgConfig)
dialector := postgres.Open(postgresDsn)
gormDb, err := gorm.Open(dialector, gormConfig)
assert.NoError(t, err)
fmt.Println(gormDb)
m := gormigrate.New(gormDb, gormigrate.DefaultOptions, Migrations)
if err := m.Migrate(); err != nil {
fmt.Errorf("database migration failed: %v", err)
}
fmt.Println(ctx, "Migration ran successfully")
}
func TestMigrationReplicate(t *testing.T) {
gLogger := gormLogger.New(log.New(os.Stdout, "\r\n", log.LstdFlags), gormLogger.Config{
SlowThreshold: 200 * time.Millisecond,
LogLevel: gormLogger.Info,
IgnoreRecordNotFoundError: false,
Colorful: true,
})
gormConfig := &gorm.Config{
Logger: gLogger,
DisableForeignKeyConstraintWhenMigrating: true,
}
var gormDb *gorm.DB
pgConfig := database.PostgresConfig{
Host: "localhost",
Port: 30001,
DbName: "noop", // this should be a completely blank database
User: "postgres",
Password: "postgres",
ExtraOptions: "",
Debug: false,
}
ctx := context.Background()
postgresDsn := database.PostgresDsn(ctx, pgConfig)
dialector := postgres.Open(postgresDsn)
gormDb, err := gorm.Open(dialector, gormConfig)
assert.NoError(t, err)
fmt.Println(gormDb)
m := gormigrate.New(gormDb, gormigrate.DefaultOptions, NoopMigrations)
if err := m.Migrate(); err != nil {
fmt.Errorf("database migration failed: %v", err)
}
fmt.Println(ctx, "Migration ran successfully")
}
func TestLegacyOnlyMigrations(t *testing.T) {
gLogger := gormLogger.New(log.New(os.Stdout, "\r\n", log.LstdFlags), gormLogger.Config{
SlowThreshold: 200 * time.Millisecond,
LogLevel: gormLogger.Info,
IgnoreRecordNotFoundError: false,
Colorful: true,
})
gormConfig := &gorm.Config{
Logger: gLogger,
DisableForeignKeyConstraintWhenMigrating: true,
}
var gormDb *gorm.DB
pgConfig := database.PostgresConfig{
Host: "localhost",
Port: 30001,
DbName: "legacy",
User: "postgres",
Password: "postgres",
ExtraOptions: "",
Debug: false,
}
ctx := context.Background()
postgresDsn := database.PostgresDsn(ctx, pgConfig)
dialector := postgres.Open(postgresDsn)
gormDb, err := gorm.Open(dialector, gormConfig)
assert.NoError(t, err)
fmt.Println(gormDb)
m := gormigrate.New(gormDb, gormigrate.DefaultOptions, LegacyMigrations)
if err := m.Migrate(); err != nil {
fmt.Errorf("database migration failed: %v", err)
}
fmt.Println(ctx, "Migration ran successfully")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment