Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: refactor connect by config to identify localhost #1505

Merged
merged 2 commits into from
Oct 26, 2023
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
2 changes: 1 addition & 1 deletion cmd/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ var (
Use: "up",
Short: "Apply pending migrations to local database",
RunE: func(cmd *cobra.Command, args []string) error {
return up.Run(cmd.Context(), includeAll, afero.NewOsFs())
return up.Run(cmd.Context(), includeAll, flags.DbConfig, afero.NewOsFs())
},
PostRun: func(cmd *cobra.Command, args []string) {
fmt.Println("Local database is up to date.")
Expand Down
19 changes: 3 additions & 16 deletions internal/db/diff/migra.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,6 @@ func RunMigra(ctx context.Context, schema []string, file string, config pgconn.C
if err := utils.LoadConfigFS(fsys); err != nil {
return err
}
if config.Host != "127.0.0.1" {
fmt.Fprintln(os.Stderr, "Connecting to remote database...")
} else {
fmt.Fprintln(os.Stderr, "Connecting to local database...")
if err := utils.AssertSupabaseDbIsRunning(); err != nil {
return err
}
}
// 1. Load all user defined schemas
if len(schema) == 0 {
schema, err = loadSchema(ctx, config, options...)
Expand All @@ -59,15 +51,10 @@ func RunMigra(ctx context.Context, schema []string, file string, config pgconn.C
return SaveDiff(out, file, fsys)
}

func loadSchema(ctx context.Context, config pgconn.Config, options ...func(*pgx.ConnConfig)) (schema []string, err error) {
var conn *pgx.Conn
if config.Host == "127.0.0.1" && config.Port == uint16(utils.Config.Db.Port) {
conn, err = utils.ConnectLocalPostgres(ctx, config, options...)
} else {
conn, err = utils.ConnectRemotePostgres(ctx, config, options...)
}
func loadSchema(ctx context.Context, config pgconn.Config, options ...func(*pgx.ConnConfig)) ([]string, error) {
conn, err := utils.ConnectByConfig(ctx, config, options...)
if err != nil {
return schema, err
return nil, err
}
defer conn.Close(context.Background())
return LoadUserSchemas(ctx, conn)
Expand Down
17 changes: 0 additions & 17 deletions internal/db/diff/migra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,23 +92,6 @@ func TestRunMigra(t *testing.T) {
assert.ErrorIs(t, err, os.ErrNotExist)
})

t.Run("throws error on missing database", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
require.NoError(t, utils.WriteConfig(fsys, false))
// Setup mock docker
require.NoError(t, apitest.MockDocker(utils.Docker))
defer gock.OffAll()
gock.New(utils.Docker.DaemonHost()).
Get("/v" + utils.Docker.ClientVersion() + "/containers/supabase_db_").
ReplyError(errors.New("network error"))
// Run test
err := RunMigra(context.Background(), []string{"public"}, "", pgconn.Config{Host: "127.0.0.1"}, fsys)
// Check error
assert.ErrorIs(t, err, utils.ErrNotRunning)
assert.Empty(t, apitest.ListUnmatchedRequests())
})

t.Run("throws error on failure to load user schemas", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
Expand Down
17 changes: 1 addition & 16 deletions internal/db/lint/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func toEnum(level string) LintLevel {

func Run(ctx context.Context, schema []string, level string, config pgconn.Config, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error {
// Sanity checks.
conn, err := connect(ctx, config, fsys, options...)
conn, err := utils.ConnectByConfig(ctx, config, options...)
if err != nil {
return err
}
Expand All @@ -57,21 +57,6 @@ func Run(ctx context.Context, schema []string, level string, config pgconn.Confi
return printResultJSON(result, toEnum(level), os.Stdout)
}

func connect(ctx context.Context, config pgconn.Config, fsys afero.Fs, options ...func(*pgx.ConnConfig)) (*pgx.Conn, error) {
if config.Host != "127.0.0.1" {
fmt.Fprintln(os.Stderr, "Connecting to remote database...")
return utils.ConnectRemotePostgres(ctx, config, options...)
}
fmt.Fprintln(os.Stderr, "Connecting to local database...")
if err := utils.LoadConfigFS(fsys); err != nil {
return nil, err
}
if err := utils.AssertSupabaseDbIsRunning(); err != nil {
return nil, err
}
return utils.ConnectLocalPostgres(ctx, pgconn.Config{}, options...)
}

func filterResult(result []Result, minLevel LintLevel) (filtered []Result) {
for _, r := range result {
out := Result{Function: r.Function}
Expand Down
3 changes: 1 addition & 2 deletions internal/db/pull/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ func Run(ctx context.Context, schema []string, config pgconn.Config, name string
return err
}
// 2. Check postgres connection
fmt.Fprintln(os.Stderr, "Connecting to remote database...")
conn, err := utils.ConnectRemotePostgres(ctx, config, options...)
conn, err := utils.ConnectByConfig(ctx, config, options...)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/db/pull/pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
)

var dbConfig = pgconn.Config{
Host: "127.0.0.1",
Host: "db.supabase.co",
Port: 5432,
User: "admin",
Password: "password",
Expand Down
2 changes: 1 addition & 1 deletion internal/db/push/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func Run(ctx context.Context, dryRun, ignoreVersionMismatch bool, includeRoles,
if dryRun {
fmt.Fprintln(os.Stderr, "DRY RUN: migrations will *not* be pushed to the database.")
}
conn, err := utils.ConnectRemotePostgres(ctx, config, options...)
conn, err := utils.ConnectByConfig(ctx, config, options...)
if err != nil {
return err
}
Expand Down
8 changes: 7 additions & 1 deletion internal/db/push/push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,13 @@ func TestMigrationPush(t *testing.T) {
conn.Query(list.LIST_MIGRATION_VERSION).
ReplyError(pgerrcode.InvalidCatalogName, `database "target" does not exist`)
// Run test
err := Run(context.Background(), false, false, false, false, dbConfig, fsys, conn.Intercept)
err := Run(context.Background(), false, false, false, false, pgconn.Config{
Host: "db.supabase.co",
Port: 5432,
User: "admin",
Password: "password",
Database: "postgres",
}, fsys, conn.Intercept)
// Check error
assert.ErrorContains(t, err, `ERROR: database "target" does not exist (SQLSTATE 3D000)`)
})
Expand Down
2 changes: 1 addition & 1 deletion internal/db/reset/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func Run(ctx context.Context, version string, config pgconn.Config, fsys afero.F
return err
}
}
if config.Host != "127.0.0.1" {
if !utils.IsLoopback(config.Host) {
if shouldReset := utils.PromptYesNo("Confirm resetting the remote database?", true, os.Stdin); !shouldReset {
return context.Canceled
}
Expand Down
2 changes: 1 addition & 1 deletion internal/inspect/bloat/bloat.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ type Result struct {
}

func Run(ctx context.Context, config pgconn.Config, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error {
conn, err := utils.ConnectRemotePostgres(ctx, config, options...)
conn, err := utils.ConnectByConfig(ctx, config, options...)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/inspect/blocking/blocking.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type Result struct {
}

func Run(ctx context.Context, config pgconn.Config, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error {
conn, err := utils.ConnectRemotePostgres(ctx, config, options...)
conn, err := utils.ConnectByConfig(ctx, config, options...)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/inspect/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type Result struct {
}

func Run(ctx context.Context, config pgconn.Config, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error {
conn, err := utils.ConnectRemotePostgres(ctx, config, options...)
conn, err := utils.ConnectByConfig(ctx, config, options...)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/inspect/calls/calls.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type Result struct {
}

func Run(ctx context.Context, config pgconn.Config, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error {
conn, err := utils.ConnectRemotePostgres(ctx, config, options...)
conn, err := utils.ConnectByConfig(ctx, config, options...)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/inspect/index_sizes/index_sizes.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type Result struct {
}

func Run(ctx context.Context, config pgconn.Config, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error {
conn, err := utils.ConnectRemotePostgres(ctx, config, options...)
conn, err := utils.ConnectByConfig(ctx, config, options...)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/inspect/index_usage/index_usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type Result struct {
}

func Run(ctx context.Context, config pgconn.Config, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error {
conn, err := utils.ConnectRemotePostgres(ctx, config, options...)
conn, err := utils.ConnectByConfig(ctx, config, options...)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/inspect/locks/locks.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type Result struct {
}

func Run(ctx context.Context, config pgconn.Config, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error {
conn, err := utils.ConnectRemotePostgres(ctx, config, options...)
conn, err := utils.ConnectByConfig(ctx, config, options...)
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type Result struct {
}

func Run(ctx context.Context, config pgconn.Config, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error {
conn, err := utils.ConnectRemotePostgres(ctx, config, options...)
conn, err := utils.ConnectByConfig(ctx, config, options...)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/inspect/outliers/outliers.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type Result struct {
}

func Run(ctx context.Context, config pgconn.Config, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error {
conn, err := utils.ConnectRemotePostgres(ctx, config, options...)
conn, err := utils.ConnectByConfig(ctx, config, options...)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/inspect/replication_slots/replication_slots.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type Result struct {
}

func Run(ctx context.Context, config pgconn.Config, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error {
conn, err := utils.ConnectRemotePostgres(ctx, config, options...)
conn, err := utils.ConnectByConfig(ctx, config, options...)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/inspect/role_connections/role_connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type Result struct {
}

func Run(ctx context.Context, config pgconn.Config, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error {
conn, err := utils.ConnectRemotePostgres(ctx, config, options...)
conn, err := utils.ConnectByConfig(ctx, config, options...)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/inspect/seq_scans/seq_scans.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Result struct {
}

func Run(ctx context.Context, config pgconn.Config, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error {
conn, err := utils.ConnectRemotePostgres(ctx, config, options...)
conn, err := utils.ConnectByConfig(ctx, config, options...)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/inspect/table_index_sizes/table_index_sizes.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type Result struct {
}

func Run(ctx context.Context, config pgconn.Config, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error {
conn, err := utils.ConnectRemotePostgres(ctx, config, options...)
conn, err := utils.ConnectByConfig(ctx, config, options...)
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type Result struct {
}

func Run(ctx context.Context, config pgconn.Config, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error {
conn, err := utils.ConnectRemotePostgres(ctx, config, options...)
conn, err := utils.ConnectByConfig(ctx, config, options...)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/inspect/table_sizes/table_sizes.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type Result struct {
}

func Run(ctx context.Context, config pgconn.Config, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error {
conn, err := utils.ConnectRemotePostgres(ctx, config, options...)
conn, err := utils.ConnectByConfig(ctx, config, options...)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/inspect/total_index_size/total_index_size.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type Result struct {
}

func Run(ctx context.Context, config pgconn.Config, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error {
conn, err := utils.ConnectRemotePostgres(ctx, config, options...)
conn, err := utils.ConnectByConfig(ctx, config, options...)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/inspect/total_table_sizes/total_table_sizes.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type Result struct {
}

func Run(ctx context.Context, config pgconn.Config, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error {
conn, err := utils.ConnectRemotePostgres(ctx, config, options...)
conn, err := utils.ConnectByConfig(ctx, config, options...)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/inspect/unused_indexes/unused_indexes.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type Result struct {
}

func Run(ctx context.Context, config pgconn.Config, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error {
conn, err := utils.ConnectRemotePostgres(ctx, config, options...)
conn, err := utils.ConnectByConfig(ctx, config, options...)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/inspect/vacuum_stats/vacuum_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ type Result struct {
}

func Run(ctx context.Context, config pgconn.Config, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error {
conn, err := utils.ConnectRemotePostgres(ctx, config, options...)
conn, err := utils.ConnectByConfig(ctx, config, options...)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/migration/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func Run(ctx context.Context, config pgconn.Config, fsys afero.Fs, options ...fu
}

func loadRemoteVersions(ctx context.Context, config pgconn.Config, options ...func(*pgx.ConnConfig)) ([]string, error) {
conn, err := utils.ConnectRemotePostgres(ctx, config, options...)
conn, err := utils.ConnectByConfig(ctx, config, options...)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/migration/repair/repair.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func Run(ctx context.Context, config pgconn.Config, version, status string, fsys
if _, err := strconv.Atoi(version); err != nil {
return ErrInvalidVersion
}
conn, err := utils.ConnectRemotePostgres(ctx, config, options...)
conn, err := utils.ConnectByConfig(ctx, config, options...)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions internal/migration/squash/squash.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func Run(ctx context.Context, version string, config pgconn.Config, fsys afero.F
return err
}
// 2. Update migration history
if len(config.Host) == 0 || !utils.PromptYesNo("Update remote migration history table?", true, os.Stdin) {
if utils.IsLoopback(config.Host) || !utils.PromptYesNo("Update remote migration history table?", true, os.Stdin) {
return nil
}
return baselineMigrations(ctx, config, version, fsys, options...)
Expand Down Expand Up @@ -110,7 +110,7 @@ func baselineMigrations(ctx context.Context, config pgconn.Config, version strin
}
}
fmt.Fprintln(os.Stderr, "Baselining migration history to", version)
conn, err := utils.ConnectRemotePostgres(ctx, config, options...)
conn, err := utils.ConnectByConfig(ctx, config, options...)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions internal/migration/squash/squash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
)

var dbConfig = pgconn.Config{
Host: "127.0.0.1",
Host: "db.supabase.co",
Port: 5432,
User: "admin",
Password: "password",
Expand Down Expand Up @@ -72,7 +72,7 @@ func TestSquashCommand(t *testing.T) {
Query(repair.INSERT_MIGRATION_VERSION, "1", "target", "{}").
Reply("INSERT 1")
// Run test
err := Run(context.Background(), "", pgconn.Config{}, fsys, conn.Intercept)
err := Run(context.Background(), "", pgconn.Config{Host: "127.0.0.1"}, fsys, conn.Intercept)
// Check error
assert.NoError(t, err)
assert.Empty(t, apitest.ListUnmatchedRequests())
Expand Down
4 changes: 2 additions & 2 deletions internal/migration/up/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ var (
errMissingLocal = errors.New("Remote migration versions not found in " + utils.MigrationsDir + " directory.")
)

func Run(ctx context.Context, includeAll bool, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error {
func Run(ctx context.Context, includeAll bool, config pgconn.Config, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error {
if err := utils.LoadConfigFS(fsys); err != nil {
return err
}
conn, err := utils.ConnectLocalPostgres(ctx, pgconn.Config{}, options...)
conn, err := utils.ConnectByConfig(ctx, config, options...)
if err != nil {
return err
}
Expand Down
Loading