Skip to content

Commit d880548

Browse files
committed
fix: write to declarative schemas instead
1 parent c6e059f commit d880548

File tree

3 files changed

+21
-23
lines changed

3 files changed

+21
-23
lines changed

internal/clone/clone.go

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,11 @@ import (
99
"github.com/cenkalti/backoff/v4"
1010
"github.com/go-errors/errors"
1111
"github.com/jackc/pgconn"
12-
"github.com/jackc/pgx/v4"
1312
"github.com/spf13/afero"
1413
"github.com/spf13/viper"
1514
"github.com/supabase/cli/internal/db/pull"
1615
"github.com/supabase/cli/internal/link"
1716
"github.com/supabase/cli/internal/login"
18-
"github.com/supabase/cli/internal/migration/new"
19-
"github.com/supabase/cli/internal/migration/repair"
2017
"github.com/supabase/cli/internal/projects/apiKeys"
2118
"github.com/supabase/cli/internal/utils"
2219
"github.com/supabase/cli/internal/utils/flags"
@@ -98,26 +95,12 @@ func linkProject(ctx context.Context, fsys afero.Fs) error {
9895
return utils.WriteFile(utils.ProjectRefPath, []byte(flags.ProjectRef), fsys)
9996
}
10097

101-
func dumpRemoteSchema(ctx context.Context, config pgconn.Config, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error {
102-
// 1. Check postgres connection
103-
conn, err := utils.ConnectByConfig(ctx, config, options...)
104-
if err != nil {
105-
return err
106-
}
107-
defer conn.Close(context.Background())
108-
// 2. Pull schema
109-
timestamp := utils.GetCurrentTimestamp()
110-
path := new.GetMigrationPath(timestamp, "remote_schema")
111-
// Ignore schemas flag when working on the initial pull
112-
if err = pull.CloneRemoteSchema(ctx, path, config, fsys); err != nil {
113-
return err
114-
}
115-
// 3. Insert a row to `schema_migrations`
116-
fmt.Fprintln(os.Stderr, "Schema written to "+utils.Bold(path))
117-
if shouldUpdate, err := utils.NewConsole().PromptYesNo(ctx, "Update remote migration history table?", true); err != nil {
98+
func dumpRemoteSchema(ctx context.Context, config pgconn.Config, fsys afero.Fs) error {
99+
schemaPath := filepath.Join(utils.SchemasDir, "remote.sql")
100+
utils.Config.Db.Migrations.SchemaPaths = append(utils.Config.Db.Migrations.SchemaPaths, filepath.ToSlash(schemaPath))
101+
if err := pull.CloneRemoteSchema(ctx, schemaPath, config, fsys); err != nil {
118102
return err
119-
} else if shouldUpdate {
120-
return repair.UpdateMigrationTable(ctx, conn, []string{timestamp}, repair.Applied, true, fsys)
121103
}
104+
fmt.Fprintln(os.Stderr, "Schema written to "+utils.Bold(schemaPath))
122105
return nil
123106
}

internal/db/diff/diff.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,21 @@ func MigrateShadowDatabase(ctx context.Context, container string, fsys afero.Fs,
133133
if _, err := conn.Exec(ctx, CREATE_TEMPLATE); err != nil {
134134
return errors.Errorf("failed to create template database: %w", err)
135135
}
136-
return migration.ApplyMigrations(ctx, migrations, conn, afero.NewIOFS(fsys))
136+
// Migrations take precedence over declarative schemas
137+
if len(migrations) > 0 {
138+
return migration.ApplyMigrations(ctx, migrations, conn, afero.NewIOFS(fsys))
139+
}
140+
declared, err := loadDeclaredSchemas(fsys)
141+
if err != nil || len(declared) == 0 {
142+
return err
143+
}
144+
fmt.Fprintln(os.Stderr, "Creating local database from declarative schemas:")
145+
msg := make([]string, len(declared))
146+
for i, m := range declared {
147+
msg[i] = fmt.Sprintf(" • %s", utils.Bold(m))
148+
}
149+
fmt.Fprintln(os.Stderr, strings.Join(msg, "\n"))
150+
return migration.SeedGlobals(ctx, declared, conn, afero.NewIOFS(fsys))
137151
}
138152

139153
func DiffDatabase(ctx context.Context, schema []string, config pgconn.Config, w io.Writer, fsys afero.Fs, differ DiffFunc, options ...func(*pgx.ConnConfig)) (string, error) {

pkg/migration/scripts/dump_schema.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ pg_dump \
4848
| sed -E "s/^REVOKE (.+) ON (.+) \"(${EXCLUDED_SCHEMAS:-})\"/-- &/" \
4949
| sed -E 's/^(CREATE EXTENSION IF NOT EXISTS "pg_tle").+/\1;/' \
5050
| sed -E 's/^(CREATE EXTENSION IF NOT EXISTS "pgsodium").+/\1;/' \
51+
| sed -E 's/^(CREATE EXTENSION IF NOT EXISTS "pgmq").+/\1;/' \
5152
| sed -E 's/^COMMENT ON EXTENSION (.+)/-- &/' \
5253
| sed -E 's/^CREATE POLICY "cron_job_/-- &/' \
5354
| sed -E 's/^ALTER TABLE "cron"/-- &/' \

0 commit comments

Comments
 (0)