Skip to content

Commit

Permalink
Fix hardcoded pgroll schema in state initialization (#284)
Browse files Browse the repository at this point in the history
Fix some hard-coded occurrences of the `pgroll` schema name where the
schema name should instead be parameterized.

This isn't the first time we've made a mistake like this so also add a
test to guard against it.
  • Loading branch information
andrew-farries committed Feb 22, 2024
1 parent 6e4ee68 commit 161fde6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
6 changes: 3 additions & 3 deletions pkg/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,18 @@ STABLE;
CREATE OR REPLACE FUNCTION %[1]s.previous_version(schemaname NAME) RETURNS text
AS $$
WITH RECURSIVE find_ancestor AS (
SELECT schema, name, parent, migration_type FROM pgroll.migrations
SELECT schema, name, parent, migration_type FROM %[1]s.migrations
WHERE name = (SELECT %[1]s.latest_version(schemaname)) AND schema = schemaname
UNION ALL
SELECT m.schema, m.name, m.parent, m.migration_type FROM pgroll.migrations m
SELECT m.schema, m.name, m.parent, m.migration_type FROM %[1]s.migrations m
INNER JOIN find_ancestor fa ON fa.parent = m.name AND fa.schema = m.schema
WHERE m.migration_type = 'inferred'
)
SELECT a.parent
FROM find_ancestor AS a
JOIN pgroll.migrations AS b ON a.parent = b.name AND a.schema = b.schema
JOIN %[1]s.migrations AS b ON a.parent = b.name AND a.schema = b.schema
WHERE b.migration_type = 'pgroll';
$$
LANGUAGE SQL
Expand Down
18 changes: 18 additions & 0 deletions pkg/state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,24 @@ func TestInferredMigration(t *testing.T) {
})
}

func TestPgRollInitializationInANonDefaultSchema(t *testing.T) {
t.Parallel()

testutils.WithStateInSchemaAndConnectionToContainer(t, "pgroll_foo", func(state *state.State, _ *sql.DB) {
ctx := context.Background()

// Ensure that pgroll state has been correctly initialized in the
// non-default schema `pgroll_foo` by performing a basic operation on the
// state
migrationActive, err := state.IsActiveMigrationPeriod(ctx, "public")
if err != nil {
t.Fatal(err)
}

assert.False(t, migrationActive)
})
}

func TestReadSchema(t *testing.T) {
t.Parallel()

Expand Down
8 changes: 6 additions & 2 deletions pkg/testutils/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func TestSchema() string {
return "public"
}

func WithStateAndConnectionToContainer(t *testing.T, fn func(*state.State, *sql.DB)) {
func WithStateInSchemaAndConnectionToContainer(t *testing.T, schema string, fn func(*state.State, *sql.DB)) {
t.Helper()
ctx := context.Background()

Expand Down Expand Up @@ -115,7 +115,7 @@ func WithStateAndConnectionToContainer(t *testing.T, fn func(*state.State, *sql.
u.Path = "/" + dbName
connStr := u.String()

st, err := state.New(ctx, connStr, "pgroll")
st, err := state.New(ctx, connStr, schema)
if err != nil {
t.Fatal(err)
}
Expand All @@ -139,6 +139,10 @@ func WithStateAndConnectionToContainer(t *testing.T, fn func(*state.State, *sql.
fn(st, db)
}

func WithStateAndConnectionToContainer(t *testing.T, fn func(*state.State, *sql.DB)) {
WithStateInSchemaAndConnectionToContainer(t, "pgroll", fn)
}

func WithMigratorInSchemaAndConnectionToContainerWithOptions(t *testing.T, schema string, opts []roll.Option, fn func(mig *roll.Roll, db *sql.DB)) {
t.Helper()
ctx := context.Background()
Expand Down

0 comments on commit 161fde6

Please sign in to comment.