Skip to content

Commit e18790d

Browse files
committed
fix: use pgkit migra with experimental flag
1 parent aac516b commit e18790d

File tree

5 files changed

+41
-8
lines changed

5 files changed

+41
-8
lines changed

cmd/db.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ var (
101101
if usePgSchema {
102102
differ = diff.DiffPgSchema
103103
fmt.Fprintln(os.Stderr, utils.Yellow("WARNING:"), "--use-pg-schema flag is experimental and may not include all entities, such as views and grants.")
104+
} else if !viper.GetBool("EXPERIMENTAL") {
105+
differ = diff.DiffSchemaMigraBash
104106
}
105107
return diff.Run(cmd.Context(), schema, file, flags.DbConfig, differ, afero.NewOsFs())
106108
},

internal/db/diff/diff.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828
"github.com/supabase/cli/pkg/parser"
2929
)
3030

31-
type DiffFunc func(context.Context, string, string, []string) (string, error)
31+
type DiffFunc func(context.Context, string, string, []string, ...func(*pgx.ConnConfig)) (string, error)
3232

3333
func Run(ctx context.Context, schema []string, file string, config pgconn.Config, differ DiffFunc, fsys afero.Fs, options ...func(*pgx.ConnConfig)) (err error) {
3434
out, err := DiffDatabase(ctx, schema, config, os.Stderr, fsys, differ, options...)
@@ -136,7 +136,7 @@ func MigrateShadowDatabase(ctx context.Context, container string, fsys afero.Fs,
136136
return migration.ApplyMigrations(ctx, migrations, conn, afero.NewIOFS(fsys))
137137
}
138138

139-
func DiffDatabase(ctx context.Context, schema []string, config pgconn.Config, w io.Writer, fsys afero.Fs, differ func(context.Context, string, string, []string) (string, error), options ...func(*pgx.ConnConfig)) (string, error) {
139+
func DiffDatabase(ctx context.Context, schema []string, config pgconn.Config, w io.Writer, fsys afero.Fs, differ DiffFunc, options ...func(*pgx.ConnConfig)) (string, error) {
140140
fmt.Fprintln(w, "Creating shadow database...")
141141
shadow, err := CreateShadowDatabase(ctx, utils.Config.Db.ShadowPort)
142142
if err != nil {
@@ -175,7 +175,7 @@ func DiffDatabase(ctx context.Context, schema []string, config pgconn.Config, w
175175
}
176176
source := utils.ToPostgresURL(shadowConfig)
177177
target := utils.ToPostgresURL(config)
178-
return differ(ctx, source, target, schema)
178+
return differ(ctx, source, target, schema, options...)
179179
}
180180

181181
func migrateBaseDatabase(ctx context.Context, config pgconn.Config, migrations []string, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error {

internal/db/diff/diff_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/h2non/gock"
1616
"github.com/jackc/pgconn"
1717
"github.com/jackc/pgerrcode"
18+
"github.com/jackc/pgx/v4"
1819
"github.com/spf13/afero"
1920
"github.com/stretchr/testify/assert"
2021
"github.com/stretchr/testify/require"
@@ -72,7 +73,17 @@ func TestRun(t *testing.T) {
7273
Reply("CREATE DATABASE")
7374
defer conn.Close(t)
7475
// Run test
75-
err := Run(context.Background(), []string{"public"}, "file", dbConfig, DiffSchemaMigra, fsys, conn.Intercept)
76+
err := Run(context.Background(), []string{"public"}, "file", dbConfig, DiffSchemaMigra, fsys, func(cc *pgx.ConnConfig) {
77+
if cc.Host == dbConfig.Host {
78+
// Fake a SSL error when connecting to target database
79+
cc.LookupFunc = func(ctx context.Context, host string) (addrs []string, err error) {
80+
return nil, errors.New("server refused TLS connection")
81+
}
82+
} else {
83+
// Hijack connection to shadow database
84+
conn.Intercept(cc)
85+
}
86+
})
7687
// Check error
7788
assert.NoError(t, err)
7889
assert.Empty(t, apitest.ListUnmatchedRequests())

internal/db/diff/migra.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ import (
99
"github.com/docker/docker/api/types/container"
1010
"github.com/docker/docker/api/types/network"
1111
"github.com/go-errors/errors"
12+
"github.com/jackc/pgx/v4"
1213
"github.com/spf13/viper"
1314
"github.com/supabase/cli/internal/gen/types"
1415
"github.com/supabase/cli/internal/utils"
1516
"github.com/supabase/cli/pkg/config"
17+
"github.com/supabase/cli/pkg/migration"
1618
)
1719

1820
var (
@@ -60,7 +62,14 @@ var (
6062
)
6163

6264
// Diffs local database schema against shadow, dumps output to stdout.
63-
func DiffSchemaMigraBash(ctx context.Context, source, target string, schema []string) (string, error) {
65+
func DiffSchemaMigraBash(ctx context.Context, source, target string, schema []string, options ...func(*pgx.ConnConfig)) (string, error) {
66+
// Load all user defined schemas
67+
if len(schema) == 0 {
68+
var err error
69+
if schema, err = loadSchema(ctx, target, options...); err != nil {
70+
return "", err
71+
}
72+
}
6473
env := []string{"SOURCE=" + source, "TARGET=" + target}
6574
// Passing in script string means command line args must be set manually, ie. "$@"
6675
args := "set -- " + strings.Join(schema, " ") + ";"
@@ -86,10 +95,20 @@ func DiffSchemaMigraBash(ctx context.Context, source, target string, schema []st
8695
return out.String(), nil
8796
}
8897

89-
func DiffSchemaMigra(ctx context.Context, source, target string, schema []string) (string, error) {
98+
func loadSchema(ctx context.Context, dbURL string, options ...func(*pgx.ConnConfig)) ([]string, error) {
99+
conn, err := utils.ConnectByUrl(ctx, dbURL, options...)
100+
if err != nil {
101+
return nil, err
102+
}
103+
defer conn.Close(context.Background())
104+
// RLS policies in auth and storage schemas can be included with -s flag
105+
return migration.ListUserSchemas(ctx, conn)
106+
}
107+
108+
func DiffSchemaMigra(ctx context.Context, source, target string, schema []string, options ...func(*pgx.ConnConfig)) (string, error) {
90109
env := []string{"SOURCE=" + source, "TARGET=" + target}
91110
// node-postgres does not support sslmode=prefer
92-
if require, err := types.IsRequireSSL(ctx, target); err != nil {
111+
if require, err := types.IsRequireSSL(ctx, target, options...); err != nil {
93112
return "", err
94113
} else if require {
95114
rootCA := caStaging + caProd

internal/db/diff/pgschema.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import (
77
"strings"
88

99
"github.com/go-errors/errors"
10+
"github.com/jackc/pgx/v4"
1011
pgschema "github.com/stripe/pg-schema-diff/pkg/diff"
1112
)
1213

13-
func DiffPgSchema(ctx context.Context, source, target string, schema []string) (string, error) {
14+
func DiffPgSchema(ctx context.Context, source, target string, schema []string, _ ...func(*pgx.ConnConfig)) (string, error) {
1415
dbSrc, err := sql.Open("pgx", source)
1516
if err != nil {
1617
return "", errors.Errorf("failed to open source database: %w", err)

0 commit comments

Comments
 (0)