feat(schema): Add support for migration hooks
This adds the ability to define hooks that will run prior to a schema migration. The hooks should only be used when a migration would fail due to data being in an invalid state, likely due to a bug in the previous schema definition. For example if a column was missing a constraint, and a new migration is attempting to add the constraint, there might already be values that violate the new constraint. In such cases, we would want the user to make a decision to address the data, and need to provide them with details about what data is problematic in case they want to manually address the problem. To help the user, if there is a sane way to address the data, we should provide an option to automatically "repair" the data, allowing the migration to continue. However, this still should be an explicit opt-in from the user, and we need to clearly describe what the repair option would do to the user so they understand the consequences of opting in. To accomplish this each migration can be provided a hook that contains a CheckFunc, RepairFunc, and RepairDescription. These functions must operate at the point-in-time of the migration immediately preceding the current migration, as as such they should not use and domain packages or repositories, instead they should use direct sql with the sql library. The description is used to explain what the RepairFunc will do. Hooks should be defined in `internal/db/schema/migrations/oss/oss.go` and registered with the schema edition. See the example in `internal/db/schema/manager_example_test.go` for more details.
This commit is contained in:
1 parent
1fa79e9aa1
commit
79866a287f
33 files changed
+1256
-156
No files matched your search
@@ -37,6 +37,7 @@ import (
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/db/schema/internal/log"
|
||||
"github.com/hashicorp/boundary/internal/db/schema/migration"
|
||||
"github.com/hashicorp/boundary/internal/db/schema/migrations"
|
||||
"github.com/hashicorp/boundary/internal/errors"
|
||||
"github.com/hashicorp/go-multierror"
|
||||
@@ -157,6 +158,34 @@ func (p *Postgres) StartRun(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// CheckHook is a hook that runs prior to a migration's statements.
|
||||
// It should run in the same transaction as a corresponding Run call.
|
||||
func (p *Postgres) CheckHook(ctx context.Context, f migration.CheckFunc) (migration.Problems, error) {
|
||||
const op = "postgres.(Postgres).CheckHook"
|
||||
if p.tx == nil {
|
||||
return nil, errors.New(ctx, errors.MigrationIntegrity, op, "no pending transaction")
|
||||
}
|
||||
if f == nil {
|
||||
return nil, errors.New(ctx, errors.MigrationIntegrity, op, "no check function")
|
||||
}
|
||||
return f(ctx, p.tx)
|
||||
}
|
||||
|
||||
// RepairHook is a hook that runs prior to a migration's statements.
|
||||
// It should run in the same transaction a corresponding Run call.
|
||||
func (p *Postgres) RepairHook(ctx context.Context, f migration.RepairFunc) (migration.Repairs, error) {
|
||||
const op = "postgres.(Postgres).RepairHook"
|
||||
|
||||
if p.tx == nil {
|
||||
return nil, errors.New(ctx, errors.MigrationIntegrity, op, "no pending transaction")
|
||||
}
|
||||
|
||||
if f == nil {
|
||||
return nil, errors.New(ctx, errors.MigrationIntegrity, op, "no repair function")
|
||||
}
|
||||
return f(ctx, p.tx)
|
||||
}
|
||||
|
||||
// CommitRun commits a transaction, if there is an error it should rollback the transaction.
|
||||
func (p *Postgres) CommitRun(ctx context.Context) error {
|
||||
const op = "postgres.(Postgres).CommitRun"
|
||||
@@ -198,7 +227,7 @@ func (p *Postgres) Run(ctx context.Context, migration io.Reader, version int, ed
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
|
||||
if _, err := p.conn.ExecContext(ctx, query); err != nil {
|
||||
if _, err := p.tx.ExecContext(ctx, query); err != nil {
|
||||
if pgErr, ok := err.(*pgconn.PgError); ok {
|
||||
var line uint
|
||||
var col uint
|
||||
|
||||
Reference in new issue
Block a user