Skip to content

Commit

Permalink
chore: refactor connect by config to identify localhost
Browse files Browse the repository at this point in the history
  • Loading branch information
sweatybridge committed Sep 19, 2023
1 parent d5c3468 commit 56b17c5
Show file tree
Hide file tree
Showing 30 changed files with 49 additions and 62 deletions.
2 changes: 1 addition & 1 deletion cmd/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,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 @@ -33,14 +33,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 != "localhost" {
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 @@ -58,15 +50,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 == "localhost" && 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: 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 != "localhost" {
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, fsys afero.
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/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
2 changes: 1 addition & 1 deletion internal/db/push/push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ 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"}, 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/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 shouldUpdate := utils.PromptYesNo("Update remote migration history table?", true, os.Stdin); !shouldUpdate {
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
10 changes: 8 additions & 2 deletions internal/migration/up/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"os"
"path/filepath"

"github.com/jackc/pgconn"
Expand All @@ -19,11 +20,16 @@ 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...)
if config.Host != "localhost" {
if shouldUp := utils.PromptYesNo("Confirm migrating the remote database?", true, os.Stdin); !shouldUp {
return context.Canceled
}
}
conn, err := utils.ConnectByConfig(ctx, config, options...)
if err != nil {
return err
}
Expand Down
10 changes: 10 additions & 0 deletions internal/utils/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net"
"net/url"
"os"
"strings"
"time"

"github.com/jackc/pgconn"
Expand Down Expand Up @@ -104,3 +105,12 @@ func ConnectByUrl(ctx context.Context, url string, options ...func(*pgx.ConnConf
// Connect to database
return pgx.ConnectConfig(ctx, config)
}

func ConnectByConfig(ctx context.Context, config pgconn.Config, options ...func(*pgx.ConnConfig)) (*pgx.Conn, error) {
if strings.ToLower(config.Host) == "localhost" {
fmt.Fprintln(os.Stderr, "Connecting to local database...")
return ConnectLocalPostgres(ctx, config, options...)
}
fmt.Fprintln(os.Stderr, "Connecting to remote database...")
return ConnectRemotePostgres(ctx, config, options...)
}

0 comments on commit 56b17c5

Please sign in to comment.