7373 return config
7474}
7575
76- var noBackupVolume bool = true
77-
7876func StartDatabase (ctx context.Context , fsys afero.Fs , w io.Writer , options ... func (* pgx.ConnConfig )) error {
7977 config := NewContainerConfig ()
8078 hostPort := strconv .FormatUint (uint64 (utils .Config .Db .Port ), 10 )
@@ -93,7 +91,7 @@ func StartDatabase(ctx context.Context, fsys afero.Fs, w io.Writer, options ...f
9391 }
9492 // Creating volume will not override existing volume, so we must inspect explicitly
9593 _ , err := utils .Docker .VolumeInspect (ctx , utils .DbId )
96- noBackupVolume = client .IsErrNotFound (err )
94+ noBackupVolume : = client .IsErrNotFound (err )
9795 if noBackupVolume {
9896 fmt .Fprintln (w , "Starting database..." )
9997 } else {
@@ -107,7 +105,7 @@ func StartDatabase(ctx context.Context, fsys afero.Fs, w io.Writer, options ...f
107105 }
108106 // Initialise if we are on PG14 and there's no existing db volume
109107 if noBackupVolume {
110- if err := InitDatabase (ctx , utils . DbId , w , options ... ); err != nil {
108+ if err := setupDatabase (ctx , fsys , w , options ... ); err != nil {
111109 return err
112110 }
113111 }
@@ -137,18 +135,12 @@ func initCurrentBranch(fsys afero.Fs) error {
137135 return afero .WriteFile (fsys , utils .CurrBranchPath , []byte ("main" ), 0644 )
138136}
139137
140- func InitDatabase (ctx context.Context , host string , w io.Writer , options ... func (* pgx.ConnConfig )) error {
141- // Initialise globals
142- conn , err := utils .ConnectLocalPostgres (ctx , pgconn.Config {}, options ... )
143- if err != nil {
144- return err
145- }
146- defer conn .Close (context .Background ())
138+ func initSchema (ctx context.Context , conn * pgx.Conn , host string , w io.Writer ) error {
147139 fmt .Fprintln (w , "Setting up initial schema..." )
148140 if utils .Config .Db .MajorVersion <= 14 {
149141 return initSchema14 (ctx , conn )
150142 }
151- return initSchema15 (ctx , conn , host )
143+ return initSchema15 (ctx , host )
152144}
153145
154146func initSchema14 (ctx context.Context , conn * pgx.Conn ) error {
@@ -158,7 +150,7 @@ func initSchema14(ctx context.Context, conn *pgx.Conn) error {
158150 return apply .BatchExecDDL (ctx , conn , strings .NewReader (utils .InitialSchemaSql ))
159151}
160152
161- func initSchema15 (ctx context.Context , conn * pgx. Conn , host string ) error {
153+ func initSchema15 (ctx context.Context , host string ) error {
162154 // Apply service migrations
163155 if err := utils .DockerRunOnceWithStream (ctx , utils .StorageImage , []string {
164156 "ANON_KEY=" + utils .Config .Auth .AnonKey ,
@@ -183,26 +175,22 @@ func initSchema15(ctx context.Context, conn *pgx.Conn, host string) error {
183175 }, []string {"gotrue" , "migrate" }, io .Discard , os .Stderr )
184176}
185177
186- func SetupDatabase (ctx context.Context , dbConfig pgconn.Config , fsys afero.Fs , w io.Writer , options ... func (* pgx.ConnConfig )) error {
187- if ! noBackupVolume {
188- return nil
189- }
190- if dbConfig .Host != utils .DbId {
191- return nil
192- }
193- // conn, err := utils.ConnectLocalPostgres(ctx, dbConfig, options...)
178+ func setupDatabase (ctx context.Context , fsys afero.Fs , w io.Writer , options ... func (* pgx.ConnConfig )) error {
194179 conn , err := utils .ConnectLocalPostgres (ctx , pgconn.Config {}, options ... )
195180 if err != nil {
196181 return err
197182 }
198183 defer conn .Close (context .Background ())
199- if err := CreateCustomRoles (ctx , conn , w , fsys ); err != nil {
184+ if err := SetupDatabase (ctx , conn , utils . DbId , w , fsys ); err != nil {
200185 return err
201186 }
202187 return reset .InitialiseDatabase (ctx , conn , fsys )
203188}
204189
205- func CreateCustomRoles (ctx context.Context , conn * pgx.Conn , w io.Writer , fsys afero.Fs ) error {
190+ func SetupDatabase (ctx context.Context , conn * pgx.Conn , host string , w io.Writer , fsys afero.Fs ) error {
191+ if err := initSchema (ctx , conn , host , w ); err != nil {
192+ return err
193+ }
206194 roles , err := fsys .Open (utils .CustomRolesPath )
207195 if errors .Is (err , os .ErrNotExist ) {
208196 return nil
0 commit comments