Skip to content

Commit 223e373

Browse files
authored
fix: allow diff to run with stopped db (#3829)
1 parent 1f45c83 commit 223e373

File tree

2 files changed

+6
-22
lines changed

2 files changed

+6
-22
lines changed

internal/db/diff/diff.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,6 @@ import (
3131
type DiffFunc func(context.Context, string, string, []string) (string, error)
3232

3333
func Run(ctx context.Context, schema []string, file string, config pgconn.Config, differ DiffFunc, fsys afero.Fs, options ...func(*pgx.ConnConfig)) (err error) {
34-
// 1. Load all user defined schemas
35-
if len(schema) == 0 {
36-
schema, err = loadSchema(ctx, config, options...)
37-
if err != nil {
38-
return err
39-
}
40-
}
41-
// 3. Run migra to diff schema
4234
out, err := DiffDatabase(ctx, schema, config, os.Stderr, fsys, differ, options...)
4335
if err != nil {
4436
return err
@@ -185,6 +177,12 @@ func DiffDatabase(ctx context.Context, schema []string, config pgconn.Config, w
185177
}
186178
}
187179
}
180+
// Load all user defined schemas
181+
if len(schema) == 0 {
182+
if schema, err = loadSchema(ctx, config, options...); err != nil {
183+
return "", err
184+
}
185+
}
188186
fmt.Fprintln(w, "Diffing schemas:", strings.Join(schema, ","))
189187
source := utils.ToPostgresURL(shadowConfig)
190188
target := utils.ToPostgresURL(config)

internal/db/diff/diff_test.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -87,20 +87,6 @@ func TestRun(t *testing.T) {
8787
assert.Equal(t, []byte(diff), contents)
8888
})
8989

90-
t.Run("throws error on failure to load user schemas", func(t *testing.T) {
91-
// Setup in-memory fs
92-
fsys := afero.NewMemMapFs()
93-
// Setup mock postgres
94-
conn := pgtest.NewConn()
95-
defer conn.Close(t)
96-
conn.Query(migration.ListSchemas, migration.ManagedSchemas).
97-
ReplyError(pgerrcode.DuplicateTable, `relation "test" already exists`)
98-
// Run test
99-
err := Run(context.Background(), []string{}, "", dbConfig, DiffSchemaMigra, fsys, conn.Intercept)
100-
// Check error
101-
assert.ErrorContains(t, err, `ERROR: relation "test" already exists (SQLSTATE 42P07)`)
102-
})
103-
10490
t.Run("throws error on failure to diff target", func(t *testing.T) {
10591
// Setup in-memory fs
10692
fsys := afero.NewMemMapFs()

0 commit comments

Comments
 (0)