forked from mtusi2026/mtusi2026lab6
chore: add lab5 files and merge env.example.
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
-- +goose Up
|
||||
-- +goose StatementBegin
|
||||
CREATE TABLE users (
|
||||
id UUID PRIMARY KEY,
|
||||
username TEXT NOT NULL,
|
||||
display_name TEXT NOT NULL,
|
||||
password_hash TEXT NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
CONSTRAINT users_username_format
|
||||
CHECK (username ~ '^[a-z0-9_]{3,32}$'),
|
||||
CONSTRAINT users_display_name_not_blank
|
||||
CHECK (display_name !~ '^[[:space:]]*$'),
|
||||
CONSTRAINT users_display_name_len
|
||||
CHECK (char_length(display_name) <= 100),
|
||||
CONSTRAINT users_password_hash_not_blank
|
||||
CHECK (length(password_hash) > 0)
|
||||
);
|
||||
CREATE UNIQUE INDEX users_username_uniq ON users (username);
|
||||
|
||||
CREATE TABLE chats (
|
||||
id UUID PRIMARY KEY,
|
||||
title TEXT NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
CONSTRAINT chats_title_not_blank CHECK (title !~ '^[[:space:]]*$'),
|
||||
CONSTRAINT chats_title_len CHECK (char_length(title) <= 100)
|
||||
);
|
||||
CREATE INDEX chats_created_id_idx ON chats (created_at, id);
|
||||
|
||||
CREATE TABLE chat_members (
|
||||
chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
|
||||
user_id UUID NOT NULL REFERENCES users(id) ON DELETE RESTRICT,
|
||||
joined_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
PRIMARY KEY (chat_id, user_id)
|
||||
);
|
||||
CREATE INDEX chat_members_user_idx ON chat_members (user_id, chat_id);
|
||||
|
||||
CREATE TABLE 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 DEFAULT now(),
|
||||
CONSTRAINT messages_text_not_blank CHECK (text !~ '^[[:space:]]*$'),
|
||||
CONSTRAINT messages_text_len CHECK (char_length(text) <= 4000)
|
||||
);
|
||||
CREATE INDEX messages_chat_created_id_idx
|
||||
ON messages (chat_id, created_at, id);
|
||||
|
||||
CREATE TABLE sessions (
|
||||
token_hash BYTEA PRIMARY KEY,
|
||||
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
expires_at TIMESTAMPTZ NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
CONSTRAINT sessions_token_hash_len CHECK (octet_length(token_hash) = 32)
|
||||
);
|
||||
CREATE INDEX sessions_user_idx ON sessions (user_id);
|
||||
CREATE INDEX sessions_expires_idx ON sessions (expires_at);
|
||||
-- +goose StatementEnd
|
||||
|
||||
-- +goose Down
|
||||
-- +goose StatementBegin
|
||||
DROP TABLE IF EXISTS sessions;
|
||||
DROP TABLE IF EXISTS messages;
|
||||
DROP TABLE IF EXISTS chat_members;
|
||||
DROP TABLE IF EXISTS chats;
|
||||
DROP TABLE IF EXISTS users;
|
||||
-- +goose StatementEnd
|
||||
@@ -0,0 +1,35 @@
|
||||
-- +goose Up
|
||||
-- +goose StatementBegin
|
||||
CREATE TABLE idempotency_keys (
|
||||
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
method TEXT NOT NULL,
|
||||
path TEXT NOT NULL,
|
||||
key TEXT NOT NULL,
|
||||
request_hash BYTEA NOT NULL,
|
||||
response_status INT NOT NULL,
|
||||
response_body BYTEA NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
expires_at TIMESTAMPTZ NOT NULL,
|
||||
|
||||
CONSTRAINT idempotency_keys_pkey
|
||||
PRIMARY KEY (user_id, method, path, key),
|
||||
CONSTRAINT idempotency_keys_key_len
|
||||
CHECK (char_length(key) BETWEEN 1 AND 128),
|
||||
CONSTRAINT idempotency_keys_method_len
|
||||
CHECK (char_length(method) BETWEEN 1 AND 16),
|
||||
CONSTRAINT idempotency_keys_path_len
|
||||
CHECK (char_length(path) BETWEEN 1 AND 2048),
|
||||
CONSTRAINT idempotency_keys_hash_len
|
||||
CHECK (octet_length(request_hash) = 32),
|
||||
CONSTRAINT idempotency_keys_status_range
|
||||
CHECK (response_status BETWEEN 100 AND 599)
|
||||
);
|
||||
|
||||
CREATE INDEX idempotency_keys_expires_idx
|
||||
ON idempotency_keys (expires_at);
|
||||
-- +goose StatementEnd
|
||||
|
||||
-- +goose Down
|
||||
-- +goose StatementBegin
|
||||
DROP TABLE IF EXISTS idempotency_keys;
|
||||
-- +goose StatementEnd
|
||||
@@ -0,0 +1,73 @@
|
||||
-- +goose Up
|
||||
-- +goose StatementBegin
|
||||
|
||||
CREATE TABLE 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 DEFAULT now(),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
|
||||
CONSTRAINT attachments_filename_len
|
||||
CHECK (char_length(filename) BETWEEN 1 AND 255),
|
||||
CONSTRAINT attachments_filename_no_slash
|
||||
CHECK (filename !~ '[/\\]'),
|
||||
CONSTRAINT attachments_filename_no_ctrl
|
||||
CHECK (filename !~ '[[:cntrl:]]'),
|
||||
CONSTRAINT attachments_content_type_chk
|
||||
CHECK (content_type IN ('image/png', 'image/jpeg')),
|
||||
CONSTRAINT attachments_size_chk
|
||||
CHECK (size_bytes > 0 AND size_bytes <= 10485760),
|
||||
CONSTRAINT attachments_sha256_len
|
||||
CHECK (octet_length(sha256) = 32),
|
||||
CONSTRAINT attachments_object_key_len
|
||||
CHECK (char_length(object_key) BETWEEN 1 AND 1024),
|
||||
CONSTRAINT attachments_status_chk
|
||||
CHECK (status IN ('ready', 'pending', 'failed')),
|
||||
CONSTRAINT attachments_error_code_consistency
|
||||
CHECK (
|
||||
(status IN ('ready', 'pending') AND error_code IS NULL) OR
|
||||
(status = 'failed' AND error_code IS NOT NULL)
|
||||
)
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX attachments_object_key_uniq
|
||||
ON attachments (object_key);
|
||||
|
||||
CREATE INDEX attachments_chat_idx
|
||||
ON attachments (chat_id, created_at, id);
|
||||
|
||||
CREATE INDEX attachments_pending_idx
|
||||
ON attachments (created_at)
|
||||
WHERE status = 'pending';
|
||||
|
||||
CREATE TABLE 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 SMALLINT NOT NULL,
|
||||
|
||||
CONSTRAINT message_attachments_pkey
|
||||
PRIMARY KEY (message_id, attachment_id),
|
||||
CONSTRAINT message_attachments_position_range
|
||||
CHECK (position >= 0 AND position < 10),
|
||||
CONSTRAINT message_attachments_position_uniq
|
||||
UNIQUE (message_id, position)
|
||||
);
|
||||
|
||||
CREATE INDEX message_attachments_attachment_idx
|
||||
ON message_attachments (attachment_id);
|
||||
|
||||
-- +goose StatementEnd
|
||||
|
||||
-- +goose Down
|
||||
-- +goose StatementBegin
|
||||
DROP TABLE IF EXISTS message_attachments;
|
||||
DROP TABLE IF EXISTS attachments;
|
||||
-- +goose StatementEnd
|
||||
@@ -0,0 +1,13 @@
|
||||
-- +goose Up
|
||||
-- +goose StatementBegin
|
||||
ALTER TABLE messages DROP CONSTRAINT IF EXISTS messages_text_not_blank;
|
||||
ALTER TABLE messages ADD CONSTRAINT messages_text_not_blank
|
||||
CHECK (text !~ '^[[:space:]]+$');
|
||||
-- +goose StatementEnd
|
||||
|
||||
-- +goose Down
|
||||
-- +goose StatementBegin
|
||||
ALTER TABLE messages DROP CONSTRAINT IF EXISTS messages_text_not_blank;
|
||||
ALTER TABLE messages ADD CONSTRAINT messages_text_not_blank
|
||||
CHECK (text !~ '^[[:space:]]*$');
|
||||
-- +goose StatementEnd
|
||||
@@ -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
|
||||
@@ -0,0 +1,32 @@
|
||||
-- +goose Up
|
||||
-- +goose StatementBegin
|
||||
|
||||
CREATE TABLE outbox (
|
||||
id UUID PRIMARY KEY,
|
||||
aggregate_type TEXT NOT NULL,
|
||||
aggregate_id UUID NOT NULL,
|
||||
event_type TEXT NOT NULL,
|
||||
payload JSONB NOT NULL,
|
||||
attempts INT NOT NULL DEFAULT 0,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
published_at TIMESTAMPTZ,
|
||||
|
||||
CONSTRAINT outbox_aggregate_type_len
|
||||
CHECK (char_length(aggregate_type) BETWEEN 1 AND 64),
|
||||
CONSTRAINT outbox_event_type_len
|
||||
CHECK (char_length(event_type) BETWEEN 1 AND 64),
|
||||
CONSTRAINT outbox_attempts_nonneg
|
||||
CHECK (attempts >= 0)
|
||||
);
|
||||
|
||||
CREATE INDEX outbox_pending_idx
|
||||
ON outbox (created_at)
|
||||
WHERE published_at IS NULL;
|
||||
|
||||
-- +goose StatementEnd
|
||||
|
||||
|
||||
-- +goose Down
|
||||
-- +goose StatementBegin
|
||||
DROP TABLE IF EXISTS outbox;
|
||||
-- +goose StatementEnd
|
||||
@@ -0,0 +1,43 @@
|
||||
-- +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 ('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 ('queued', 'processing', 'ready') AND error_code IS NULL) OR
|
||||
(status = 'failed' AND error_code IS NOT NULL)
|
||||
);
|
||||
|
||||
DROP INDEX IF EXISTS attachments_pending_idx;
|
||||
DROP INDEX IF EXISTS attachments_active_idx;
|
||||
CREATE INDEX attachments_active_idx
|
||||
ON attachments (created_at)
|
||||
WHERE status IN ('queued', 'processing');
|
||||
|
||||
-- +goose StatementEnd
|
||||
|
||||
|
||||
-- +goose Down
|
||||
-- +goose StatementBegin
|
||||
|
||||
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_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
|
||||
@@ -0,0 +1,26 @@
|
||||
-- +goose Up
|
||||
-- +goose StatementBegin
|
||||
|
||||
CREATE TABLE attachment_variants (
|
||||
attachment_id UUID NOT NULL REFERENCES attachments(id) ON DELETE CASCADE,
|
||||
name TEXT NOT NULL,
|
||||
object_key TEXT NOT NULL,
|
||||
content_type TEXT NOT NULL,
|
||||
size_bytes BIGINT NOT NULL,
|
||||
width INT NOT NULL,
|
||||
height INT NOT NULL,
|
||||
sha256 BYTEA NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
|
||||
CONSTRAINT attachment_variants_pkey PRIMARY KEY (attachment_id, name),
|
||||
CONSTRAINT attachment_variants_name_chk CHECK (name IN ('thumbnail', 'crop', 'optimized')),
|
||||
CONSTRAINT attachment_variants_size_chk CHECK (size_bytes > 0),
|
||||
CONSTRAINT attachment_variants_sha256_len CHECK (octet_length(sha256) = 32)
|
||||
);
|
||||
|
||||
-- +goose StatementEnd
|
||||
|
||||
-- +goose Down
|
||||
-- +goose StatementBegin
|
||||
DROP TABLE IF EXISTS attachment_variants;
|
||||
-- +goose StatementEnd
|
||||
@@ -0,0 +1,25 @@
|
||||
-- +goose Up
|
||||
-- +goose StatementBegin
|
||||
|
||||
CREATE TABLE failed_jobs (
|
||||
id UUID PRIMARY KEY,
|
||||
attachment_id UUID NOT NULL REFERENCES attachments(id) ON DELETE CASCADE,
|
||||
reason_code TEXT NOT NULL,
|
||||
attempts INT NOT NULL,
|
||||
last_error_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
resolved_at TIMESTAMPTZ,
|
||||
resolution TEXT,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
|
||||
CONSTRAINT failed_jobs_reason_len CHECK (char_length(reason_code) BETWEEN 1 AND 64),
|
||||
CONSTRAINT failed_jobs_attempts_pos CHECK (attempts > 0)
|
||||
);
|
||||
|
||||
CREATE INDEX failed_jobs_unresolved_idx ON failed_jobs (created_at) WHERE resolved_at IS NULL;
|
||||
|
||||
-- +goose StatementEnd
|
||||
|
||||
-- +goose Down
|
||||
-- +goose StatementBegin
|
||||
DROP TABLE IF EXISTS failed_jobs;
|
||||
-- +goose StatementEnd
|
||||
@@ -0,0 +1,6 @@
|
||||
package migrations
|
||||
|
||||
import "embed"
|
||||
|
||||
//go:embed *.sql
|
||||
var FS embed.FS
|
||||
Reference in New Issue
Block a user