Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 516
fix(cli): repair remote db pull with pg-delta and per-connection role step-down#5895
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 “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
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6ffa721
fix(cli): repair remote db pull with pg-delta and per-connection role…
avallete 4407659
fix(cli): write one migration file per pg-delta plan unit and format …
avallete 70f1717
fix(cli): close failed connection-probe pools and report all pulled m…
avallete 797c43b
Merge branch 'develop' into avallete/slack-message-bug-debug-adb372
avallete 555063e
fix(cli): split db diff file output by plan unit and record pulled hi…
avallete d72d355
fix(cli): harden the pg-delta migration writer against collisions, ne…
avallete File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -38,7 +38,7 @@ func Run(ctx context.Context, schema []string, file string, config pgconn.Config | ||
| out := result.SQL | ||
| branch := utils.GetGitBranch(fsys) | ||
| fmt.Fprintln(os.Stderr, "Finished "+utils.Aqua("supabase db diff")+" on branch "+utils.Aqua(branch)+".\n") | ||
| if err := SaveDiff(out, file, fsys); err != nil { | ||
| if err := SaveDiff(result, file, fsys); err != nil { | ||
| return err | ||
| } | ||
| drops := findDropStatements(out) | ||
| @@ -225,22 +225,31 @@ func DiffDatabase(ctx context.Context, schema []string, config pgconn.Config, w | ||
| } else { | ||
| fmt.Fprintln(w, "Diffing schemas...") | ||
| } | ||
| if IsPgDeltaDebugEnabled() && usePgDelta { | ||
| // Capture the shadow baseline catalog and edge-runtime stderr so an | ||
| // empty diff can be inspected later. DiffPgDeltaRefDetailed mirrors the | ||
| // pg-delta differ but additionally surfaces stderr, which differ() drops. | ||
| debugCapture := &PgDeltaDebugCapture{} | ||
| if snapshot, exportErr := exportCatalogPgDelta(ctx, utils.ToPostgresURL(shadowConfig), "postgres", options...); exportErr == nil { | ||
| debugCapture.SourceCatalog = snapshot | ||
| } else { | ||
| fmt.Fprintf(w, "Warning: failed to export shadow pg-delta catalog: %v\n", exportErr) | ||
| if usePgDelta { | ||
| // pg-delta always goes through the diffPgDeltaRefDetailed seam so callers get | ||
| // the execution-aware per-unit files (db pull writes one migration file each); | ||
| // db diff/declarative flatten them back via SQL. This mirrors the config-based | ||
| // differ (DiffPgDelta) exactly, so it is safe to bypass the injected differ() | ||
| // here — differ() remains the migra engine path below. | ||
| var debugCapture *PgDeltaDebugCapture | ||
| if IsPgDeltaDebugEnabled() { | ||
| // Capture the shadow baseline catalog and edge-runtime stderr so an | ||
| // empty diff can be inspected later. | ||
| debugCapture = &PgDeltaDebugCapture{} | ||
| if snapshot, exportErr := exportCatalogPgDelta(ctx, utils.ToPostgresURL(shadowConfig), "postgres", options...); exportErr == nil { | ||
| debugCapture.SourceCatalog = snapshot | ||
| } else { | ||
| fmt.Fprintf(w, "Warning: failed to export shadow pg-delta catalog: %v\n", exportErr) | ||
| } | ||
| } | ||
| result, err := DiffPgDeltaRefDetailed(ctx, utils.ToPostgresURL(shadowConfig), utils.ToPostgresURL(config), schema, pgDeltaFormatOptions(), options...) | ||
| result, err := diffPgDeltaRefDetailed(ctx, utils.ToPostgresURL(shadowConfig), utils.ToPostgresURL(config), schema, pgDeltaFormatOptions(), options...) | ||
| if err != nil { | ||
| return DatabaseDiff{}, err | ||
| } | ||
| debugCapture.Stderr = result.Stderr | ||
| return DatabaseDiff{SQL: result.SQL, Debug: debugCapture}, nil | ||
| if debugCapture != nil { | ||
| debugCapture.Stderr = result.Stderr | ||
| } | ||
| return DatabaseDiff{SQL: joinPgDeltaFiles(result.Files), Files: result.Files, Debug: debugCapture}, nil | ||
avallete marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| output, err := differ(ctx, shadowConfig, config, schema, options...) | ||
| if err != nil { | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| package diff | ||
| import ( | ||
| "testing" | ||
| "github.com/spf13/afero" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| "github.com/supabase/cli/internal/utils" | ||
| ) | ||
| func TestSaveDiff(t *testing.T) { | ||
| t.Run("reports no changes on empty diff", func(t *testing.T) { | ||
| fsys := afero.NewMemMapFs() | ||
| require.NoError(t, SaveDiff(DatabaseDiff{SQL: ""}, "my_diff", fsys)) | ||
| // Nothing written when there are no schema changes. | ||
| entries, err := afero.ReadDir(fsys, utils.MigrationsDir) | ||
| assert.Error(t, err) | ||
| assert.Empty(t, entries) | ||
| }) | ||
| t.Run("writes a single migration file for a single-unit plan", func(t *testing.T) { | ||
| fsys := afero.NewMemMapFs() | ||
| files := []PgDeltaPlanFile{ | ||
| {Order: 1, Name: "schema_changes", TransactionMode: "transactional", SQL: "create table a ();"}, | ||
| } | ||
| result := DatabaseDiff{SQL: joinPgDeltaFiles(files), Files: files} | ||
| require.NoError(t, SaveDiff(result, "my_diff", fsys)) | ||
| entries, err := afero.ReadDir(fsys, utils.MigrationsDir) | ||
| require.NoError(t, err) | ||
| require.Len(t, entries, 1) | ||
| // A single-unit plan keeps the plain `<ts>_<name>.sql` name and the exact | ||
| // diff SQL, byte-identical to the pre-multi-file behavior (no trailing newline | ||
| // added, no unit-name suffix). | ||
| assert.Regexp(t, `^\d{14}_my_diff\.sql$`, entries[0].Name()) | ||
| contents, err := afero.ReadFile(fsys, utils.MigrationsDir+"/"+entries[0].Name()) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, "create table a ();", string(contents)) | ||
| }) | ||
| t.Run("writes one migration file per unit for a multi-unit plan", func(t *testing.T) { | ||
| fsys := afero.NewMemMapFs() | ||
| files := []PgDeltaPlanFile{ | ||
| {Order: 1, Name: "schema_changes", TransactionMode: "transactional", SQL: "alter type mood add value 'ok';"}, | ||
| {Order: 2, Name: "after_enum_values", TransactionMode: "transactional", SQL: "insert into t values ('ok');"}, | ||
| } | ||
| result := DatabaseDiff{SQL: joinPgDeltaFiles(files), Files: files} | ||
| require.NoError(t, SaveDiff(result, "my_diff", fsys)) | ||
| entries, err := afero.ReadDir(fsys, utils.MigrationsDir) | ||
| require.NoError(t, err) | ||
| require.Len(t, entries, 2) | ||
| // Multi-unit plans split into one ordered file per unit, each suffixed with the | ||
| // unit name, so `db push`/`reset` applies each unit as its own transaction. | ||
| assert.Regexp(t, `^\d{14}_my_diff_schema_changes\.sql$`, entries[0].Name()) | ||
| assert.Regexp(t, `^\d{14}_my_diff_after_enum_values\.sql$`, entries[1].Name()) | ||
| }) | ||
| t.Run("prints diff to stdout when no file is given", func(t *testing.T) { | ||
| fsys := afero.NewMemMapFs() | ||
| require.NoError(t, SaveDiff(DatabaseDiff{SQL: "create table a ();"}, "", fsys)) | ||
| entries, _ := afero.ReadDir(fsys, utils.MigrationsDir) | ||
| assert.Empty(t, entries) | ||
| }) | ||
| t.Run("creates nested parent directories for a nested single-unit name", func(t *testing.T) { | ||
| fsys := afero.NewMemMapFs() | ||
| require.NoError(t, SaveDiff(DatabaseDiff{SQL: "create table a ();"}, "snapshots/remote", fsys)) | ||
| matches, err := afero.Glob(fsys, utils.MigrationsDir+"/*_snapshots/remote.sql") | ||
| require.NoError(t, err) | ||
| require.Len(t, matches, 1) | ||
| contents, err := afero.ReadFile(fsys, matches[0]) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, "create table a ();", string(contents)) | ||
| }) | ||
| t.Run("creates nested parent directories for a nested multi-unit name", func(t *testing.T) { | ||
| fsys := afero.NewMemMapFs() | ||
| files := []PgDeltaPlanFile{ | ||
| {Order: 1, Name: "schema_changes", TransactionMode: "transactional", SQL: "alter type mood add value 'ok';"}, | ||
| {Order: 2, Name: "after_enum_values", TransactionMode: "transactional", SQL: "insert into t values ('ok');"}, | ||
| } | ||
| result := DatabaseDiff{SQL: joinPgDeltaFiles(files), Files: files} | ||
| require.NoError(t, SaveDiff(result, "snapshots/remote", fsys)) | ||
| matches, err := afero.Glob(fsys, utils.MigrationsDir+"/*_snapshots/remote_*.sql") | ||
| require.NoError(t, err) | ||
| require.Len(t, matches, 2) | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.