@@ -9,9 +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"
14+ "github.com/supabase/cli/internal/gen/types"
1315 "github.com/supabase/cli/internal/utils"
1416 "github.com/supabase/cli/pkg/config"
17+ "github.com/supabase/cli/pkg/migration"
1518)
1619
1720var (
2023 //go:embed templates/migra.ts
2124 diffSchemaTypeScript string
2225
26+ //go:embed templates/staging-ca-2021.crt
27+ caStaging string
28+ //go:embed templates/prod-ca-2021.crt
29+ caProd string
30+
2331 managedSchemas = []string {
2432 // Local development
2533 "_analytics" ,
5462)
5563
5664// Diffs local database schema against shadow, dumps output to stdout.
57- 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+ }
5873 env := []string {"SOURCE=" + source , "TARGET=" + target }
5974 // Passing in script string means command line args must be set manually, ie. "$@"
6075 args := "set -- " + strings .Join (schema , " " ) + ";"
@@ -80,8 +95,25 @@ func DiffSchemaMigraBash(ctx context.Context, source, target string, schema []st
8095 return out .String (), nil
8196}
8297
83- 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 ) {
84109 env := []string {"SOURCE=" + source , "TARGET=" + target }
110+ // node-postgres does not support sslmode=prefer
111+ if require , err := types .IsRequireSSL (ctx , target , options ... ); err != nil {
112+ return "" , err
113+ } else if require {
114+ rootCA := caStaging + caProd
115+ env = append (env , "SSL_CA=" + rootCA )
116+ }
85117 if len (schema ) > 0 {
86118 env = append (env , "INCLUDED_SCHEMAS=" + strings .Join (schema , "," ))
87119 } else {
0 commit comments