@@ -64,25 +64,25 @@ func resetDatabase(ctx context.Context, fsys afero.Fs, options ...func(*pgx.Conn
6464 if err := RecreateDatabase (ctx , options ... ); err != nil {
6565 return err
6666 }
67- defer RestartDatabase (context .Background (), os .Stderr )
68- return initDatabase (ctx , fsys , options ... )
69- }
70-
71- func initDatabase (ctx context.Context , fsys afero.Fs , options ... func (* pgx.ConnConfig )) error {
72- url := fmt .Sprintf ("postgresql://supabase_admin:postgres@localhost:%d/postgres?connect_timeout=2" , utils .Config .Db .Port )
73- conn , err := utils .ConnectByUrl (ctx , url , options ... )
74- if err != nil {
67+ if err := initDatabase (ctx , options ... ); err != nil {
7568 return err
7669 }
77- defer conn . Close (context .Background ())
78- fmt . Fprintln ( os . Stderr , "Initializing schema ..." )
79- if err := apply . BatchExecDDL ( ctx , conn , strings . NewReader ( utils . InitialSchemaSql )); err != nil {
70+ RestartDatabase (context .Background (), os . Stderr )
71+ conn , err := utils . ConnectLocalPostgres ( ctx , pgconn. Config {}, options ... )
72+ if err != nil {
8073 return err
8174 }
82- if _ , err := conn .Exec (ctx , SET_POSTGRES_ROLE ); err != nil {
75+ defer conn .Close (context .Background ())
76+ return InitialiseDatabase (ctx , conn , fsys )
77+ }
78+
79+ func initDatabase (ctx context.Context , options ... func (* pgx.ConnConfig )) error {
80+ conn , err := utils .ConnectLocalPostgres (ctx , pgconn.Config {User : "supabase_admin" }, options ... )
81+ if err != nil {
8382 return err
8483 }
85- return InitialiseDatabase (ctx , conn , fsys )
84+ defer conn .Close (context .Background ())
85+ return apply .BatchExecDDL (ctx , conn , strings .NewReader (utils .InitialSchemaSql ))
8686}
8787
8888func InitialiseDatabase (ctx context.Context , conn * pgx.Conn , fsys afero.Fs ) error {
@@ -94,21 +94,22 @@ func InitialiseDatabase(ctx context.Context, conn *pgx.Conn, fsys afero.Fs) erro
9494
9595// Recreate postgres database by connecting to template1
9696func RecreateDatabase (ctx context.Context , options ... func (* pgx.ConnConfig )) error {
97- url := fmt .Sprintf ("postgresql://supabase_admin:postgres@localhost:%d/template1?connect_timeout=2" , utils .Config .Db .Port )
98- conn , err := utils .ConnectByUrl (ctx , url , options ... )
97+ conn , err := utils .ConnectLocalPostgres (ctx , pgconn.Config {User : "supabase_admin" , Database : "template1" }, options ... )
9998 if err != nil {
10099 return err
101100 }
102101 defer conn .Close (context .Background ())
103102 if err := DisconnectClients (ctx , conn ); err != nil {
104103 return err
105104 }
106- drop := "DROP DATABASE IF EXISTS postgres WITH (FORCE);"
107- if _ , err := conn .Exec (ctx , drop ); err != nil {
108- return err
105+ // We are not dropping roles here because they are cluster level entities. Use stop && start instead.
106+ sql := repair.MigrationFile {
107+ Lines : []string {
108+ "DROP DATABASE IF EXISTS postgres WITH (FORCE)" ,
109+ "CREATE DATABASE postgres WITH OWNER postgres" ,
110+ },
109111 }
110- _ , err = conn .Exec (ctx , "CREATE DATABASE postgres WITH OWNER postgres;" )
111- return err
112+ return sql .ExecBatch (ctx , conn )
112113}
113114
114115func SeedDatabase (ctx context.Context , conn * pgx.Conn , fsys afero.Fs ) error {
@@ -133,10 +134,8 @@ func DisconnectClients(ctx context.Context, conn *pgx.Conn) error {
133134 }
134135 }
135136 term := fmt .Sprintf (utils .TerminateDbSqlFmt , "postgres" )
136- if _ , err := conn .Exec (ctx , term ); err != nil {
137- return err
138- }
139- return nil
137+ _ , err := conn .Exec (ctx , term )
138+ return err
140139}
141140
142141func RestartDatabase (ctx context.Context , w io.Writer ) {
@@ -229,7 +228,7 @@ func ListSchemas(ctx context.Context, conn *pgx.Conn, exclude ...string) ([]stri
229228 }
230229 schemas = append (schemas , name )
231230 }
232- return schemas , nil
231+ return schemas , rows . Err ()
233232}
234233
235234func likeEscapeSchema (schemas []string ) (result []string ) {
0 commit comments