Skip to content

Commit

Permalink
Create migration set factory
Browse files Browse the repository at this point in the history
  • Loading branch information
qbart committed Oct 17, 2021
1 parent 539bd80 commit 4c68291
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
24 changes: 6 additions & 18 deletions krab/action_migrate_up_hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,12 @@ func TestActionMigrateUpHooks(t *testing.T) {
db.MustExec("CREATE SCHEMA tenants")
defer db.MustExec("DROP SCHEMA tenants CASCADE")

set := &MigrationSet{
Hooks: &Hooks{
Before: "SET search_path TO tenants",
},
RefName: "tenants",
Migrations: []*Migration{
{
Version: "v1",
Up: MigrationUp{
SQL: `CREATE TABLE animals(name VARCHAR)`,
},
Down: MigrationDown{
SQL: `DROP TABLE animals`,
},
},
},
}
set.InitDefaults()
set := createMigrationSet("tenants",
"v1",
`CREATE TABLE animals(name VARCHAR)`,
`DROP TABLE animals`,
)
set.Hooks = &Hooks{Before: "SET search_path TO tenants"}

err := (&ActionMigrateUp{Set: set}).Do(ctx, db, cli.NullUI())
assert.NoError(err, "First migration should pass")
Expand Down
22 changes: 22 additions & 0 deletions krab/factories_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package krab

func createMigrationSet(
refName string,
migrationData ...string,
) *MigrationSet {
migrations := make([]*Migration, len(migrationData)/3)

for i := 0; i < len(migrationData); i += 3 {
migrations[i/3] = &Migration{
Version: migrationData[i],
Up: MigrationUp{
SQL: migrationData[i+1],
},
Down: MigrationDown{
SQL: migrationData[i+2],
},
}
}

return &MigrationSet{RefName: refName, Migrations: migrations, Hooks: &Hooks{}}
}

0 comments on commit 4c68291

Please sign in to comment.