Migration round trip (family idea "Migration CI lane"): - TestMigrationsRoundTrip, in the integration lane, migrates a scratch database to the baseline, then all the way up, then every migration after the baseline down, then up again. - The schema after the down pass must match a database migrated to the baseline alone: columns, indexes, constraints, sequences, enums, functions and triggers. So a down file that leaves something behind fails, as well as one that errors. - The baseline's own down file refuses to run by design, so the round trip stops there. - The test uses golang-migrate as a library, with no migrator binary. - The scratch database is dropped afterwards, and the shared test database is never migrated down. dbtest (family idea "dbtest Pool and Fixtures"): - dbtest.URL skips without a database. It fails instead when MINSTREL_TEST_DATABASE_REQUIRED is set, and the integration lane sets it. - dbtest.On(t, db) gives One/Exec/Count fixtures. The local copies are repointed to them: exec in api, mustExec/mustID in backup, countQueueRows in scrobble, and countTrackSim/countArtistSim in similarity. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+18
-11
@@ -37,18 +37,9 @@ func Open(ctx context.Context, dsn string) (*pgxpool.Pool, error) {
|
||||
// to call concurrently with application traffic only if the database is idle;
|
||||
// in practice this runs once at startup before the HTTP server binds.
|
||||
func Migrate(dsn string, logger *slog.Logger) error {
|
||||
if dsn == "" {
|
||||
return errors.New("db: database URL is empty")
|
||||
}
|
||||
|
||||
src, err := iofs.New(migrationsFS, "migrations")
|
||||
m, err := newMigrator(dsn)
|
||||
if err != nil {
|
||||
return fmt.Errorf("db: iofs source: %w", err)
|
||||
}
|
||||
|
||||
m, err := migrate.NewWithSourceInstance("iofs", src, "pgx5://"+trimScheme(dsn))
|
||||
if err != nil {
|
||||
return fmt.Errorf("db: migrate init: %w", err)
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
if srcErr, dbErr := m.Close(); srcErr != nil || dbErr != nil {
|
||||
@@ -67,6 +58,22 @@ func Migrate(dsn string, logger *slog.Logger) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// newMigrator opens the embedded migrations against dsn. The caller closes it.
|
||||
func newMigrator(dsn string) (*migrate.Migrate, error) {
|
||||
if dsn == "" {
|
||||
return nil, errors.New("db: database URL is empty")
|
||||
}
|
||||
src, err := iofs.New(migrationsFS, "migrations")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("db: iofs source: %w", err)
|
||||
}
|
||||
m, err := migrate.NewWithSourceInstance("iofs", src, "pgx5://"+trimScheme(dsn))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("db: migrate init: %w", err)
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// trimScheme rewrites a postgres://... or postgresql://... DSN to the bare
|
||||
// form expected by the migrate pgx driver (it registers its own scheme).
|
||||
func trimScheme(dsn string) string {
|
||||
|
||||
Reference in New Issue
Block a user