forked from mtusi2026/mtusi2026lab6
chore: import lab5 baseline
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab5/migrations"
|
||||
)
|
||||
|
||||
var (
|
||||
errURLRequired = errors.New("database URL is required")
|
||||
errMinConnections = errors.New("minimum database connections must not be negative")
|
||||
errMaxConnections = errors.New("maximum database connections must be positive")
|
||||
errConnectionRange = errors.New("minimum database connections exceed maximum")
|
||||
errMaxConnectionAge = errors.New("maximum database connection age must be positive")
|
||||
errMaxConnectionIdle = errors.New("maximum database connection idle time must be positive")
|
||||
errHealthCheckPeriod = errors.New("database health check period must be positive")
|
||||
errConnectionTimeout = errors.New("database connection timeout must be positive")
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
URL string
|
||||
MinConnections int32
|
||||
MaxConnections int32
|
||||
MaxConnectionAge time.Duration
|
||||
MaxConnectionIdle time.Duration
|
||||
HealthCheckPeriod time.Duration
|
||||
ConnectTimeout time.Duration
|
||||
}
|
||||
|
||||
func Open(ctx context.Context, config Config) (*pgxpool.Pool, error) {
|
||||
if err := validateConfig(config); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
poolConfig, err := pgxpool.ParseConfig(config.URL)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parse database configuration: %w", err)
|
||||
}
|
||||
poolConfig.MinConns = config.MinConnections
|
||||
poolConfig.MaxConns = config.MaxConnections
|
||||
poolConfig.MaxConnLifetime = config.MaxConnectionAge
|
||||
poolConfig.MaxConnIdleTime = config.MaxConnectionIdle
|
||||
poolConfig.HealthCheckPeriod = config.HealthCheckPeriod
|
||||
|
||||
connectCtx, cancel := context.WithTimeout(ctx, config.ConnectTimeout)
|
||||
defer cancel()
|
||||
pool, err := pgxpool.NewWithConfig(connectCtx, poolConfig)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create database pool: %w", err)
|
||||
}
|
||||
if err := pool.Ping(connectCtx); err != nil {
|
||||
pool.Close()
|
||||
return nil, fmt.Errorf("ping database: %w", err)
|
||||
}
|
||||
|
||||
return pool, nil
|
||||
}
|
||||
|
||||
func Migrate(ctx context.Context, pool *pgxpool.Pool, timeout time.Duration) error {
|
||||
if timeout <= 0 {
|
||||
return errors.New("database migration timeout must be positive")
|
||||
}
|
||||
migrationCtx, cancel := context.WithTimeout(ctx, timeout)
|
||||
defer cancel()
|
||||
return applyMigrations(migrationCtx, pool, migrations.FS)
|
||||
}
|
||||
|
||||
func validateConfig(config Config) error {
|
||||
switch {
|
||||
case config.URL == "":
|
||||
return errURLRequired
|
||||
case config.MinConnections < 0:
|
||||
return errMinConnections
|
||||
case config.MaxConnections < 1:
|
||||
return errMaxConnections
|
||||
case config.MinConnections > config.MaxConnections:
|
||||
return errConnectionRange
|
||||
case config.MaxConnectionAge <= 0:
|
||||
return errMaxConnectionAge
|
||||
case config.MaxConnectionIdle <= 0:
|
||||
return errMaxConnectionIdle
|
||||
case config.HealthCheckPeriod <= 0:
|
||||
return errHealthCheckPeriod
|
||||
case config.ConnectTimeout <= 0:
|
||||
return errConnectionTimeout
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
const (
|
||||
migrationLockID int64 = 0x4d54555349324c42
|
||||
migrationUnlockTimeout = 5 * time.Second
|
||||
)
|
||||
|
||||
var migrationNamePattern = regexp.MustCompile(`^(\d+)_([a-z0-9_]+)\.sql$`)
|
||||
|
||||
type migration struct {
|
||||
version int64
|
||||
name string
|
||||
checksum string
|
||||
sql string
|
||||
}
|
||||
|
||||
func applyMigrations(ctx context.Context, pool *pgxpool.Pool, migrationFS fs.FS) (returnErr error) {
|
||||
migrations, err := readMigrations(migrationFS)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
connection, err := pool.Acquire(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("acquire connection for migrations: %w", err)
|
||||
}
|
||||
defer connection.Release()
|
||||
if _, err := connection.Exec(ctx, "SELECT pg_advisory_lock($1)", migrationLockID); err != nil {
|
||||
return fmt.Errorf("lock migrations: %w", err)
|
||||
}
|
||||
defer func() {
|
||||
unlockCtx, cancel := context.WithTimeout(context.Background(), migrationUnlockTimeout)
|
||||
defer cancel()
|
||||
if _, err := connection.Exec(unlockCtx, "SELECT pg_advisory_unlock($1)", migrationLockID); err != nil {
|
||||
_ = connection.Conn().Close(unlockCtx)
|
||||
if returnErr == nil {
|
||||
returnErr = fmt.Errorf("unlock migrations: %w", err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
if _, err := connection.Exec(ctx, `
|
||||
CREATE TABLE IF NOT EXISTS schema_migrations (
|
||||
version BIGINT PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
checksum TEXT NOT NULL,
|
||||
applied_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp()
|
||||
)`); err != nil {
|
||||
return fmt.Errorf("create migration history: %w", err)
|
||||
}
|
||||
for _, migration := range migrations {
|
||||
if err := applyMigration(ctx, connection, migration); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func readMigrations(migrationFS fs.FS) ([]migration, error) {
|
||||
entries, err := fs.ReadDir(migrationFS, ".")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read embedded migrations: %w", err)
|
||||
}
|
||||
migrations := make([]migration, 0, len(entries))
|
||||
versions := make(map[int64]string)
|
||||
for _, entry := range entries {
|
||||
if entry.IsDir() || filepath.Ext(entry.Name()) != ".sql" {
|
||||
continue
|
||||
}
|
||||
matches := migrationNamePattern.FindStringSubmatch(entry.Name())
|
||||
if matches == nil {
|
||||
return nil, fmt.Errorf("invalid migration filename %q", entry.Name())
|
||||
}
|
||||
version, err := strconv.ParseInt(matches[1], 10, 64)
|
||||
if err != nil || version < 1 {
|
||||
return nil, fmt.Errorf("invalid migration version in %q", entry.Name())
|
||||
}
|
||||
if previous, exists := versions[version]; exists {
|
||||
return nil, fmt.Errorf("migration version %d is used by %q and %q", version, previous, entry.Name())
|
||||
}
|
||||
data, err := fs.ReadFile(migrationFS, entry.Name())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read migration %q: %w", entry.Name(), err)
|
||||
}
|
||||
hash := sha256.Sum256(data)
|
||||
versions[version] = entry.Name()
|
||||
migrations = append(migrations, migration{
|
||||
version: version,
|
||||
name: entry.Name(),
|
||||
checksum: hex.EncodeToString(hash[:]),
|
||||
sql: string(data),
|
||||
})
|
||||
}
|
||||
if len(migrations) == 0 {
|
||||
return nil, errors.New("no database migrations found")
|
||||
}
|
||||
sort.Slice(migrations, func(i, j int) bool {
|
||||
return migrations[i].version < migrations[j].version
|
||||
})
|
||||
return migrations, nil
|
||||
}
|
||||
|
||||
func applyMigration(ctx context.Context, connection *pgxpool.Conn, migration migration) error {
|
||||
var checksum string
|
||||
err := connection.QueryRow(ctx, "SELECT checksum FROM schema_migrations WHERE version = $1", migration.version).Scan(&checksum)
|
||||
switch {
|
||||
case err == nil && checksum != migration.checksum:
|
||||
return fmt.Errorf("migration %q checksum does not match applied version", migration.name)
|
||||
case err == nil:
|
||||
return nil
|
||||
case !errors.Is(err, pgx.ErrNoRows):
|
||||
return fmt.Errorf("read migration %q state: %w", migration.name, err)
|
||||
}
|
||||
|
||||
transaction, err := connection.Begin(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("begin migration %q: %w", migration.name, err)
|
||||
}
|
||||
defer func() {
|
||||
_ = transaction.Rollback(context.WithoutCancel(ctx))
|
||||
}()
|
||||
if _, err := transaction.Exec(ctx, migration.sql, pgx.QueryExecModeSimpleProtocol); err != nil {
|
||||
return fmt.Errorf("apply migration %q: %w", migration.name, err)
|
||||
}
|
||||
if _, err := transaction.Exec(ctx, `
|
||||
INSERT INTO schema_migrations (version, name, checksum)
|
||||
VALUES ($1, $2, $3)`, migration.version, migration.name, migration.checksum); err != nil {
|
||||
return fmt.Errorf("record migration %q: %w", migration.name, err)
|
||||
}
|
||||
if err := transaction.Commit(ctx); err != nil {
|
||||
return fmt.Errorf("commit migration %q: %w", migration.name, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user