[management] Clone test databases from a migrated template

Profiling the management unit suite per engine showed that nearly every
test in management/server spends 1.0 to 1.7 seconds creating its store
on MySQL and about 0.9 seconds on Postgres, against 0.3 seconds on
sqlite. The work is CREATE DATABASE followed by 26 pre-migrations, a
40-table AutoMigrate and 9 post-migrations, repeated for each of the
package's 133 store creation sites. That put the package at 10 minutes
of its 20 minute go test budget on a healthy runner, and over it on a
slow one.

Run the full migration once per process into a template database and
clone it for every test database. Postgres copies it server-side with
CREATE DATABASE ... TEMPLATE. MySQL has no equivalent, so the template's
SHOW CREATE TABLE statements are replayed into the new database with
foreign key checks off, which keeps the ON DELETE CASCADE constraints
that CREATE TABLE ... LIKE would drop. The per-test store then opens
with migrations skipped. Every test still gets its own database, so
isolation is unchanged.
This commit is contained in:
mlsmaycon
2026-09-12 12:00:37 +00:00
parent ba9b226b4b
commit e9b9a154c1
2 changed files with 226 additions and 13 deletions
+10 -2
View File
@@ -3213,7 +3213,11 @@ func NewSqliteStoreFromFileStore(ctx context.Context, fileStore *FileStore, data
// NewPostgresqlStoreFromSqlStore restores a store from SqlStore and stores Postgres DB.
func NewPostgresqlStoreFromSqlStore(ctx context.Context, sqliteStore *SqlStore, dsn string, metrics telemetry.AppMetrics) (*SqlStore, error) {
store, err := NewPostgresqlStoreForTests(ctx, dsn, metrics, false)
return newPostgresqlStoreFromSqlStore(ctx, sqliteStore, dsn, metrics, false)
}
func newPostgresqlStoreFromSqlStore(ctx context.Context, sqliteStore *SqlStore, dsn string, metrics telemetry.AppMetrics, skipMigration bool) (*SqlStore, error) {
store, err := NewPostgresqlStoreForTests(ctx, dsn, metrics, skipMigration)
if err != nil {
return nil, err
}
@@ -3279,7 +3283,11 @@ func connectToPgDbForTests(ctx context.Context, dsn string) (*pgxpool.Pool, erro
// NewMysqlStoreFromSqlStore restores a store from SqlStore and stores MySQL DB.
func NewMysqlStoreFromSqlStore(ctx context.Context, sqliteStore *SqlStore, dsn string, metrics telemetry.AppMetrics) (*SqlStore, error) {
store, err := NewMysqlStore(ctx, dsn, metrics, false)
return newMysqlStoreFromSqlStore(ctx, sqliteStore, dsn, metrics, false)
}
func newMysqlStoreFromSqlStore(ctx context.Context, sqliteStore *SqlStore, dsn string, metrics telemetry.AppMetrics, skipMigration bool) (*SqlStore, error) {
store, err := NewMysqlStore(ctx, dsn, metrics, skipMigration)
if err != nil {
return nil, err
}