@@ -74,15 +74,17 @@ func runMigrations(migrationType string, migrationsDir string, cfg *config.Confi
7474 return fmt .Errorf ("failed to get migration status: %v" , err )
7575 }
7676 case "fresh" , "fresh-seed-full" , "fresh-seed-basic" :
77-
78- // Close the current connection because it connects to the target database.
77+ // Close the current connection to the db
7978 err := sqlDB .Close ()
8079 if err != nil {
8180 return err
8281 }
8382
84- // Connect to a maintenance database (like "postgres").
85- mDB , err := database .ConnectToPostgres (cfg , dbLogger )
83+ // Connect to the 'postgres' maintenance database to drop/recreate the target db
84+ maintenanceCfg := * cfg
85+ maintenanceCfg .Postgres .Database = "postgres"
86+
87+ mDB , err := database .ConnectToPostgres (& maintenanceCfg , dbLogger )
8688 if err != nil {
8789 return fmt .Errorf ("failed to connect to maintenance database: %v" , err )
8890 }
@@ -91,16 +93,18 @@ func runMigrations(migrationType string, migrationsDir string, cfg *config.Confi
9193 return fmt .Errorf ("failed to get raw SQL DB for maintenance: %v" , err )
9294 }
9395
94- // Drop all tables explicitly.
96+ // Drop and recreate the target db
9597 if err := database .DropAndRecreateDatabase (mSqlDB , cfg ); err != nil {
9698 return fmt .Errorf ("failed to recreate database: %v" , err )
9799 }
100+
101+ // Close maintenance connection
98102 err = mSqlDB .Close ()
99103 if err != nil {
100104 return err
101- } // Close the maintenance connection.
105+ }
102106
103- // Reconnect to the newly created target database.
107+ // Reconnect to the freshly created target db
104108 gormDB , err = database .ConnectToPostgres (cfg , dbLogger )
105109 if err != nil {
106110 return fmt .Errorf ("failed to reconnect to Postgres: %v" , err )
@@ -110,11 +114,12 @@ func runMigrations(migrationType string, migrationsDir string, cfg *config.Confi
110114 return fmt .Errorf ("failed to get raw SQL DB after reconnection: %v" , err )
111115 }
112116
113- // Then, run migrations.
117+ // Run migrations
114118 if err := goose .Up (sqlDB , migrationsDir ); err != nil {
115119 return fmt .Errorf ("failed to apply fresh migrations: %v" , err )
116120 }
117- // If seeding is required, run the seeder.
121+
122+ // Seed if needed
118123 if migrationType == "fresh-seed-full" || migrationType == "fresh-seed-basic" {
119124 seedType := "full"
120125 if migrationType == "fresh-seed-basic" {
0 commit comments