forked from mtusi2026/mtusi2026lab5
chore(migrate) moved codebase from lab4
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
// 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)
|
||||
}
|
||||
|
||||
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,71 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"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/stdlib"
|
||||
)
|
||||
|
||||
//go:embed migrations/*.sql
|
||||
var migrationFiles embed.FS
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
connConfig, err := pgx.ParseConfig(databaseURL)
|
||||
if err != nil {
|
||||
_ = source.Close()
|
||||
return fmt.Errorf("parse DATABASE_URL for migrations: %w", err)
|
||||
}
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
DROP TABLE IF EXISTS messages;
|
||||
DROP TABLE IF EXISTS chat_members;
|
||||
DROP TABLE IF EXISTS chats;
|
||||
DROP TABLE IF EXISTS users;
|
||||
@@ -0,0 +1,38 @@
|
||||
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)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS messages_chat_created_id_idx
|
||||
ON messages (chat_id, created_at, id);
|
||||
@@ -0,0 +1 @@
|
||||
DROP TABLE IF EXISTS message_idempotency;
|
||||
@@ -0,0 +1,14 @@
|
||||
CREATE TABLE IF NOT EXISTS message_idempotency (
|
||||
sender_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
chat_id uuid NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
|
||||
idempotency_key text NOT NULL,
|
||||
request_hash bytea NOT NULL,
|
||||
message_id uuid NOT NULL REFERENCES messages(id) ON DELETE CASCADE,
|
||||
created_at timestamptz NOT NULL,
|
||||
expires_at timestamptz NOT NULL,
|
||||
PRIMARY KEY (sender_id, chat_id, idempotency_key),
|
||||
CONSTRAINT message_idempotency_key_len CHECK (char_length(idempotency_key) BETWEEN 1 AND 128)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS message_idempotency_expires_at_idx
|
||||
ON message_idempotency (expires_at);
|
||||
@@ -0,0 +1,6 @@
|
||||
DROP TABLE IF EXISTS message_attachments;
|
||||
DROP TABLE IF EXISTS attachments;
|
||||
|
||||
ALTER TABLE messages DROP CONSTRAINT IF EXISTS messages_text_len;
|
||||
ALTER TABLE messages
|
||||
ADD CONSTRAINT messages_text_len CHECK (char_length(text) BETWEEN 1 AND 4000);
|
||||
@@ -0,0 +1,46 @@
|
||||
ALTER TABLE messages DROP CONSTRAINT IF EXISTS messages_text_len;
|
||||
ALTER TABLE messages
|
||||
ADD CONSTRAINT messages_text_len CHECK (char_length(text) BETWEEN 0 AND 4000);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS attachments (
|
||||
id uuid PRIMARY KEY,
|
||||
chat_id uuid NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
|
||||
owner_id uuid NOT NULL REFERENCES users(id) ON DELETE RESTRICT,
|
||||
filename text NOT NULL,
|
||||
content_type text NOT NULL,
|
||||
size_bytes bigint NOT NULL,
|
||||
sha256 bytea NOT NULL,
|
||||
object_key text NOT NULL,
|
||||
status text NOT NULL,
|
||||
error_code text,
|
||||
created_at timestamptz NOT NULL,
|
||||
updated_at timestamptz NOT NULL,
|
||||
CONSTRAINT attachments_filename_len CHECK (char_length(filename) BETWEEN 1 AND 255),
|
||||
CONSTRAINT attachments_content_type CHECK (content_type IN ('image/png', 'image/jpeg')),
|
||||
CONSTRAINT attachments_size CHECK (size_bytes BETWEEN 1 AND 10485760),
|
||||
CONSTRAINT attachments_sha256_len CHECK (octet_length(sha256) = 32),
|
||||
CONSTRAINT attachments_status CHECK (status IN ('pending', 'ready', 'failed')),
|
||||
CONSTRAINT attachments_error_state CHECK (
|
||||
(status = 'pending' AND error_code IS NULL) OR
|
||||
(status = 'ready' AND error_code IS NULL) OR
|
||||
(status = 'failed' AND error_code IS NOT NULL)
|
||||
),
|
||||
CONSTRAINT attachments_object_key_key UNIQUE (object_key)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS attachments_chat_created_idx
|
||||
ON attachments (chat_id, created_at, id);
|
||||
CREATE INDEX IF NOT EXISTS attachments_status_created_idx
|
||||
ON attachments (status, created_at);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS message_attachments (
|
||||
message_id uuid NOT NULL REFERENCES messages(id) ON DELETE CASCADE,
|
||||
attachment_id uuid NOT NULL REFERENCES attachments(id) ON DELETE RESTRICT,
|
||||
position integer NOT NULL,
|
||||
PRIMARY KEY (message_id, attachment_id),
|
||||
CONSTRAINT message_attachments_position CHECK (position >= 0),
|
||||
CONSTRAINT message_attachments_position_key UNIQUE (message_id, position)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS message_attachments_attachment_idx
|
||||
ON message_attachments (attachment_id);
|
||||
@@ -0,0 +1,88 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io/fs"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/golang-migrate/migrate/v4/source/iofs"
|
||||
)
|
||||
|
||||
// latestMigrationVersion is the only value to bump when a migration is added.
|
||||
// The rest of the test derives filenames and ordering from the directory.
|
||||
const latestMigrationVersion = 3
|
||||
|
||||
var migrationFilePattern = regexp.MustCompile(`^(\d{4})_[a-z0-9_]+\.(up|down)\.sql$`)
|
||||
|
||||
func TestEmbeddedMigrationsAreCompleteAndContiguous(t *testing.T) {
|
||||
entries, err := fs.ReadDir(migrationFiles, "migrations")
|
||||
if err != nil {
|
||||
t.Fatalf("read embedded migrations: %v", err)
|
||||
}
|
||||
|
||||
up := make(map[int]string, latestMigrationVersion)
|
||||
down := make(map[int]string, latestMigrationVersion)
|
||||
|
||||
for _, entry := range entries {
|
||||
if entry.IsDir() {
|
||||
continue
|
||||
}
|
||||
name := entry.Name()
|
||||
match := migrationFilePattern.FindStringSubmatch(name)
|
||||
if match == nil {
|
||||
t.Fatalf("migration %q does not match NNNN_name.{up,down}.sql", name)
|
||||
}
|
||||
|
||||
version, parseErr := strconv.Atoi(match[1])
|
||||
if parseErr != nil || version < 1 || version > latestMigrationVersion {
|
||||
t.Fatalf("migration %q has version outside 1..%d", name, latestMigrationVersion)
|
||||
}
|
||||
|
||||
files := up
|
||||
if match[2] == "down" {
|
||||
files = down
|
||||
}
|
||||
if previous, ok := files[version]; ok {
|
||||
t.Fatalf("version %d has two %s files: %q and %q", version, match[2], previous, name)
|
||||
}
|
||||
files[version] = name
|
||||
}
|
||||
|
||||
for version := 1; version <= latestMigrationVersion; version++ {
|
||||
if up[version] == "" {
|
||||
t.Fatalf("version %d is missing an up migration", version)
|
||||
}
|
||||
if down[version] == "" {
|
||||
t.Fatalf("version %d is missing a down migration", version)
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
for expected := 1; expected <= latestMigrationVersion; expected++ {
|
||||
if version != uint(expected) {
|
||||
t.Fatalf("migration order = %d, want %d", version, expected)
|
||||
}
|
||||
next, nextErr := source.Next(version)
|
||||
if expected == latestMigrationVersion {
|
||||
if !errors.Is(nextErr, fs.ErrNotExist) {
|
||||
t.Fatalf("expected %d to be the last migration, got %v", version, nextErr)
|
||||
}
|
||||
break
|
||||
}
|
||||
if nextErr != nil {
|
||||
t.Fatalf("read migration after %d: %v", version, nextErr)
|
||||
}
|
||||
version = next
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user