@@ -33,14 +33,16 @@ type DiffFunc func(context.Context, string, string, []string) (string, error)
33
33
func Run (ctx context.Context , schema []string , file string , config pgconn.Config , differ DiffFunc , fsys afero.Fs , options ... func (* pgx.ConnConfig )) (err error ) {
34
34
// Sanity checks.
35
35
if utils .IsLocalDatabase (config ) {
36
- if container , err := createShadowIfNotExists (ctx , fsys ); err != nil {
36
+ if declared , err := loadDeclaredSchemas (fsys ); err != nil {
37
+ return err
38
+ } else if container , err := createShadowIfNotExists (ctx , declared ); err != nil {
37
39
return err
38
40
} else if len (container ) > 0 {
39
41
defer utils .DockerRemove (container )
40
42
if err := start .WaitForHealthyService (ctx , start .HealthTimeout , container ); err != nil {
41
43
return err
42
44
}
43
- if err := migrateBaseDatabase (ctx , container , fsys , options ... ); err != nil {
45
+ if err := migrateBaseDatabase (ctx , container , declared , fsys , options ... ); err != nil {
44
46
return err
45
47
}
46
48
}
@@ -70,23 +72,31 @@ func Run(ctx context.Context, schema []string, file string, config pgconn.Config
70
72
return nil
71
73
}
72
74
73
- func createShadowIfNotExists (ctx context.Context , fsys afero.Fs ) (string , error ) {
74
- if exists , err := afero .DirExists (fsys , utils .SchemasDir ); err != nil {
75
- return "" , errors .Errorf ("failed to check schemas: %w" , err )
76
- } else if ! exists {
75
+ func createShadowIfNotExists (ctx context.Context , migrations []string ) (string , error ) {
76
+ if len (migrations ) == 0 {
77
77
return "" , nil
78
78
}
79
79
if err := utils .AssertSupabaseDbIsRunning (); ! errors .Is (err , utils .ErrNotRunning ) {
80
80
return "" , err
81
81
}
82
- fmt .Fprintf (os .Stderr , "Creating local database from %s...\n " , utils .Bold (utils .SchemasDir ))
82
+ fmt .Fprintln (os .Stderr , "Creating local database from declarative schemas:" )
83
+ msg := make ([]string , len (migrations ))
84
+ for i , m := range migrations {
85
+ msg [i ] = fmt .Sprintf (" • %s" , utils .Bold (m ))
86
+ }
87
+ fmt .Fprintln (os .Stderr , strings .Join (msg , "\n " ))
83
88
return CreateShadowDatabase (ctx , utils .Config .Db .Port )
84
89
}
85
90
86
91
func loadDeclaredSchemas (fsys afero.Fs ) ([]string , error ) {
87
92
if schemas := utils .Config .Db .Migrations .SchemaPaths ; len (schemas ) > 0 {
88
93
return schemas .Files (afero .NewIOFS (fsys ))
89
94
}
95
+ if exists , err := afero .DirExists (fsys , utils .SchemasDir ); err != nil {
96
+ return nil , errors .Errorf ("failed to check schemas: %w" , err )
97
+ } else if ! exists {
98
+ return nil , nil
99
+ }
90
100
var declared []string
91
101
if err := afero .Walk (fsys , utils .SchemasDir , func (path string , info fs.FileInfo , err error ) error {
92
102
if err != nil {
@@ -170,11 +180,7 @@ func MigrateShadowDatabase(ctx context.Context, container string, fsys afero.Fs,
170
180
return migration .ApplyMigrations (ctx , migrations , conn , afero .NewIOFS (fsys ))
171
181
}
172
182
173
- func migrateBaseDatabase (ctx context.Context , container string , fsys afero.Fs , options ... func (* pgx.ConnConfig )) error {
174
- migrations , err := loadDeclaredSchemas (fsys )
175
- if err != nil {
176
- return err
177
- }
183
+ func migrateBaseDatabase (ctx context.Context , container string , migrations []string , fsys afero.Fs , options ... func (* pgx.ConnConfig )) error {
178
184
conn , err := utils .ConnectLocalPostgres (ctx , pgconn.Config {}, options ... )
179
185
if err != nil {
180
186
return err
0 commit comments