refactor(schema): Add support for migration editions
This refactors the schema package and subpackages to support editions.
An Edition is a collection of sql migrations. A given boundary binary
can have multiple migration editions. The `schema.Manager` is updated to
apply the necessary migrations in the correct order, leveraging an
Edition's priority to determine the order in which the separate Editions
are applied.
The sql migration files are now nested under a directory for the
edition.
This also switches from the generated code to using embed, which
eliminates the need for the `make migrations` target and the generated
code. As such this commit removes the target and generated code.
See:
https://pkg.go.dev/embed
This commit is contained in:
144 files changed
+3469
-11098
No files matched your search
@@ -29,7 +29,7 @@ func migrateDatabase(ctx context.Context, ui cli.Ui, dialect, u string, initiali
|
||||
ui.Error(fmt.Sprintf("Unable to connect to the database at %q", u))
|
||||
return noop, 2
|
||||
}
|
||||
man, err := schema.NewManager(ctx, dialect, dBase)
|
||||
man, err := schema.NewManager(ctx, schema.Dialect(dialect), dBase)
|
||||
if err != nil {
|
||||
if errors.Match(errors.T(errors.MigrationLock), err) {
|
||||
ui.Error("Unable to capture a lock on the database.")
|
||||
@@ -53,26 +53,22 @@ func migrateDatabase(ctx context.Context, ui cli.Ui, dialect, u string, initiali
|
||||
ui.Error(fmt.Errorf("Error getting database state: %w", err).Error())
|
||||
return unlock, 2
|
||||
}
|
||||
if initialized && !st.InitializationStarted {
|
||||
if initialized && !st.Initialized {
|
||||
ui.Output(base.WrapAtLength("Database has not been initialized. Please use 'boundary database init' to initialize the boundary database."))
|
||||
return unlock, -1
|
||||
}
|
||||
if !initialized && st.InitializationStarted {
|
||||
if !initialized && st.Initialized {
|
||||
ui.Output(base.WrapAtLength("Database has already been initialized. Please use 'boundary database migrate' for any upgrade needs."))
|
||||
return unlock, -1
|
||||
}
|
||||
if st.Dirty {
|
||||
ui.Error(base.WrapAtLength("Database is in a bad state. Please revert back to the last known good state."))
|
||||
return unlock, 2
|
||||
}
|
||||
if err := man.RollForward(ctx); err != nil {
|
||||
if err := man.ApplyMigrations(ctx); err != nil {
|
||||
ui.Error(fmt.Errorf("Error running database migrations: %w", err).Error())
|
||||
return unlock, 2
|
||||
}
|
||||
if base.Format(ui) == "table" {
|
||||
ui.Info("Migrations successfully run.")
|
||||
}
|
||||
migrationLogs, err := schema.GetMigrationLog(ctx, dBase)
|
||||
migrationLogs, err := man.GetMigrationLog(ctx)
|
||||
if err != nil {
|
||||
ui.Error(fmt.Errorf("Error retrieving database migration logs: %w", err).Error())
|
||||
return unlock, 2
|
||||
|
||||
@@ -53,13 +53,11 @@ func TestMigrateDatabase(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
earlyMigrationVersion := 2000
|
||||
oState := schema.TestCloneMigrationStates(t)
|
||||
nState := schema.TestCreatePartialMigrationState(oState["postgres"], earlyMigrationVersion)
|
||||
oState["postgres"] = nState
|
||||
|
||||
man, err := schema.NewManager(ctx, dialect, dBase, schema.WithMigrationStates(oState))
|
||||
man, err := schema.NewManager(ctx, schema.Dialect(dialect), dBase, schema.WithEditions(
|
||||
schema.TestCreatePartialEditions(schema.Dialect(dialect), schema.PartialEditions{"oss": earlyMigrationVersion}),
|
||||
))
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, man.RollForward(ctx))
|
||||
require.NoError(t, man.ApplyMigrations(ctx))
|
||||
return u
|
||||
},
|
||||
expectedCode: 0,
|
||||
@@ -79,13 +77,11 @@ func TestMigrateDatabase(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
earlyMigrationVersion := 2000
|
||||
oState := schema.TestCloneMigrationStates(t)
|
||||
nState := schema.TestCreatePartialMigrationState(oState["postgres"], earlyMigrationVersion)
|
||||
oState["postgres"] = nState
|
||||
|
||||
man, err := schema.NewManager(ctx, dialect, dBase, schema.WithMigrationStates(oState))
|
||||
man, err := schema.NewManager(ctx, schema.Dialect(dialect), dBase, schema.WithEditions(
|
||||
schema.TestCreatePartialEditions(schema.Dialect(dialect), schema.PartialEditions{"oss": earlyMigrationVersion}),
|
||||
))
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, man.RollForward(ctx))
|
||||
require.NoError(t, man.ApplyMigrations(ctx))
|
||||
return u
|
||||
},
|
||||
expectedCode: -1,
|
||||
@@ -157,7 +153,7 @@ func TestMigrateDatabase(t *testing.T) {
|
||||
dBase, err := common.SqlOpen(dialect, u)
|
||||
require.NoError(t, err)
|
||||
|
||||
man, err := schema.NewManager(ctx, dialect, dBase)
|
||||
man, err := schema.NewManager(ctx, schema.Dialect(dialect), dBase)
|
||||
require.NoError(t, err)
|
||||
// This is an advisory lock on the DB which is released when the DB session ends.
|
||||
err = man.ExclusiveLock(ctx)
|
||||
@@ -180,7 +176,7 @@ func TestMigrateDatabase(t *testing.T) {
|
||||
dBase, err := common.SqlOpen(dialect, u)
|
||||
require.NoError(t, err)
|
||||
|
||||
man, err := schema.NewManager(ctx, dialect, dBase)
|
||||
man, err := schema.NewManager(ctx, schema.Dialect(dialect), dBase)
|
||||
require.NoError(t, err)
|
||||
// This is an advisory lock on the DB which is released when the DB session ends.
|
||||
err = man.ExclusiveLock(ctx)
|
||||
@@ -220,9 +216,9 @@ func TestVerifyOplogIsEmpty(t *testing.T) {
|
||||
dBase, err := common.SqlOpen(dialect, u)
|
||||
require.NoError(t, err)
|
||||
|
||||
man, err := schema.NewManager(ctx, dialect, dBase)
|
||||
man, err := schema.NewManager(ctx, schema.Dialect(dialect), dBase)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, man.RollForward(ctx))
|
||||
require.NoError(t, man.ApplyMigrations(ctx))
|
||||
|
||||
cmd := InitCommand{Command: base.NewCommand(cli.NewMockUi())}
|
||||
cmd.srv = base.NewServer(&base.Command{UI: cmd.UI})
|
||||
|
||||
Reference in new issue
Block a user