8000 Fix previous_version: Take inferred migrations into account by agedemenli · Pull Request #631 · xataio/pgroll · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix previous_version: Take inferred migrations into account #631

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking &ldquo 8000 ;Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/roll/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (m *Roll) Complete(ctx context.Context) error {

// Drop the old schema
if !m.disableVersionSchemas && (!migration.ContainsRawSQLOperation() || !m.noVersionSchemaForRawSQL) {
prevVersion, err := m.state.PreviousVersion(ctx, m.schema)
prevVersion, err := m.state.PreviousVersion(ctx, m.schema, false)
if err != nil {
return fmt.Errorf("unable to get name of previous version: %w", err)
}
Expand Down Expand Up @@ -241,7 +241,7 @@ func (m *Roll) Rollback(ctx context.Context) error {
}

// get the name of the previous version of the schema
previousVersion, err := m.state.PreviousVersion(ctx, m.schema)
previousVersion, err := m.state.PreviousVersion(ctx, m.schema, true)
if err != nil {
return fmt.Errorf("unable to get name of previous version: %w", err)
}
Expand Down
13 changes: 10 additions & 3 deletions pkg/roll/execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,20 @@ func TestNoVersionSchemaForRawSQLMigrationsOptionIsRespected(t *testing.T) {
err = mig.Start(ctx, &migrations.Migration{Name: "03_create_table", Operations: migrations.Operations{createTableOp("table3")}})
require.NoError(t, err)

// The previous version is migration 01 because there is no version schema
// for migration 02 due to the `WithNoVersionSchemaForRawSQL` option
prevVersion, err := st.PreviousVersion(ctx, "public")
// The previous version is migration 01 if raw SQL migrations are ignored
prevVersion, err := st.PreviousVersion(ctx, "public", false)
require.NoError(t, err)
require.NotNil(t, prevVersion)
assert.Equal(t, "01_create_table", *prevVersion)

// The previous version is migration 02 (inferred) but there is no version schema
// for migration 02 due to the `WithNoVersionSchemaForRawSQL` option
prevVersion, err = st.PreviousVersion(ctx, "public", true)
require.NoError(t, err)
require.NotNil(t, prevVersion)
assert.Equal(t, "02_create_table", *prevVersion)
assert.False(t, schemaExists(t, db, roll.VersionedSchemaName(schema, "02_create_table")))

// Complete the third migration
err = mig.Complete(ctx)
require.NoError(t, err)
Expand Down
15 changes: 11 additions & 4 deletions pkg/state/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ STABLE;
-- Get the name of the previous version of the schema, or NULL if there is none.
-- This ignores previous versions for which no version schema exists, such as
-- versions corresponding to inferred migrations.
CREATE OR REPLACE FUNCTION placeholder.previous_version (schemaname name)
CREATE OR REPLACE FUNCTION placeholder.previous_version (schemaname name, includeInferred boolean)
RETURNS text
AS $$
WITH RECURSIVE ancestors AS (
Expand Down Expand Up @@ -107,10 +107,17 @@ CREATE OR REPLACE FUNCTION placeholder.previous_version (schemaname name)
a.name
FROM
ancestors a
JOIN information_schema.schemata s ON s.schema_name = schemaname || '_' || a.name
WHERE
migration_type = 'pgroll'
AND a.depth > 0
a.depth > 0
AND (includeInferred
OR (a.migration_type = 'pgroll'
AND EXISTS (
SELECT
s.schema_name
FROM
information_schema.schemata s
WHERE
s.schema_name = schemaname || '_' || a.name)))
ORDER BY
a.depth ASC
LIMIT 1;
Expand Down
6 changes: 3 additions & 3 deletions pkg/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,11 @@ func (s *State) LatestVersion(ctx context.Context, schema string) (*string, erro
}

// PreviousVersion returns the name of the previous version schema
func (s *State) PreviousVersion(ctx context.Context, schema string) (*string, error) {
func (s *State) PreviousVersion(ctx context.Context, schema string, includeInferred bool) (*string, error) {
var parent *string
err := s.pgConn.QueryRowContext(ctx,
fmt.Sprintf("SELECT %s.previous_version($1)", pq.QuoteIdentifier(s.schema)),
schema).Scan(&parent)
fmt.Sprintf("SELECT %s.previous_version($1, $2)", pq.QuoteIdentifier(s.schema)),
schema, includeInferred).Scan(&parent)
if err != nil {
return nil, err
}
Expand Down
0