Skip to content

Commit 3725f55

Browse files
authored
fix: account for declared schemas in config file (#3355)
1 parent c8778df commit 3725f55

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

internal/db/diff/diff.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,16 @@ type DiffFunc func(context.Context, string, string, []string) (string, error)
3333
func Run(ctx context.Context, schema []string, file string, config pgconn.Config, differ DiffFunc, fsys afero.Fs, options ...func(*pgx.ConnConfig)) (err error) {
3434
// Sanity checks.
3535
if utils.IsLocalDatabase(config) {
36-
if container, err := createShadowIfNotExists(ctx, fsys); err != nil {
36+
if declared, err := loadDeclaredSchemas(fsys); err != nil {
37+
return err
38+
} else if container, err := createShadowIfNotExists(ctx, declared); err != nil {
3739
return err
3840
} else if len(container) > 0 {
3941
defer utils.DockerRemove(container)
4042
if err := start.WaitForHealthyService(ctx, start.HealthTimeout, container); err != nil {
4143
return err
4244
}
43-
if err := migrateBaseDatabase(ctx, container, fsys, options...); err != nil {
45+
if err := migrateBaseDatabase(ctx, container, declared, fsys, options...); err != nil {
4446
return err
4547
}
4648
}
@@ -70,23 +72,31 @@ func Run(ctx context.Context, schema []string, file string, config pgconn.Config
7072
return nil
7173
}
7274

73-
func createShadowIfNotExists(ctx context.Context, fsys afero.Fs) (string, error) {
74-
if exists, err := afero.DirExists(fsys, utils.SchemasDir); err != nil {
75-
return "", errors.Errorf("failed to check schemas: %w", err)
76-
} else if !exists {
75+
func createShadowIfNotExists(ctx context.Context, migrations []string) (string, error) {
76+
if len(migrations) == 0 {
7777
return "", nil
7878
}
7979
if err := utils.AssertSupabaseDbIsRunning(); !errors.Is(err, utils.ErrNotRunning) {
8080
return "", err
8181
}
82-
fmt.Fprintf(os.Stderr, "Creating local database from %s...\n", utils.Bold(utils.SchemasDir))
82+
fmt.Fprintln(os.Stderr, "Creating local database from declarative schemas:")
83+
msg := make([]string, len(migrations))
84+
for i, m := range migrations {
85+
msg[i] = fmt.Sprintf(" • %s", utils.Bold(m))
86+
}
87+
fmt.Fprintln(os.Stderr, strings.Join(msg, "\n"))
8388
return CreateShadowDatabase(ctx, utils.Config.Db.Port)
8489
}
8590

8691
func loadDeclaredSchemas(fsys afero.Fs) ([]string, error) {
8792
if schemas := utils.Config.Db.Migrations.SchemaPaths; len(schemas) > 0 {
8893
return schemas.Files(afero.NewIOFS(fsys))
8994
}
95+
if exists, err := afero.DirExists(fsys, utils.SchemasDir); err != nil {
96+
return nil, errors.Errorf("failed to check schemas: %w", err)
97+
} else if !exists {
98+
return nil, nil
99+
}
90100
var declared []string
91101
if err := afero.Walk(fsys, utils.SchemasDir, func(path string, info fs.FileInfo, err error) error {
92102
if err != nil {
@@ -170,11 +180,7 @@ func MigrateShadowDatabase(ctx context.Context, container string, fsys afero.Fs,
170180
return migration.ApplyMigrations(ctx, migrations, conn, afero.NewIOFS(fsys))
171181
}
172182

173-
func migrateBaseDatabase(ctx context.Context, container string, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error {
174-
migrations, err := loadDeclaredSchemas(fsys)
175-
if err != nil {
176-
return err
177-
}
183+
func migrateBaseDatabase(ctx context.Context, container string, migrations []string, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error {
178184
conn, err := utils.ConnectLocalPostgres(ctx, pgconn.Config{}, options...)
179185
if err != nil {
180186
return err

0 commit comments

Comments
 (0)