This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
BEGIN;
|
||||
|
||||
DROP TABLE sessions;
|
||||
DROP TABLE messages;
|
||||
DROP TABLE chat_members;
|
||||
DROP TABLE chats;
|
||||
DROP TABLE users;
|
||||
|
||||
COMMIT;
|
||||
|
||||
@@ -0,0 +1,113 @@
|
||||
BEGIN;
|
||||
|
||||
CREATE TABLE users (
|
||||
id UUID PRIMARY KEY,
|
||||
username VARCHAR(32) NOT NULL,
|
||||
display_name TEXT NOT NULL,
|
||||
password_hash TEXT NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
CONSTRAINT users_username_unique
|
||||
UNIQUE (username),
|
||||
|
||||
CONSTRAINT users_username_format
|
||||
CHECK (username ~ '^[a-z0-9_]{3,32}$'),
|
||||
|
||||
CONSTRAINT users_display_name_length
|
||||
CHECK (char_length(display_name) BETWEEN 1 AND 100),
|
||||
|
||||
CONSTRAINT users_display_name_not_blank
|
||||
CHECK (btrim(display_name) <> ''),
|
||||
|
||||
CONSTRAINT users_password_hash_not_blank
|
||||
CHECK (btrim(password_hash) <> '')
|
||||
);
|
||||
|
||||
CREATE TABLE chats (
|
||||
id UUID PRIMARY KEY,
|
||||
title TEXT NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
CONSTRAINT chats_title_length
|
||||
CHECK (char_length(title) BETWEEN 1 AND 100),
|
||||
|
||||
CONSTRAINT chats_title_not_blank
|
||||
CHECK (btrim(title) <> '')
|
||||
);
|
||||
|
||||
CREATE INDEX chats_created_at_id_idx
|
||||
ON chats (created_at, id);
|
||||
|
||||
CREATE TABLE chat_members (
|
||||
chat_id UUID NOT NULL,
|
||||
user_id UUID NOT NULL,
|
||||
position SMALLINT NOT NULL,
|
||||
|
||||
CONSTRAINT chat_members_primary_key
|
||||
PRIMARY KEY (chat_id, user_id),
|
||||
|
||||
CONSTRAINT chat_members_position_unique
|
||||
UNIQUE (chat_id, position),
|
||||
|
||||
CONSTRAINT chat_members_position_range
|
||||
CHECK (position BETWEEN 0 AND 99),
|
||||
|
||||
CONSTRAINT chat_members_chat_foreign_key
|
||||
FOREIGN KEY (chat_id)
|
||||
REFERENCES chats (id)
|
||||
ON DELETE RESTRICT,
|
||||
|
||||
CONSTRAINT chat_members_user_foreign_key
|
||||
FOREIGN KEY (user_id)
|
||||
REFERENCES users (id)
|
||||
ON DELETE RESTRICT
|
||||
);
|
||||
|
||||
CREATE INDEX chat_members_user_chat_idx
|
||||
ON chat_members (user_id, chat_id);
|
||||
|
||||
CREATE TABLE messages (
|
||||
id UUID PRIMARY KEY,
|
||||
chat_id UUID NOT NULL,
|
||||
sender_id UUID NOT NULL,
|
||||
text TEXT NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
CONSTRAINT messages_text_length
|
||||
CHECK (char_length(text) BETWEEN 1 AND 4000),
|
||||
|
||||
CONSTRAINT messages_text_not_blank
|
||||
CHECK (btrim(text) <> ''),
|
||||
|
||||
CONSTRAINT messages_sender_membership_foreign_key
|
||||
FOREIGN KEY (chat_id, sender_id)
|
||||
REFERENCES chat_members (chat_id, user_id)
|
||||
ON DELETE RESTRICT
|
||||
);
|
||||
|
||||
CREATE INDEX messages_chat_created_at_id_idx
|
||||
ON messages (chat_id, created_at, id);
|
||||
|
||||
CREATE TABLE sessions (
|
||||
token_hash BYTEA PRIMARY KEY,
|
||||
user_id UUID NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
expires_at TIMESTAMPTZ NOT NULL,
|
||||
|
||||
CONSTRAINT sessions_user_foreign_key
|
||||
FOREIGN KEY (user_id)
|
||||
REFERENCES users (id)
|
||||
ON DELETE RESTRICT,
|
||||
|
||||
CONSTRAINT sessions_token_hash_length
|
||||
CHECK (octet_length(token_hash) = 32),
|
||||
|
||||
CONSTRAINT sessions_expiry_after_creation
|
||||
CHECK (expires_at > created_at)
|
||||
);
|
||||
|
||||
CREATE INDEX sessions_expires_at_idx
|
||||
ON sessions (expires_at);
|
||||
|
||||
COMMIT;
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
BEGIN;
|
||||
|
||||
DROP TABLE IF EXISTS message_idempotency;
|
||||
|
||||
COMMIT;
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
BEGIN;
|
||||
|
||||
CREATE TABLE message_idempotency (
|
||||
user_id UUID NOT NULL,
|
||||
method VARCHAR(8) NOT NULL,
|
||||
canonical_path TEXT NOT NULL,
|
||||
idempotency_key VARCHAR(128) NOT NULL,
|
||||
request_hash BYTEA NOT NULL,
|
||||
message_id UUID NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
expires_at TIMESTAMPTZ NOT NULL,
|
||||
|
||||
CONSTRAINT message_idempotency_primary_key
|
||||
PRIMARY KEY (
|
||||
user_id,
|
||||
method,
|
||||
canonical_path,
|
||||
idempotency_key
|
||||
),
|
||||
|
||||
CONSTRAINT message_idempotency_user_foreign_key
|
||||
FOREIGN KEY (user_id)
|
||||
REFERENCES users (id)
|
||||
ON DELETE RESTRICT,
|
||||
|
||||
CONSTRAINT message_idempotency_message_foreign_key
|
||||
FOREIGN KEY (message_id)
|
||||
REFERENCES messages (id)
|
||||
ON DELETE RESTRICT
|
||||
DEFERRABLE INITIALLY DEFERRED,
|
||||
|
||||
CONSTRAINT message_idempotency_message_unique
|
||||
UNIQUE (message_id),
|
||||
|
||||
CONSTRAINT message_idempotency_method_format
|
||||
CHECK (method = 'POST'),
|
||||
|
||||
CONSTRAINT message_idempotency_key_format
|
||||
CHECK (idempotency_key ~ '^[A-Za-z0-9._:-]{1,128}$'),
|
||||
|
||||
CONSTRAINT message_idempotency_path_not_blank
|
||||
CHECK (btrim(canonical_path) <> ''),
|
||||
|
||||
CONSTRAINT message_idempotency_request_hash_length
|
||||
CHECK (octet_length(request_hash) = 32),
|
||||
|
||||
CONSTRAINT message_idempotency_expiry_after_creation
|
||||
CHECK (expires_at >= created_at + INTERVAL '24 hours')
|
||||
);
|
||||
|
||||
CREATE INDEX message_idempotency_expires_at_idx
|
||||
ON message_idempotency (expires_at);
|
||||
|
||||
COMMIT;
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
BEGIN;
|
||||
|
||||
-- Downgrading would destroy the only content of attachment-only messages.
|
||||
-- Refuse instead of silently losing valid lab4 data.
|
||||
DO $$
|
||||
BEGIN
|
||||
IF EXISTS (
|
||||
SELECT 1
|
||||
FROM messages
|
||||
WHERE btrim(text) = ''
|
||||
) THEN
|
||||
RAISE EXCEPTION
|
||||
'cannot downgrade while attachment-only messages exist';
|
||||
END IF;
|
||||
END;
|
||||
$$;
|
||||
|
||||
DROP TRIGGER message_attachments_payload_check ON message_attachments;
|
||||
DROP TRIGGER messages_payload_check ON messages;
|
||||
DROP FUNCTION enforce_message_payload_from_link();
|
||||
DROP FUNCTION enforce_message_payload_from_message();
|
||||
|
||||
DROP TABLE message_attachments;
|
||||
DROP TABLE attachments;
|
||||
|
||||
ALTER TABLE messages
|
||||
DROP CONSTRAINT messages_id_chat_unique,
|
||||
DROP CONSTRAINT messages_text_length;
|
||||
|
||||
ALTER TABLE messages
|
||||
ADD CONSTRAINT messages_text_length
|
||||
CHECK (char_length(text) BETWEEN 1 AND 4000),
|
||||
ADD CONSTRAINT messages_text_not_blank
|
||||
CHECK (btrim(text) <> '');
|
||||
|
||||
COMMIT;
|
||||
|
||||
@@ -0,0 +1,192 @@
|
||||
BEGIN;
|
||||
|
||||
-- Text may be empty only when the same transaction links at least one
|
||||
-- attachment. A deferred constraint trigger below enforces that invariant.
|
||||
ALTER TABLE messages
|
||||
DROP CONSTRAINT messages_text_length,
|
||||
DROP CONSTRAINT messages_text_not_blank;
|
||||
|
||||
ALTER TABLE messages
|
||||
ADD CONSTRAINT messages_text_length
|
||||
CHECK (char_length(text) BETWEEN 0 AND 4000),
|
||||
ADD CONSTRAINT messages_id_chat_unique
|
||||
UNIQUE (id, chat_id);
|
||||
|
||||
CREATE TABLE attachments (
|
||||
id UUID PRIMARY KEY,
|
||||
chat_id UUID NOT NULL,
|
||||
owner_id UUID NOT NULL,
|
||||
object_key TEXT NOT NULL,
|
||||
filename TEXT NOT NULL,
|
||||
content_type VARCHAR(32) NOT NULL,
|
||||
size_bytes BIGINT NOT NULL,
|
||||
sha256 BYTEA NOT NULL,
|
||||
width INTEGER NOT NULL,
|
||||
height INTEGER NOT NULL,
|
||||
status VARCHAR(16) NOT NULL,
|
||||
error_code TEXT,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
CONSTRAINT attachments_id_chat_unique
|
||||
UNIQUE (id, chat_id),
|
||||
|
||||
CONSTRAINT attachments_object_key_unique
|
||||
UNIQUE (object_key),
|
||||
|
||||
CONSTRAINT attachments_owner_membership_foreign_key
|
||||
FOREIGN KEY (chat_id, owner_id)
|
||||
REFERENCES chat_members (chat_id, user_id)
|
||||
ON DELETE RESTRICT,
|
||||
|
||||
CONSTRAINT attachments_object_key_not_blank
|
||||
CHECK (btrim(object_key) <> ''),
|
||||
|
||||
CONSTRAINT attachments_filename_length
|
||||
CHECK (char_length(filename) BETWEEN 1 AND 255),
|
||||
|
||||
CONSTRAINT attachments_filename_safe
|
||||
CHECK (
|
||||
btrim(filename) <> ''
|
||||
AND filename NOT IN ('.', '..')
|
||||
AND position('/' in filename) = 0
|
||||
AND position(chr(92) in filename) = 0
|
||||
AND filename !~ '[[:cntrl:]]'
|
||||
),
|
||||
|
||||
CONSTRAINT attachments_content_type_supported
|
||||
CHECK (content_type IN ('image/png', 'image/jpeg')),
|
||||
|
||||
CONSTRAINT attachments_size_range
|
||||
CHECK (size_bytes BETWEEN 1 AND 10485760),
|
||||
|
||||
CONSTRAINT attachments_sha256_length
|
||||
CHECK (octet_length(sha256) = 32),
|
||||
|
||||
CONSTRAINT attachments_dimensions_positive
|
||||
CHECK (width > 0 AND height > 0),
|
||||
|
||||
CONSTRAINT attachments_pixel_limit
|
||||
CHECK (width::BIGINT * height::BIGINT <= 40000000),
|
||||
|
||||
CONSTRAINT attachments_status_supported
|
||||
CHECK (status IN ('pending', 'ready', 'failed')),
|
||||
|
||||
CONSTRAINT attachments_status_error_consistency
|
||||
CHECK (
|
||||
(status = 'failed' AND error_code IS NOT NULL AND btrim(error_code) <> '')
|
||||
OR
|
||||
(status IN ('pending', 'ready') AND error_code IS NULL)
|
||||
),
|
||||
|
||||
CONSTRAINT attachments_updated_after_creation
|
||||
CHECK (updated_at >= created_at)
|
||||
);
|
||||
|
||||
CREATE INDEX attachments_chat_created_at_id_idx
|
||||
ON attachments (chat_id, created_at, id);
|
||||
|
||||
CREATE INDEX attachments_status_updated_at_idx
|
||||
ON attachments (status, updated_at, id);
|
||||
|
||||
CREATE TABLE message_attachments (
|
||||
message_id UUID NOT NULL,
|
||||
chat_id UUID NOT NULL,
|
||||
attachment_id UUID NOT NULL,
|
||||
position SMALLINT NOT NULL,
|
||||
|
||||
CONSTRAINT message_attachments_primary_key
|
||||
PRIMARY KEY (message_id, attachment_id),
|
||||
|
||||
CONSTRAINT message_attachments_position_unique
|
||||
UNIQUE (message_id, position),
|
||||
|
||||
CONSTRAINT message_attachments_position_range
|
||||
CHECK (position BETWEEN 0 AND 9),
|
||||
|
||||
CONSTRAINT message_attachments_message_foreign_key
|
||||
FOREIGN KEY (message_id, chat_id)
|
||||
REFERENCES messages (id, chat_id)
|
||||
ON DELETE CASCADE,
|
||||
|
||||
CONSTRAINT message_attachments_attachment_foreign_key
|
||||
FOREIGN KEY (attachment_id, chat_id)
|
||||
REFERENCES attachments (id, chat_id)
|
||||
ON DELETE RESTRICT
|
||||
);
|
||||
|
||||
CREATE INDEX message_attachments_attachment_id_idx
|
||||
ON message_attachments (attachment_id, message_id);
|
||||
|
||||
CREATE FUNCTION enforce_message_payload_from_message()
|
||||
RETURNS TRIGGER
|
||||
LANGUAGE plpgsql
|
||||
AS $$
|
||||
BEGIN
|
||||
IF EXISTS (
|
||||
SELECT 1
|
||||
FROM messages AS message
|
||||
WHERE message.id = NEW.id
|
||||
AND btrim(message.text) = ''
|
||||
AND NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM message_attachments AS link
|
||||
WHERE link.message_id = message.id
|
||||
)
|
||||
) THEN
|
||||
RAISE EXCEPTION USING
|
||||
ERRCODE = '23514',
|
||||
CONSTRAINT = 'messages_content_required',
|
||||
MESSAGE = 'message must contain non-blank text or an attachment';
|
||||
END IF;
|
||||
RETURN NULL;
|
||||
END;
|
||||
$$;
|
||||
|
||||
CREATE FUNCTION enforce_message_payload_from_link()
|
||||
RETURNS TRIGGER
|
||||
LANGUAGE plpgsql
|
||||
AS $$
|
||||
DECLARE
|
||||
affected_message_id UUID;
|
||||
BEGIN
|
||||
IF TG_OP = 'DELETE' THEN
|
||||
affected_message_id := OLD.message_id;
|
||||
ELSE
|
||||
affected_message_id := NEW.message_id;
|
||||
END IF;
|
||||
|
||||
IF EXISTS (
|
||||
SELECT 1
|
||||
FROM messages AS message
|
||||
WHERE message.id = affected_message_id
|
||||
AND btrim(message.text) = ''
|
||||
AND NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM message_attachments AS link
|
||||
WHERE link.message_id = message.id
|
||||
)
|
||||
) THEN
|
||||
RAISE EXCEPTION USING
|
||||
ERRCODE = '23514',
|
||||
CONSTRAINT = 'messages_content_required',
|
||||
MESSAGE = 'message must contain non-blank text or an attachment';
|
||||
END IF;
|
||||
RETURN NULL;
|
||||
END;
|
||||
$$;
|
||||
|
||||
CREATE CONSTRAINT TRIGGER messages_payload_check
|
||||
AFTER INSERT OR UPDATE ON messages
|
||||
DEFERRABLE INITIALLY DEFERRED
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION enforce_message_payload_from_message();
|
||||
|
||||
CREATE CONSTRAINT TRIGGER message_attachments_payload_check
|
||||
AFTER INSERT OR UPDATE OR DELETE ON message_attachments
|
||||
DEFERRABLE INITIALLY DEFERRED
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION enforce_message_payload_from_link();
|
||||
|
||||
COMMIT;
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
BEGIN;
|
||||
|
||||
DROP TABLE IF EXISTS attachment_jobs;
|
||||
DROP TABLE IF EXISTS attachment_variants;
|
||||
|
||||
ALTER TABLE attachments
|
||||
DROP CONSTRAINT attachments_status_supported,
|
||||
DROP CONSTRAINT attachments_status_error_consistency,
|
||||
DROP CONSTRAINT attachments_staging_key_not_blank,
|
||||
DROP CONSTRAINT attachments_staging_key_unique,
|
||||
DROP CONSTRAINT attachments_pipeline_version_positive,
|
||||
DROP COLUMN pipeline_version,
|
||||
DROP COLUMN staging_key;
|
||||
|
||||
UPDATE attachments SET status = 'pending' WHERE status IN ('queued', 'processing');
|
||||
|
||||
ALTER TABLE attachments
|
||||
ADD CONSTRAINT attachments_status_supported CHECK (status IN ('pending', 'ready', 'failed')),
|
||||
ADD CONSTRAINT attachments_status_error_consistency CHECK (
|
||||
(status = 'failed' AND error_code IS NOT NULL AND btrim(error_code) <> '')
|
||||
OR (status IN ('pending', 'ready') AND error_code IS NULL)
|
||||
);
|
||||
|
||||
COMMIT;
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
BEGIN;
|
||||
|
||||
ALTER TABLE attachments
|
||||
DROP CONSTRAINT attachments_status_supported,
|
||||
DROP CONSTRAINT attachments_status_error_consistency;
|
||||
|
||||
UPDATE attachments SET status = 'queued' WHERE status = 'pending';
|
||||
|
||||
ALTER TABLE attachments
|
||||
ADD COLUMN staging_key TEXT,
|
||||
ADD COLUMN pipeline_version INTEGER NOT NULL DEFAULT 1,
|
||||
ADD CONSTRAINT attachments_staging_key_unique UNIQUE (staging_key),
|
||||
ADD CONSTRAINT attachments_staging_key_not_blank CHECK (staging_key IS NULL OR btrim(staging_key) <> ''),
|
||||
ADD CONSTRAINT attachments_pipeline_version_positive CHECK (pipeline_version > 0),
|
||||
ADD CONSTRAINT attachments_status_supported CHECK (status IN ('queued', 'processing', 'ready', 'failed')),
|
||||
ADD CONSTRAINT attachments_status_error_consistency CHECK (
|
||||
(status = 'failed' AND error_code IS NOT NULL AND btrim(error_code) <> '')
|
||||
OR (status IN ('queued', 'processing', 'ready') AND error_code IS NULL)
|
||||
);
|
||||
|
||||
CREATE TABLE attachment_variants (
|
||||
attachment_id UUID NOT NULL REFERENCES attachments(id) ON DELETE CASCADE,
|
||||
pipeline_version INTEGER NOT NULL,
|
||||
name VARCHAR(32) NOT NULL,
|
||||
object_key TEXT NOT NULL UNIQUE,
|
||||
content_type VARCHAR(32) NOT NULL,
|
||||
size_bytes BIGINT NOT NULL,
|
||||
width INTEGER NOT NULL,
|
||||
height INTEGER NOT NULL,
|
||||
sha256 BYTEA NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (attachment_id, pipeline_version, name),
|
||||
CONSTRAINT attachment_variants_name_supported CHECK (name IN ('thumbnail', 'crop', 'optimized')),
|
||||
CONSTRAINT attachment_variants_content_type_supported CHECK (content_type IN ('image/jpeg', 'image/png', 'image/webp')),
|
||||
CONSTRAINT attachment_variants_values_valid CHECK (
|
||||
pipeline_version > 0 AND btrim(object_key) <> '' AND size_bytes > 0
|
||||
AND width > 0 AND height > 0 AND octet_length(sha256) = 32
|
||||
)
|
||||
);
|
||||
|
||||
CREATE TABLE attachment_jobs (
|
||||
job_id UUID PRIMARY KEY,
|
||||
attachment_id UUID NOT NULL REFERENCES attachments(id) ON DELETE CASCADE,
|
||||
schema_version INTEGER NOT NULL DEFAULT 1,
|
||||
job_type TEXT NOT NULL DEFAULT 'attachment.process.v1',
|
||||
pipeline_version INTEGER NOT NULL,
|
||||
attempt INTEGER NOT NULL DEFAULT 1,
|
||||
max_attempts INTEGER NOT NULL,
|
||||
status VARCHAR(16) NOT NULL DEFAULT 'queued',
|
||||
error_code TEXT,
|
||||
published_at TIMESTAMPTZ,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
UNIQUE (attachment_id, pipeline_version),
|
||||
CONSTRAINT attachment_jobs_schema_supported CHECK (schema_version = 1 AND job_type = 'attachment.process.v1'),
|
||||
CONSTRAINT attachment_jobs_attempts_valid CHECK (attempt BETWEEN 1 AND max_attempts AND max_attempts > 0),
|
||||
CONSTRAINT attachment_jobs_status_supported CHECK (status IN ('queued', 'processing', 'succeeded', 'failed')),
|
||||
CONSTRAINT attachment_jobs_error_consistency CHECK (
|
||||
(status = 'failed' AND error_code IS NOT NULL AND btrim(error_code) <> '') OR status <> 'failed'
|
||||
)
|
||||
);
|
||||
|
||||
CREATE INDEX attachment_jobs_dispatch_idx ON attachment_jobs (created_at, job_id)
|
||||
WHERE status = 'queued' AND published_at IS NULL;
|
||||
|
||||
COMMIT;
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
BEGIN;
|
||||
|
||||
DROP TABLE IF EXISTS attachment_job_events;
|
||||
DROP INDEX IF EXISTS attachment_jobs_lease_idx;
|
||||
DROP INDEX IF EXISTS attachment_jobs_dispatch_idx;
|
||||
|
||||
ALTER TABLE attachment_jobs
|
||||
DROP CONSTRAINT IF EXISTS attachment_jobs_replay_count_nonnegative,
|
||||
DROP CONSTRAINT IF EXISTS attachment_jobs_lease_consistency,
|
||||
DROP COLUMN IF EXISTS replay_count,
|
||||
DROP COLUMN IF EXISTS leased_by,
|
||||
DROP COLUMN IF EXISTS lease_until,
|
||||
DROP COLUMN IF EXISTS available_at;
|
||||
|
||||
CREATE INDEX attachment_jobs_dispatch_idx
|
||||
ON attachment_jobs (created_at, job_id)
|
||||
WHERE status = 'queued' AND published_at IS NULL;
|
||||
|
||||
COMMIT;
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
BEGIN;
|
||||
|
||||
ALTER TABLE attachment_jobs
|
||||
ADD COLUMN available_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
ADD COLUMN lease_until TIMESTAMPTZ,
|
||||
ADD COLUMN leased_by TEXT,
|
||||
ADD COLUMN replay_count INTEGER NOT NULL DEFAULT 0;
|
||||
|
||||
-- An upgrade can encounter a job left in processing by an older worker that
|
||||
-- had no lease support. Requeue it through the outbox instead of inventing an
|
||||
-- unbounded lease.
|
||||
UPDATE attachment_jobs
|
||||
SET status = 'queued', published_at = NULL, available_at = CURRENT_TIMESTAMP,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE status = 'processing';
|
||||
|
||||
ALTER TABLE attachment_jobs
|
||||
ADD CONSTRAINT attachment_jobs_lease_consistency CHECK (
|
||||
(status = 'processing' AND lease_until IS NOT NULL AND leased_by IS NOT NULL AND btrim(leased_by) <> '')
|
||||
OR (status <> 'processing' AND lease_until IS NULL AND leased_by IS NULL)
|
||||
),
|
||||
ADD CONSTRAINT attachment_jobs_replay_count_nonnegative CHECK (replay_count >= 0);
|
||||
|
||||
DROP INDEX attachment_jobs_dispatch_idx;
|
||||
CREATE INDEX attachment_jobs_dispatch_idx
|
||||
ON attachment_jobs (available_at, created_at, job_id)
|
||||
WHERE status = 'queued' AND published_at IS NULL;
|
||||
|
||||
CREATE INDEX attachment_jobs_lease_idx
|
||||
ON attachment_jobs (lease_until, job_id)
|
||||
WHERE status = 'processing';
|
||||
|
||||
CREATE TABLE attachment_job_events (
|
||||
event_id BIGSERIAL PRIMARY KEY,
|
||||
job_id UUID NOT NULL REFERENCES attachment_jobs(job_id) ON DELETE CASCADE,
|
||||
event_type VARCHAR(32) NOT NULL,
|
||||
previous_status VARCHAR(16) NOT NULL,
|
||||
new_status VARCHAR(16) NOT NULL,
|
||||
attempt INTEGER NOT NULL,
|
||||
error_code TEXT,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
CONSTRAINT attachment_job_events_type_supported CHECK (
|
||||
event_type IN ('retry_scheduled', 'lease_recovered', 'operator_replay', 'permanent_failure', 'completed')
|
||||
),
|
||||
CONSTRAINT attachment_job_events_attempt_positive CHECK (attempt > 0)
|
||||
);
|
||||
|
||||
CREATE INDEX attachment_job_events_job_idx
|
||||
ON attachment_job_events (job_id, created_at, event_id);
|
||||
|
||||
COMMIT;
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
BEGIN;
|
||||
|
||||
DELETE FROM attachment_job_events WHERE event_type IN ('version_upgrade', 'superseded');
|
||||
UPDATE attachment_jobs
|
||||
SET status='failed', error_code='superseded_pipeline',
|
||||
lease_until=NULL, leased_by=NULL, updated_at=CURRENT_TIMESTAMP
|
||||
WHERE status='superseded';
|
||||
|
||||
ALTER TABLE attachment_job_events
|
||||
DROP CONSTRAINT attachment_job_events_type_supported,
|
||||
ADD CONSTRAINT attachment_job_events_type_supported CHECK (
|
||||
event_type IN ('retry_scheduled', 'lease_recovered', 'operator_replay', 'permanent_failure', 'completed')
|
||||
);
|
||||
|
||||
ALTER TABLE attachment_jobs
|
||||
DROP CONSTRAINT attachment_jobs_status_supported,
|
||||
ADD CONSTRAINT attachment_jobs_status_supported
|
||||
CHECK (status IN ('queued', 'processing', 'succeeded', 'failed'));
|
||||
|
||||
COMMIT;
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
BEGIN;
|
||||
|
||||
ALTER TABLE attachment_jobs
|
||||
DROP CONSTRAINT attachment_jobs_status_supported,
|
||||
ADD CONSTRAINT attachment_jobs_status_supported
|
||||
CHECK (status IN ('queued', 'processing', 'succeeded', 'failed', 'superseded'));
|
||||
|
||||
ALTER TABLE attachment_job_events
|
||||
DROP CONSTRAINT attachment_job_events_type_supported,
|
||||
ADD CONSTRAINT attachment_job_events_type_supported CHECK (
|
||||
event_type IN ('retry_scheduled', 'lease_recovered', 'operator_replay',
|
||||
'permanent_failure', 'completed', 'version_upgrade', 'superseded')
|
||||
);
|
||||
|
||||
COMMIT;
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
ALTER TABLE attachment_jobs
|
||||
DROP COLUMN IF EXISTS tracestate,
|
||||
DROP COLUMN IF EXISTS traceparent;
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
ALTER TABLE attachment_jobs
|
||||
ADD COLUMN traceparent varchar(55),
|
||||
ADD COLUMN tracestate varchar(512);
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
DROP TABLE IF EXISTS login_rate_limits;
|
||||
@@ -0,0 +1,17 @@
|
||||
BEGIN;
|
||||
|
||||
CREATE TABLE login_rate_limits (
|
||||
identifier_hash BYTEA PRIMARY KEY,
|
||||
window_started_at TIMESTAMPTZ NOT NULL,
|
||||
attempts INTEGER NOT NULL,
|
||||
|
||||
CONSTRAINT login_rate_limits_hash_length
|
||||
CHECK (octet_length(identifier_hash) = 32),
|
||||
CONSTRAINT login_rate_limits_attempts_positive
|
||||
CHECK (attempts > 0)
|
||||
);
|
||||
|
||||
CREATE INDEX login_rate_limits_window_started_at_idx
|
||||
ON login_rate_limits (window_started_at);
|
||||
|
||||
COMMIT;
|
||||
Reference in New Issue
Block a user