From 18afe2563e014f24a86497fa38cc02b2b31459c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Fri, 6 Nov 2026 21:27:05 +0300 Subject: [PATCH] feat(db): lab5 migration 0005 add queued/processing statuses and job columns on attachments --- backend/migrations/0005_jobs.sql | 77 ++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 backend/migrations/0005_jobs.sql diff --git a/backend/migrations/0005_jobs.sql b/backend/migrations/0005_jobs.sql new file mode 100644 index 00000000..07913480 --- /dev/null +++ b/backend/migrations/0005_jobs.sql @@ -0,0 +1,77 @@ +-- +goose Up +-- +goose StatementBegin + + +ALTER TABLE attachments DROP CONSTRAINT IF EXISTS attachments_status_chk; +ALTER TABLE attachments ADD CONSTRAINT attachments_status_chk + CHECK (status IN ('pending', 'queued', 'processing', 'ready', 'failed')); + +ALTER TABLE attachments DROP CONSTRAINT IF EXISTS attachments_error_code_consistency; +ALTER TABLE attachments ADD CONSTRAINT attachments_error_code_consistency + CHECK ( + (status IN ('pending', 'queued', 'processing', 'ready') AND error_code IS NULL) OR + (status = 'failed' AND error_code IS NOT NULL) + ); + +ALTER TABLE attachments ADD COLUMN attempts INT NOT NULL DEFAULT 0; +ALTER TABLE attachments ADD CONSTRAINT attachments_attempts_nonneg + CHECK (attempts >= 0); + +ALTER TABLE attachments ADD COLUMN next_attempt_at TIMESTAMPTZ; +ALTER TABLE attachments ADD COLUMN locked_until TIMESTAMPTZ; + +ALTER TABLE attachments ADD COLUMN pipeline_version INT NOT NULL DEFAULT 1; +ALTER TABLE attachments ADD CONSTRAINT attachments_pipeline_version_pos + CHECK (pipeline_version >= 1); + +ALTER TABLE attachments ADD COLUMN staging_key TEXT; +ALTER TABLE attachments ADD CONSTRAINT attachments_staging_key_len + CHECK (staging_key IS NULL OR char_length(staging_key) BETWEEN 1 AND 1024); + + +DROP INDEX IF EXISTS attachments_pending_idx; + +CREATE INDEX attachments_active_idx + ON attachments (status, next_attempt_at) + WHERE status IN ('queued', 'processing'); + +CREATE INDEX attachments_locked_idx + ON attachments (locked_until) + WHERE status = 'processing'; + +-- +goose StatementEnd + +-- +goose Down +-- +goose StatementBegin + +DROP INDEX IF EXISTS attachments_locked_idx; +DROP INDEX IF EXISTS attachments_active_idx; + +CREATE INDEX attachments_pending_idx + ON attachments (created_at) + WHERE status = 'pending'; + +ALTER TABLE attachments DROP CONSTRAINT IF EXISTS attachments_staging_key_len; +ALTER TABLE attachments DROP COLUMN IF EXISTS staging_key; + +ALTER TABLE attachments DROP CONSTRAINT IF EXISTS attachments_pipeline_version_pos; +ALTER TABLE attachments DROP COLUMN IF EXISTS pipeline_version; + +ALTER TABLE attachments DROP COLUMN IF EXISTS locked_until; +ALTER TABLE attachments DROP COLUMN IF EXISTS next_attempt_at; + +ALTER TABLE attachments DROP CONSTRAINT IF EXISTS attachments_attempts_nonneg; +ALTER TABLE attachments DROP COLUMN IF EXISTS attempts; + +ALTER TABLE attachments DROP CONSTRAINT IF EXISTS attachments_error_code_consistency; +ALTER TABLE attachments ADD CONSTRAINT attachments_error_code_consistency + CHECK ( + (status IN ('ready', 'pending') AND error_code IS NULL) OR + (status = 'failed' AND error_code IS NOT NULL) + ); + +ALTER TABLE attachments DROP CONSTRAINT IF EXISTS attachments_status_chk; +ALTER TABLE attachments ADD CONSTRAINT attachments_status_chk + CHECK (status IN ('ready', 'pending', 'failed')); + +-- +goose StatementEnd \ No newline at end of file