Files
threshold/internal/cmd/commands/database/funcs_test.go
T
Timothy Messier 79866a287f 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.
2022-08-22 12:15:20 -07:00

233 lines
6.7 KiB
Go

package database
import (
"context"
"testing"
"github.com/hashicorp/boundary/internal/cmd/base"
"github.com/hashicorp/boundary/internal/db/common"
"github.com/hashicorp/boundary/internal/db/schema"
"github.com/hashicorp/boundary/testing/dbtest"
"github.com/mitchellh/cli"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestMigrateDatabase(t *testing.T) {
ctx := context.Background()
dialect := dbtest.Postgres
cases := []struct {
name string
initialized bool
urlProvider func() string
expectedCode int
expectedOutput string
expectedError string
}{
{
name: "not_initialized_expected_not_intialized",
initialized: false,
urlProvider: func() string {
c, u, _, err := dbtest.StartUsingTemplate(dialect, dbtest.WithTemplate(dbtest.Template1))
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, c())
})
return u
},
expectedCode: 0,
expectedOutput: "Migrations successfully run.\n",
},
{
name: "basic_initialized_expects_initialized",
initialized: true,
urlProvider: func() string {
c, u, _, err := dbtest.StartUsingTemplate(dialect)
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, c())
})
dBase, err := common.SqlOpen(dialect, u)
require.NoError(t, err)
earlyMigrationVersion := 2000
man, err := schema.NewManager(ctx, schema.Dialect(dialect), dBase, schema.WithEditions(
schema.TestCreatePartialEditions(schema.Dialect(dialect), schema.PartialEditions{"oss": earlyMigrationVersion}),
))
require.NoError(t, err)
_, err = man.ApplyMigrations(ctx)
require.NoError(t, err)
return u
},
expectedCode: 0,
expectedOutput: "Migrations successfully run.\n",
},
{
name: "basic_initialized_expects_not_initialized",
initialized: false,
urlProvider: func() string {
c, u, _, err := dbtest.StartUsingTemplate(dialect)
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, c())
})
dBase, err := common.SqlOpen(dialect, u)
require.NoError(t, err)
earlyMigrationVersion := 2000
man, err := schema.NewManager(ctx, schema.Dialect(dialect), dBase, schema.WithEditions(
schema.TestCreatePartialEditions(schema.Dialect(dialect), schema.PartialEditions{"oss": earlyMigrationVersion}),
))
require.NoError(t, err)
_, err = man.ApplyMigrations(ctx)
require.NoError(t, err)
return u
},
expectedCode: -1,
expectedOutput: "Database has already been initialized. Please use 'boundary database migrate'\nfor any upgrade needs.\n",
},
{
name: "old_version_table_used_intialized",
initialized: true,
urlProvider: func() string {
c, u, _, err := dbtest.StartUsingTemplate(dialect, dbtest.WithTemplate(dbtest.Template1))
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, c())
})
dBase, err := common.SqlOpen(dialect, u)
require.NoError(t, err)
createStmt := `create table if not exists schema_migrations (version bigint primary key, dirty boolean not null)`
_, err = dBase.Exec(createStmt)
require.NoError(t, err)
return u
},
expectedCode: 0,
expectedOutput: "Migrations successfully run.\n",
},
{
name: "old_version_table_used_not_intialized",
initialized: false,
urlProvider: func() string {
c, u, _, err := dbtest.StartUsingTemplate(dialect, dbtest.WithTemplate(dbtest.Template1))
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, c())
})
dBase, err := common.SqlOpen(dialect, u)
require.NoError(t, err)
createStmt := `create table if not exists schema_migrations (version bigint primary key, dirty boolean not null)`
_, err = dBase.Exec(createStmt)
require.NoError(t, err)
return u
},
expectedCode: -1,
expectedOutput: "Database has already been initialized. Please use 'boundary database migrate'\nfor any upgrade needs.\n",
},
{
name: "bad_url_initialized",
initialized: true,
urlProvider: func() string { return "badurl" },
expectedCode: 2,
expectedError: "Unable to connect to the database at \"badurl\"\n",
},
{
name: "bad_url_not_intialized",
initialized: false,
urlProvider: func() string { return "badurl" },
expectedCode: 2,
expectedError: "Unable to connect to the database at \"badurl\"\n",
},
{
name: "cant_get_lock_initialized",
initialized: true,
urlProvider: func() string {
c, u, _, err := dbtest.StartUsingTemplate(dialect)
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, c())
})
dBase, err := common.SqlOpen(dialect, u)
require.NoError(t, err)
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)
require.NoError(t, err)
return u
},
expectedCode: 2,
expectedError: "Unable to capture a lock on the database.\n",
},
{
name: "cant_get_lock_not_initialized",
initialized: false,
urlProvider: func() string {
c, u, _, err := dbtest.StartUsingTemplate(dialect)
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, c())
})
dBase, err := common.SqlOpen(dialect, u)
require.NoError(t, err)
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)
require.NoError(t, err)
return u
},
expectedCode: 2,
expectedError: "Unable to capture a lock on the database.\n",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
u := tc.urlProvider()
ui := cli.NewMockUi()
clean, errCode := migrateDatabase(ctx, ui, dialect, u, tc.initialized, 10, nil)
clean()
assert.EqualValues(t, tc.expectedCode, errCode)
assert.Equal(t, tc.expectedOutput, ui.OutputWriter.String())
assert.Equal(t, tc.expectedError, ui.ErrorWriter.String())
})
}
}
func TestVerifyOplogIsEmpty(t *testing.T) {
dialect := "postgres"
ctx := context.Background()
c, u, _, err := dbtest.StartUsingTemplate(dialect)
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, c())
})
_ = c
dBase, err := common.SqlOpen(dialect, u)
require.NoError(t, err)
man, err := schema.NewManager(ctx, schema.Dialect(dialect), dBase)
require.NoError(t, err)
_, err = man.ApplyMigrations(ctx)
require.NoError(t, err)
cmd := InitCommand{Server: base.NewServer(base.NewCommand(cli.NewMockUi()))}
cmd.DatabaseUrl = u
require.NoError(t, cmd.ConnectToDatabase(ctx, dialect))
assert.NoError(t, cmd.verifyOplogIsEmpty(ctx))
}