refactor(app) agent refactoring + a little review

This commit is contained in:
imadev
2026-10-31 20:22:16 +03:00
parent 2ef20e7040
commit 362ba16cc4
25 changed files with 240 additions and 375 deletions
+5 -89
View File
@@ -1,7 +1,6 @@
package database
import (
"context"
"embed"
"errors"
"fmt"
@@ -11,23 +10,18 @@ import (
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 (
legacyMigrationLockID int64 = 728_2026
migrationsTable = "schema_migrations"
)
func Migrate(ctx context.Context, pool *pgxpool.Pool, databaseURL string) error {
if err := upgradeLegacyMigrationsTable(ctx, pool); err != nil {
return err
}
const migrationsTable = "schema_migrations"
// Migrate applies every pending embedded migration. golang-migrate takes its
// own advisory lock, so concurrent processes serialise instead of racing the
// schema.
func Migrate(databaseURL string) error {
source, err := iofs.New(migrationFiles, "migrations")
if err != nil {
return fmt.Errorf("load embedded migrations: %w", err)
@@ -75,81 +69,3 @@ func Migrate(ctx context.Context, pool *pgxpool.Pool, databaseURL string) error
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 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.
//
// 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() {
rollbackCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
_ = tx.Rollback(rollbackCtx)
}()
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
}