forked from mtusi2026/mtusi2026lab2
refactor(app) AI refactoring before moving to Lab3 done
This commit is contained in:
@@ -1,23 +1,14 @@
|
||||
// AGENT: A tiny migration runner instead of a framework: one advisory lock,
|
||||
// one schema_migrations row per file, each file in its own transaction.
|
||||
// Package database owns the PostgreSQL connection pool and schema migrations.
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"embed"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
//go:embed migrations/*.sql
|
||||
var migrationFiles embed.FS
|
||||
|
||||
const migrationLockID int64 = 728_2026
|
||||
|
||||
func Connect(ctx context.Context, url string, maxConns int32, wait time.Duration) (*pgxpool.Pool, error) {
|
||||
cfg, err := pgxpool.ParseConfig(url)
|
||||
if err != nil {
|
||||
@@ -53,75 +44,3 @@ func Connect(ctx context.Context, url string, maxConns int32, wait time.Duration
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
func Migrate(ctx context.Context, pool *pgxpool.Pool) error {
|
||||
conn, err := pool.Acquire(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("acquire migration connection: %w", err)
|
||||
}
|
||||
defer conn.Release()
|
||||
|
||||
if _, err = conn.Exec(ctx, "SELECT pg_advisory_lock($1)", migrationLockID); err != nil {
|
||||
return fmt.Errorf("acquire migration lock: %w", err)
|
||||
}
|
||||
defer func() {
|
||||
unlockCtx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
_, _ = conn.Exec(unlockCtx, "SELECT pg_advisory_unlock($1)", migrationLockID)
|
||||
}()
|
||||
|
||||
if _, err = conn.Exec(ctx, `
|
||||
CREATE TABLE IF NOT EXISTS schema_migrations (
|
||||
version text PRIMARY KEY,
|
||||
applied_at timestamptz NOT NULL DEFAULT now()
|
||||
)`); err != nil {
|
||||
return fmt.Errorf("create schema_migrations: %w", err)
|
||||
}
|
||||
|
||||
entries, err := fs.ReadDir(migrationFiles, "migrations")
|
||||
if err != nil {
|
||||
return fmt.Errorf("read embedded migrations: %w", err)
|
||||
}
|
||||
sort.Slice(entries, func(i, j int) bool { return entries[i].Name() < entries[j].Name() })
|
||||
|
||||
for _, entry := range entries {
|
||||
if entry.IsDir() {
|
||||
continue
|
||||
}
|
||||
|
||||
var applied bool
|
||||
if err = conn.QueryRow(ctx,
|
||||
"SELECT EXISTS(SELECT 1 FROM schema_migrations WHERE version = $1)", entry.Name(),
|
||||
).Scan(&applied); err != nil {
|
||||
return fmt.Errorf("check migration %s: %w", entry.Name(), err)
|
||||
}
|
||||
if applied {
|
||||
continue
|
||||
}
|
||||
|
||||
body, readErr := migrationFiles.ReadFile("migrations/" + entry.Name())
|
||||
if readErr != nil {
|
||||
return fmt.Errorf("read migration %s: %w", entry.Name(), readErr)
|
||||
}
|
||||
|
||||
tx, beginErr := conn.Begin(ctx)
|
||||
if beginErr != nil {
|
||||
return fmt.Errorf("begin migration %s: %w", entry.Name(), beginErr)
|
||||
}
|
||||
if _, execErr := tx.Exec(ctx, string(body)); execErr != nil {
|
||||
_ = tx.Rollback(ctx)
|
||||
return fmt.Errorf("apply migration %s: %w", entry.Name(), execErr)
|
||||
}
|
||||
if _, insertErr := tx.Exec(ctx,
|
||||
"INSERT INTO schema_migrations (version) VALUES ($1)", entry.Name(),
|
||||
); insertErr != nil {
|
||||
_ = tx.Rollback(ctx)
|
||||
return fmt.Errorf("record migration %s: %w", entry.Name(), insertErr)
|
||||
}
|
||||
if commitErr := tx.Commit(ctx); commitErr != nil {
|
||||
return fmt.Errorf("commit migration %s: %w", entry.Name(), commitErr)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,162 @@
|
||||
// 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 (
|
||||
"context"
|
||||
"embed"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/golang-migrate/migrate/v4"
|
||||
pgxmigrate "github.com/golang-migrate/migrate/v4/database/pgx/v5"
|
||||
"github.com/golang-migrate/migrate/v4/source/iofs"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/jackc/pgx/v5/stdlib"
|
||||
)
|
||||
|
||||
//go:embed migrations/*.sql
|
||||
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
|
||||
}
|
||||
|
||||
source, err := iofs.New(migrationFiles, "migrations")
|
||||
if err != nil {
|
||||
return fmt.Errorf("load embedded migrations: %w", err)
|
||||
}
|
||||
|
||||
connConfig, err := pgx.ParseConfig(databaseURL)
|
||||
if err != nil {
|
||||
_ = 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)
|
||||
}
|
||||
connConfig.RuntimeParams["lock_timeout"] = "30000"
|
||||
connConfig.ConnectTimeout = 3 * time.Second
|
||||
|
||||
db := stdlib.OpenDB(*connConfig)
|
||||
db.SetMaxOpenConns(1)
|
||||
db.SetMaxIdleConns(1)
|
||||
|
||||
driver, err := pgxmigrate.WithInstance(db, &pgxmigrate.Config{
|
||||
MigrationsTable: migrationsTable,
|
||||
StatementTimeout: 30 * time.Second,
|
||||
})
|
||||
if err != nil {
|
||||
_ = source.Close()
|
||||
_ = db.Close()
|
||||
return fmt.Errorf("create migration driver: %w", err)
|
||||
}
|
||||
|
||||
migrator, err := migrate.NewWithInstance("iofs", source, "pgx5", driver)
|
||||
if err != nil {
|
||||
_ = source.Close()
|
||||
_ = driver.Close()
|
||||
return fmt.Errorf("create migrator: %w", err)
|
||||
}
|
||||
defer func() {
|
||||
// Close closes both the embedded source and the database connection.
|
||||
_, _ = migrator.Close()
|
||||
}()
|
||||
|
||||
if err := migrator.Up(); err != nil && !errors.Is(err, migrate.ErrNoChange) {
|
||||
return fmt.Errorf("apply migrations: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// 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
|
||||
// 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.
|
||||
//
|
||||
// AGENT: This is a one-time compatibility bridge, not a permanent second
|
||||
// migration format. It runs under the old advisory lock before golang-migrate
|
||||
// creates its own lock and metadata.
|
||||
func upgradeLegacyMigrationsTable(ctx context.Context, pool *pgxpool.Pool) error {
|
||||
conn, err := pool.Acquire(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("acquire migration connection: %w", err)
|
||||
}
|
||||
defer conn.Release()
|
||||
|
||||
if _, err := conn.Exec(ctx, "SELECT pg_advisory_lock($1)", legacyMigrationLockID); err != nil {
|
||||
return fmt.Errorf("acquire migration lock: %w", err)
|
||||
}
|
||||
defer func() {
|
||||
unlockCtx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
_, _ = conn.Exec(unlockCtx, "SELECT pg_advisory_unlock($1)", legacyMigrationLockID)
|
||||
}()
|
||||
|
||||
var legacy bool
|
||||
if err := conn.QueryRow(ctx, `
|
||||
SELECT EXISTS (
|
||||
SELECT 1
|
||||
FROM information_schema.columns
|
||||
WHERE table_schema = current_schema()
|
||||
AND table_name = $1
|
||||
AND column_name = 'applied_at'
|
||||
)`, migrationsTable).Scan(&legacy); err != nil {
|
||||
return fmt.Errorf("inspect %s: %w", migrationsTable, err)
|
||||
}
|
||||
if !legacy {
|
||||
return nil
|
||||
}
|
||||
|
||||
tx, err := conn.Begin(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("begin migration table upgrade: %w", err)
|
||||
}
|
||||
defer func() { _ = tx.Rollback(context.Background()) }()
|
||||
|
||||
const upgrade = `
|
||||
DROP TABLE IF EXISTS schema_migrations_migrate_tmp;
|
||||
|
||||
CREATE TABLE schema_migrations_migrate_tmp (
|
||||
version bigint NOT NULL PRIMARY KEY,
|
||||
dirty boolean NOT NULL
|
||||
);
|
||||
|
||||
INSERT INTO schema_migrations_migrate_tmp (version, dirty)
|
||||
SELECT MAX(substring(version FROM '^[0-9]+')::bigint), false
|
||||
FROM schema_migrations
|
||||
WHERE version ~ '^[0-9]+'
|
||||
HAVING COUNT(*) > 0;
|
||||
|
||||
DROP TABLE schema_migrations;
|
||||
ALTER TABLE schema_migrations_migrate_tmp RENAME TO schema_migrations;`
|
||||
|
||||
if _, err := tx.Exec(ctx, upgrade); err != nil {
|
||||
return fmt.Errorf("upgrade legacy %s: %w", migrationsTable, err)
|
||||
}
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return fmt.Errorf("commit migration table upgrade: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
-- AGENT: Local rollback for the baseline schema. The application only runs Up
|
||||
-- migrations; this file exists so golang-migrate can also step down in tests.
|
||||
DROP TABLE IF EXISTS messages;
|
||||
DROP TABLE IF EXISTS chat_members;
|
||||
DROP TABLE IF EXISTS chats;
|
||||
DROP TABLE IF EXISTS users;
|
||||
@@ -0,0 +1,43 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io/fs"
|
||||
"testing"
|
||||
|
||||
"github.com/golang-migrate/migrate/v4/source/iofs"
|
||||
)
|
||||
|
||||
func TestEmbeddedMigrationsParse(t *testing.T) {
|
||||
entries, err := fs.ReadDir(migrationFiles, "migrations")
|
||||
if err != nil {
|
||||
t.Fatalf("read embedded migrations: %v", err)
|
||||
}
|
||||
|
||||
names := make(map[string]bool, len(entries))
|
||||
for _, entry := range entries {
|
||||
names[entry.Name()] = true
|
||||
}
|
||||
for _, want := range []string{"0001_init.up.sql", "0001_init.down.sql"} {
|
||||
if !names[want] {
|
||||
t.Fatalf("migration %q is missing from the embedded filesystem", want)
|
||||
}
|
||||
}
|
||||
|
||||
source, err := iofs.New(migrationFiles, "migrations")
|
||||
if err != nil {
|
||||
t.Fatalf("parse migrations with golang-migrate: %v", err)
|
||||
}
|
||||
defer func() { _ = source.Close() }()
|
||||
|
||||
version, err := source.First()
|
||||
if err != nil {
|
||||
t.Fatalf("read first migration: %v", err)
|
||||
}
|
||||
if version != 1 {
|
||||
t.Fatalf("first migration version = %d, want 1", version)
|
||||
}
|
||||
if _, err := source.Next(version); !errors.Is(err, fs.ErrNotExist) {
|
||||
t.Fatalf("expected the baseline to be the last migration, got %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user