Compare commits

...

2 Commits

Author SHA1 Message Date
Grendgi
79eac4d251 fix: make files initial migration idempotent
Some checks failed
CI / hygiene (push) Successful in 1s
Build and Deploy / build-and-deploy (push) Failing after 4m16s
CI / test (push) Successful in 18s
2026-06-16 13:12:34 +03:00
Grendgi
5d721186cd fix: serialize files migrations during rollout 2026-06-16 13:11:24 +03:00
4 changed files with 30 additions and 23 deletions

View File

@@ -55,5 +55,4 @@ jobs:
kubectl apply -f k8s/server-service.yaml
kubectl -n files set image deployment/files-server \
files-server=${{ env.NODE_REGISTRY }}/admin/files-server:${{ github.sha }}
kubectl -n files rollout status deployment/files-server --timeout=120s
kubectl -n files rollout status deployment/files-server --timeout=240s

View File

@@ -13,7 +13,17 @@ import (
)
func Run(ctx context.Context, pool *pgxpool.Pool, migrationsDir string) error {
_, err := pool.Exec(ctx, `
tx, err := pool.Begin(ctx)
if err != nil {
return fmt.Errorf("begin migration tx: %w", err)
}
defer tx.Rollback(ctx)
if _, err := tx.Exec(ctx, `SELECT pg_advisory_xact_lock(8507432101)`); err != nil {
return fmt.Errorf("acquire migration lock: %w", err)
}
_, err = tx.Exec(ctx, `
CREATE TABLE IF NOT EXISTS schema_migrations (
version VARCHAR(255) PRIMARY KEY,
applied_at TIMESTAMPTZ NOT NULL DEFAULT now()
@@ -32,7 +42,7 @@ func Run(ctx context.Context, pool *pgxpool.Pool, migrationsDir string) error {
for _, f := range files {
version := strings.TrimSuffix(filepath.Base(f), ".up.sql")
var exists bool
if err := pool.QueryRow(ctx, `SELECT EXISTS(SELECT 1 FROM schema_migrations WHERE version = $1)`, version).Scan(&exists); err != nil {
if err := tx.QueryRow(ctx, `SELECT EXISTS(SELECT 1 FROM schema_migrations WHERE version = $1)`, version).Scan(&exists); err != nil {
return fmt.Errorf("check migration %s: %w", version, err)
}
if exists {
@@ -42,13 +52,13 @@ func Run(ctx context.Context, pool *pgxpool.Pool, migrationsDir string) error {
if err != nil {
return fmt.Errorf("read migration %s: %w", version, err)
}
if _, err := pool.Exec(ctx, string(sql)); err != nil {
if _, err := tx.Exec(ctx, string(sql)); err != nil {
return fmt.Errorf("apply migration %s: %w", version, err)
}
if _, err := pool.Exec(ctx, `INSERT INTO schema_migrations (version) VALUES ($1)`, version); err != nil {
if _, err := tx.Exec(ctx, `INSERT INTO schema_migrations (version) VALUES ($1)`, version); err != nil {
return fmt.Errorf("record migration %s: %w", version, err)
}
slog.Info("applied migration", "version", version)
}
return nil
return tx.Commit(ctx)
}

View File

@@ -46,7 +46,7 @@ spec:
path: /healthz
port: 3001
periodSeconds: 5
failureThreshold: 30
failureThreshold: 60
livenessProbe:
httpGet:
path: /healthz
@@ -84,4 +84,3 @@ spec:
target:
type: Utilization
averageUtilization: 70

View File

@@ -1,6 +1,6 @@
CREATE EXTENSION IF NOT EXISTS pgcrypto;
CREATE TABLE files_nodes (
CREATE TABLE IF NOT EXISTS files_nodes (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
parent_id UUID REFERENCES files_nodes(id) ON DELETE CASCADE,
node_type TEXT NOT NULL CHECK (node_type IN ('folder', 'file', 'google_sheet', 'office_document')),
@@ -25,11 +25,11 @@ CREATE TABLE files_nodes (
)
);
CREATE INDEX files_nodes_parent_idx ON files_nodes(parent_id) WHERE deleted_at IS NULL;
CREATE INDEX files_nodes_owner_idx ON files_nodes(owner_user_id) WHERE deleted_at IS NULL;
CREATE INDEX files_nodes_type_idx ON files_nodes(node_type) WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS files_nodes_parent_idx ON files_nodes(parent_id) WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS files_nodes_owner_idx ON files_nodes(owner_user_id) WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS files_nodes_type_idx ON files_nodes(node_type) WHERE deleted_at IS NULL;
CREATE TABLE files_access (
CREATE TABLE IF NOT EXISTS files_access (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
node_id UUID NOT NULL REFERENCES files_nodes(id) ON DELETE CASCADE,
user_id UUID NOT NULL,
@@ -39,10 +39,10 @@ CREATE TABLE files_access (
UNIQUE (node_id, user_id)
);
CREATE INDEX files_access_node_idx ON files_access(node_id);
CREATE INDEX files_access_user_idx ON files_access(user_id);
CREATE INDEX IF NOT EXISTS files_access_node_idx ON files_access(node_id);
CREATE INDEX IF NOT EXISTS files_access_user_idx ON files_access(user_id);
CREATE TABLE files_public_links (
CREATE TABLE IF NOT EXISTS files_public_links (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
node_id UUID NOT NULL REFERENCES files_nodes(id) ON DELETE CASCADE,
token_hash TEXT NOT NULL UNIQUE,
@@ -53,10 +53,10 @@ CREATE TABLE files_public_links (
revoked_at TIMESTAMPTZ
);
CREATE INDEX files_public_links_node_idx ON files_public_links(node_id);
CREATE INDEX files_public_links_active_idx ON files_public_links(token_hash, expires_at) WHERE revoked_at IS NULL;
CREATE INDEX IF NOT EXISTS files_public_links_node_idx ON files_public_links(node_id);
CREATE INDEX IF NOT EXISTS files_public_links_active_idx ON files_public_links(token_hash, expires_at) WHERE revoked_at IS NULL;
CREATE TABLE files_audit_events (
CREATE TABLE IF NOT EXISTS files_audit_events (
id BIGSERIAL PRIMARY KEY,
actor_user_id UUID,
action TEXT NOT NULL,
@@ -66,6 +66,5 @@ CREATE TABLE files_audit_events (
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX files_audit_events_actor_idx ON files_audit_events(actor_user_id, created_at DESC);
CREATE INDEX files_audit_events_entity_idx ON files_audit_events(entity_type, entity_id, created_at DESC);
CREATE INDEX IF NOT EXISTS files_audit_events_actor_idx ON files_audit_events(actor_user_id, created_at DESC);
CREATE INDEX IF NOT EXISTS files_audit_events_entity_idx ON files_audit_events(entity_type, entity_id, created_at DESC);