diff --git a/internal/db/pull/pull.go b/internal/db/pull/pull.go index 9c822971b..1ea776e80 100644 --- a/internal/db/pull/pull.go +++ b/internal/db/pull/pull.go @@ -7,7 +7,6 @@ import ( "math" "os" "path/filepath" - "slices" "strconv" "strings" @@ -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" @@ -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 { @@ -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 @@ -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 { diff --git a/internal/db/pull/pull_test.go b/internal/db/pull/pull_test.go index 293448156..49fecb19f 100644 --- a/internal/db/pull/pull_test.go +++ b/internal/db/pull/pull_test.go @@ -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()