forked from mtusi2026/mtusi2026lab3
feat(app) lab 3 implemented
This commit is contained in:
@@ -1,6 +1,3 @@
|
||||
// AGENT: golang-migrate now runs the embedded SQL files instead of the previous
|
||||
// hand-written runner. The legacy table upgrade below keeps existing lab 2
|
||||
// volumes usable, so switching engines does not require deleting data.
|
||||
package database
|
||||
|
||||
import (
|
||||
@@ -22,16 +19,10 @@ import (
|
||||
var migrationFiles embed.FS
|
||||
|
||||
const (
|
||||
// The old in-house runner used this advisory lock. We keep the same key for
|
||||
// the one-time table upgrade so an upgrade cannot race a still-running old
|
||||
// instance that is applying the baseline migration.
|
||||
legacyMigrationLockID int64 = 728_2026
|
||||
migrationsTable = "schema_migrations"
|
||||
)
|
||||
|
||||
// Migrate applies the embedded golang-migrate migrations. golang-migrate takes
|
||||
// its own PostgreSQL advisory lock, records the current version and dirty flag
|
||||
// in schema_migrations, and wraps each migration file in one transaction.
|
||||
func Migrate(ctx context.Context, pool *pgxpool.Pool, databaseURL string) error {
|
||||
if err := upgradeLegacyMigrationsTable(ctx, pool); err != nil {
|
||||
return err
|
||||
@@ -47,8 +38,6 @@ func Migrate(ctx context.Context, pool *pgxpool.Pool, databaseURL string) error
|
||||
_ = source.Close()
|
||||
return fmt.Errorf("parse DATABASE_URL for migrations: %w", err)
|
||||
}
|
||||
// golang-migrate acquires its lock with context.Background(). A PostgreSQL
|
||||
// lock timeout keeps startup bounded if another instance never releases it.
|
||||
if connConfig.RuntimeParams == nil {
|
||||
connConfig.RuntimeParams = make(map[string]string)
|
||||
}
|
||||
@@ -89,7 +78,7 @@ func Migrate(ctx context.Context, pool *pgxpool.Pool, databaseURL string) error
|
||||
|
||||
// upgradeLegacyMigrationsTable replaces the table written by the previous
|
||||
// in-house runner (version text, applied_at) with the golang-migrate schema
|
||||
// (version bigint, dirty boolean). The schema itself is left untouched and the
|
||||
// (version bigint, dirty boolean). The data schema itself is left untouched and the
|
||||
// highest applied version is carried over, so a preserved volume does not need
|
||||
// to be recreated. The migration DDL is idempotent, but preserving the version
|
||||
// keeps the migration history truthful.
|
||||
@@ -132,7 +121,11 @@ func upgradeLegacyMigrationsTable(ctx context.Context, pool *pgxpool.Pool) error
|
||||
if err != nil {
|
||||
return fmt.Errorf("begin migration table upgrade: %w", err)
|
||||
}
|
||||
defer func() { _ = tx.Rollback(context.Background()) }()
|
||||
defer func() {
|
||||
rollbackCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
_ = tx.Rollback(rollbackCtx)
|
||||
}()
|
||||
|
||||
const upgrade = `
|
||||
DROP TABLE IF EXISTS schema_migrations_migrate_tmp;
|
||||
|
||||
Reference in New Issue
Block a user