[management] Close the gorm handle when a test store fails to open

The Postgres and MySQL constructors left the gorm connection open when the
migration failed, so the template database still had a session and its drop
was refused. The drop now also retries while Postgres reports the database
as in use, and logs a warning instead of a debug line when it gives up.
This commit is contained in:
mlsmaycon
2026-09-12 12:42:31 +00:00
parent 889ee8ef92
commit 6210e50f25
2 changed files with 14 additions and 3 deletions
+9 -1
View File
@@ -3154,7 +3154,12 @@ func NewMysqlStore(ctx context.Context, dsn string, metrics telemetry.AppMetrics
return nil, err
}
return NewSqlStore(ctx, db, types.MysqlStoreEngine, metrics, skipMigration)
store, err := NewSqlStore(ctx, db, types.MysqlStoreEngine, metrics, skipMigration)
if err != nil {
closeGormDB(db)
return nil, err
}
return store, nil
}
func getGormConfig() *gorm.Config {
@@ -3238,11 +3243,14 @@ func NewPostgresqlStoreForTests(ctx context.Context, dsn string, metrics telemet
}
pool, err := connectToPgDbForTests(context.Background(), dsn)
if err != nil {
closeGormDB(db)
return nil, err
}
store, err := NewSqlStore(ctx, db, types.PostgresStoreEngine, metrics, skipMigration)
if err != nil {
// Release the sessions, or the caller cannot drop the database.
pool.Close()
closeGormDB(db)
return nil, err
}
store.pool = pool
+5 -2
View File
@@ -1063,9 +1063,12 @@ func cloneMysqlSchema(ctx context.Context, dsn string, tableDDL []string) error
// dropDatabase removes a template that never became usable, so a failed setup
// does not leave it behind on a shared server.
// dropDatabase removes a template database that failed to migrate. The server
// may still be tearing down the sessions the failed migration held, so the
// drop retries while Postgres reports the database as in use.
func dropDatabase(admin *gorm.DB, name string) {
if err := admin.Exec(fmt.Sprintf("DROP DATABASE IF EXISTS %s", name)).Error; err != nil {
log.Debugf("failed to drop template database %s: %v", name, err)
if err := execWithTemplateRetry(admin, fmt.Sprintf("DROP DATABASE IF EXISTS %s", name)); err != nil {
log.Warnf("failed to drop template database %s: %v", name, err)
}
}