Skip to content
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
69 changes: 3 additions & 66 deletions internal/db/pull/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"math"
"os"
"path/filepath"
"slices"
"strconv"
"strings"

Expand All @@ -17,7 +16,6 @@ import (
"github.com/spf13/afero"
"github.com/supabase/cli/internal/db/diff"
"github.com/supabase/cli/internal/db/dump"
"github.com/supabase/cli/internal/db/start"
"github.com/supabase/cli/internal/migration/list"
"github.com/supabase/cli/internal/migration/new"
"github.com/supabase/cli/internal/migration/repair"
Expand Down Expand Up @@ -71,16 +69,8 @@ func run(ctx context.Context, schema []string, path string, conn *pgx.Conn, fsys
} else if err != nil {
return err
}
// 2. Fetch user defined schemas
if len(schema) == 0 {
var err error
if schema, err = migration.ListUserSchemas(ctx, conn); err != nil {
return err
}
schema = append(schema, managedSchemas...)
}
// 3. Fetch remote schema changes
return diffUserSchemas(ctx, schema, path, config, fsys)
// 2. Fetch remote schema changes
return diffRemoteSchema(ctx, schema, path, config, fsys)
}

func dumpRemoteSchema(ctx context.Context, path string, config pgconn.Config, fsys afero.Fs) error {
Expand All @@ -103,7 +93,7 @@ func diffRemoteSchema(ctx context.Context, schema []string, path string, config
if err != nil {
return err
}
if len(output) == 0 {
if trimmed := strings.TrimSpace(output); len(trimmed) == 0 {
return errors.New(errInSync)
}
// Append to existing migration file since we run this after dump
Expand All @@ -118,59 +108,6 @@ func diffRemoteSchema(ctx context.Context, schema []string, path string, config
return nil
}

func diffUserSchemas(ctx context.Context, schema []string, path string, config pgconn.Config, fsys afero.Fs) error {
var managed, user []string
for _, s := range schema {
if slices.Contains(managedSchemas, s) {
managed = append(managed, s)
} else {
user = append(user, s)
}
}
fmt.Fprintln(os.Stderr, "Creating shadow database...")
shadow, err := diff.CreateShadowDatabase(ctx, utils.Config.Db.ShadowPort)
if err != nil {
return err
}
defer utils.DockerRemove(shadow)
if err := start.WaitForHealthyService(ctx, start.HealthTimeout, shadow); err != nil {
return err
}
if err := diff.MigrateShadowDatabase(ctx, shadow, fsys); err != nil {
return err
}
shadowConfig := pgconn.Config{
Host: utils.Config.Hostname,
Port: utils.Config.Db.ShadowPort,
User: "postgres",
Password: utils.Config.Db.Password,
Database: "postgres",
}
// Diff managed and user defined schemas separately
var output string
if len(user) > 0 {
fmt.Fprintln(os.Stderr, "Diffing schemas:", strings.Join(user, ","))
if output, err = diff.DiffSchemaMigraBash(ctx, shadowConfig, config, user); err != nil {
return err
}
}
if len(managed) > 0 {
fmt.Fprintln(os.Stderr, "Diffing schemas:", strings.Join(managed, ","))
if result, err := diff.DiffSchemaMigra(ctx, shadowConfig, config, managed); err != nil {
return err
} else {
output += result
}
}
if len(output) == 0 {
return errors.New(errInSync)
}
if err := utils.WriteFile(path, []byte(output), fsys); err != nil {
return errors.Errorf("failed to write dump file: %w", err)
}
return nil
}

func assertRemoteInSync(ctx context.Context, conn *pgx.Conn, fsys afero.Fs) error {
remoteMigrations, err := migration.ListRemoteMigrations(ctx, conn)
if err != nil {
Expand Down
18 changes: 0 additions & 18 deletions internal/db/pull/pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,24 +83,6 @@ func TestPullSchema(t *testing.T) {
assert.Equal(t, []byte("test"), contents)
})

t.Run("throws error on load user schema failure", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
path := filepath.Join(utils.MigrationsDir, "0_test.sql")
require.NoError(t, afero.WriteFile(fsys, path, []byte(""), 0644))
// Setup mock postgres
conn := pgtest.NewConn()
defer conn.Close(t)
conn.Query(migration.LIST_MIGRATION_VERSION).
Reply("SELECT 1", []any{"0"}).
Query(migration.ListSchemas, migration.ManagedSchemas).
ReplyError(pgerrcode.DuplicateTable, `relation "test" already exists`)
// Run test
err := run(context.Background(), nil, "", conn.MockClient(t), fsys)
// Check error
assert.ErrorContains(t, err, `ERROR: relation "test" already exists (SQLSTATE 42P07)`)
})

t.Run("throws error on diff failure", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
Expand Down
Loading