forked from mtusi2026/mtusi2026lab3
chore(app) transferred codebase from Lab2
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
// Package database owns the PostgreSQL connection pool and schema migrations.
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
func Connect(ctx context.Context, url string, maxConns int32, wait time.Duration) (*pgxpool.Pool, error) {
|
||||
cfg, err := pgxpool.ParseConfig(url)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parse DATABASE_URL: %w", err)
|
||||
}
|
||||
|
||||
cfg.MaxConns = maxConns
|
||||
cfg.MinConns = 1
|
||||
cfg.MaxConnLifetime = 30 * time.Minute
|
||||
cfg.MaxConnIdleTime = 5 * time.Minute
|
||||
cfg.HealthCheckPeriod = 30 * time.Second
|
||||
cfg.ConnConfig.ConnectTimeout = 3 * time.Second
|
||||
|
||||
pool, err := pgxpool.NewWithConfig(ctx, cfg)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create pool: %w", err)
|
||||
}
|
||||
|
||||
// AGENT: Compose may start the API while PostgreSQL is still booting. A bounded
|
||||
// retry keeps the container from crash-looping without hiding a permanent error.
|
||||
deadline := time.Now().Add(wait)
|
||||
for {
|
||||
pingCtx, cancel := context.WithTimeout(ctx, 2*time.Second)
|
||||
err = pool.Ping(pingCtx)
|
||||
cancel()
|
||||
if err == nil {
|
||||
return pool, nil
|
||||
}
|
||||
if time.Now().After(deadline) || ctx.Err() != nil {
|
||||
pool.Close()
|
||||
return nil, fmt.Errorf("postgres is not reachable: %w", err)
|
||||
}
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
@@ -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,46 @@
|
||||
-- AGENT: Single versioned baseline migration for lab 2. The application records
|
||||
-- applied versions in schema_migrations, so rerunning it never duplicates rows.
|
||||
-- The unique username constraint is the authoritative defence against the
|
||||
-- concurrent-registration race, not a SELECT followed by an INSERT.
|
||||
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id uuid PRIMARY KEY,
|
||||
username text NOT NULL,
|
||||
display_name text NOT NULL,
|
||||
password_hash text NOT NULL,
|
||||
created_at timestamptz NOT NULL,
|
||||
CONSTRAINT users_username_key UNIQUE (username),
|
||||
CONSTRAINT users_username_format CHECK (username ~ '^[a-z0-9_]{3,32}$'),
|
||||
CONSTRAINT users_display_name_len CHECK (char_length(display_name) BETWEEN 1 AND 100)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS chats (
|
||||
id uuid PRIMARY KEY,
|
||||
title text NOT NULL,
|
||||
created_at timestamptz NOT NULL,
|
||||
CONSTRAINT chats_title_len CHECK (char_length(title) BETWEEN 1 AND 100)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS chat_members (
|
||||
chat_id uuid NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
|
||||
user_id uuid NOT NULL REFERENCES users(id) ON DELETE RESTRICT,
|
||||
PRIMARY KEY (chat_id, user_id)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS chat_members_user_idx
|
||||
ON chat_members (user_id, chat_id);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS messages (
|
||||
id uuid PRIMARY KEY,
|
||||
chat_id uuid NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
|
||||
sender_id uuid NOT NULL REFERENCES users(id) ON DELETE RESTRICT,
|
||||
text text NOT NULL,
|
||||
created_at timestamptz NOT NULL,
|
||||
CONSTRAINT messages_text_len CHECK (char_length(text) BETWEEN 1 AND 4000)
|
||||
);
|
||||
|
||||
-- AGENT: This composite index matches the only history predicate and sort order:
|
||||
-- (chat_id, created_at, id). It lets each keyset page seek directly to the cursor
|
||||
-- instead of scanning the chat history from the beginning.
|
||||
CREATE INDEX IF NOT EXISTS messages_chat_created_id_idx
|
||||
ON messages (chat_id, created_at, id);
|
||||
@@ -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