ЛР 4: вложения в S3 и двусторонняя сверка хранилищ
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
-- +goose Up
|
||||
CREATE TABLE users
|
||||
(
|
||||
id uuid PRIMARY KEY NOT NULL DEFAULT gen_random_uuid(),
|
||||
username VARCHAR(32) UNIQUE NOT NULL,
|
||||
display_name VARCHAR(100) NOT NULL,
|
||||
password_hash TEXT NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL,
|
||||
|
||||
CONSTRAINT users_username_format_check CHECK (username COLLATE "C" ~ '^[a-z0-9_]{3,32}$'),
|
||||
CONSTRAINT users_display_name_length_check CHECK (char_length(display_name) BETWEEN 1 AND 100),
|
||||
CONSTRAINT users_display_name_not_blank_check CHECK (char_length(btrim(display_name)) > 0),
|
||||
CONSTRAINT users_password_hash_not_empty_check CHECK (char_length(password_hash) > 0)
|
||||
|
||||
);
|
||||
|
||||
|
||||
|
||||
CREATE TABLE chats
|
||||
(
|
||||
id UUID PRIMARY KEY NOT NULL DEFAULT gen_random_uuid(),
|
||||
title VARCHAR(100) NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL,
|
||||
|
||||
CONSTRAINT chats_title_length_check CHECK (char_length(title) BETWEEN 1 AND 100 ),
|
||||
CONSTRAINT chats_title_not_blank_check CHECK (char_length(btrim(title)) > 0)
|
||||
|
||||
);
|
||||
|
||||
CREATE TABLE chat_members
|
||||
(
|
||||
chat_id UUID NOT NULL REFERENCES chats (id),
|
||||
user_id UUID NOT NULL REFERENCES users (id),
|
||||
|
||||
PRIMARY KEY (chat_id, user_id)
|
||||
);
|
||||
|
||||
CREATE INDEX idx_user_chat_id ON chat_members (user_id, chat_id);
|
||||
|
||||
|
||||
CREATE TABLE messages
|
||||
(
|
||||
id UUID PRIMARY KEY NOT NULL DEFAULT gen_random_uuid(),
|
||||
chat_id UUID NOT NULL,
|
||||
sender_id UUID NOT NULL,
|
||||
text TEXT NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL,
|
||||
|
||||
FOREIGN KEY (chat_id, sender_id)
|
||||
REFERENCES chat_members (chat_id, user_id),
|
||||
|
||||
CONSTRAINT messages_text_length_check CHECK (char_length(text) BETWEEN 1 AND 4000),
|
||||
CONSTRAINT messages_text_not_blank_check CHECK (char_length(btrim(text)) > 0)
|
||||
);
|
||||
|
||||
CREATE INDEX idx_messages_chat_created_id ON messages (chat_id, created_at, id);
|
||||
|
||||
|
||||
CREATE TABLE sessions
|
||||
(
|
||||
token_hash BYTEA PRIMARY KEY NOT NULL,
|
||||
user_id UUID NOT NULL REFERENCES users (id),
|
||||
created_at TIMESTAMPTZ NOT NULL,
|
||||
expires_at TIMESTAMPTZ NOT NULL,
|
||||
revoked_at TIMESTAMPTZ NULL,
|
||||
|
||||
CONSTRAINT sessions_expiry_after_creation_check CHECK (expires_at > created_at)
|
||||
);
|
||||
|
||||
CREATE INDEX idx_session_user_id ON sessions (user_id);
|
||||
|
||||
|
||||
-- +goose Down
|
||||
|
||||
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;
|
||||
@@ -0,0 +1,20 @@
|
||||
-- +goose Up
|
||||
ALTER TABLE users
|
||||
ALTER COLUMN created_at SET DEFAULT now();
|
||||
ALTER TABLE chats
|
||||
ALTER COLUMN created_at SET DEFAULT now();
|
||||
ALTER TABLE messages
|
||||
ALTER COLUMN created_at SET DEFAULT now();
|
||||
ALTER TABLE sessions
|
||||
ALTER COLUMN created_at SET DEFAULT now();
|
||||
|
||||
-- +goose Down
|
||||
|
||||
ALTER TABLE sessions
|
||||
ALTER COLUMN created_at DROP DEFAULT;
|
||||
ALTER TABLE messages
|
||||
ALTER COLUMN created_at DROP DEFAULT;
|
||||
ALTER TABLE chats
|
||||
ALTER COLUMN created_at DROP DEFAULT;
|
||||
ALTER TABLE users
|
||||
ALTER COLUMN created_at DROP DEFAULT;
|
||||
@@ -0,0 +1,17 @@
|
||||
-- +goose Up
|
||||
DROP TABLE IF EXISTS sessions;
|
||||
|
||||
-- +goose Down
|
||||
-- Restore the schema at version 2; deleted session data cannot be recovered.
|
||||
CREATE TABLE sessions
|
||||
(
|
||||
token_hash BYTEA PRIMARY KEY NOT NULL,
|
||||
user_id UUID NOT NULL REFERENCES users (id),
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
expires_at TIMESTAMPTZ NOT NULL,
|
||||
revoked_at TIMESTAMPTZ NULL,
|
||||
|
||||
CONSTRAINT sessions_expiry_after_creation_check CHECK (expires_at > created_at)
|
||||
);
|
||||
|
||||
CREATE INDEX idx_session_user_id ON sessions (user_id);
|
||||
@@ -0,0 +1,23 @@
|
||||
-- +goose Up
|
||||
|
||||
CREATE TABLE message_idempotency
|
||||
(
|
||||
user_id UUID NOT NULL REFERENCES users(id),
|
||||
chat_id UUID NOT NULL REFERENCES chats(id),
|
||||
idempotency_key VARCHAR(128) NOT NULL,
|
||||
message_id UUID NOT NULL REFERENCES messages(id),
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
expires_at TIMESTAMPTZ NOT NULL DEFAULT (now() + interval '24 hours'),
|
||||
|
||||
PRIMARY KEY (user_id, chat_id, idempotency_key),
|
||||
UNIQUE (message_id),
|
||||
|
||||
CONSTRAINT message_idempotency_key_format_check
|
||||
CHECK (idempotency_key COLLATE "C" ~ '^[A-Za-z0-9._:-]{1,128}$'),
|
||||
CONSTRAINT message_idempotency_expiry_check
|
||||
CHECK (expires_at > created_at)
|
||||
);
|
||||
|
||||
-- +goose Down
|
||||
|
||||
DROP TABLE IF EXISTS message_idempotency;
|
||||
@@ -0,0 +1,54 @@
|
||||
-- +goose Up
|
||||
CREATE TABLE attachments
|
||||
(
|
||||
id UUID PRIMARY KEY NOT NULL DEFAULT gen_random_uuid(),
|
||||
chat_id UUID NOT NULL,
|
||||
owner_id UUID NOT NULL,
|
||||
filename text NOT NULL,
|
||||
object_key text UNIQUE NOT NULL,
|
||||
content_type VARCHAR(10) NOT NULL,
|
||||
size_bytes bigint NOT NULL,
|
||||
sha256 text NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
|
||||
CONSTRAINT fk_attachments_chat_member FOREIGN KEY (chat_id, owner_id)
|
||||
REFERENCES chat_members(chat_id, user_id),
|
||||
CONSTRAINT attachments_sha256_format_check CHECK (sha256 COLLATE "C" ~ '^[0-9a-f]{64}$'),
|
||||
CONSTRAINT attachments_content_type_check CHECK (content_type IN ('image/png', 'image/jpeg')),
|
||||
CONSTRAINT attachments_filename_check CHECK (
|
||||
char_length(filename) BETWEEN 1 AND 255
|
||||
AND filename !~ '[/\\]'
|
||||
),
|
||||
CONSTRAINT attachments_size_bytes_check CHECK (size_bytes BETWEEN 1 AND 10485760),
|
||||
CONSTRAINT attachments_id_chat_unique UNIQUE (id, chat_id),
|
||||
CONSTRAINT attachments_object_key_length_check CHECK (char_length(object_key) > 0)
|
||||
|
||||
);
|
||||
|
||||
ALTER TABLE messages
|
||||
ADD CONSTRAINT messages_id_chat_unique UNIQUE (id, chat_id);
|
||||
|
||||
CREATE TABLE messages_attachments
|
||||
(
|
||||
message_id UUID NOT NULL ,
|
||||
attachment_id UUID NOT NULL ,
|
||||
chat_id UUID NOT NULL,
|
||||
position SMALLINT NOT NULL,
|
||||
|
||||
|
||||
CONSTRAINT message_attachments_pk PRIMARY KEY (message_id, attachment_id),
|
||||
CONSTRAINT message_attachments_position_unique UNIQUE (message_id, position),
|
||||
CONSTRAINT message_attachments_position_check CHECK (position BETWEEN 0 AND 9),
|
||||
|
||||
CONSTRAINT message_attachments_message_chat_fk
|
||||
FOREIGN KEY (message_id, chat_id) REFERENCES messages(id, chat_id),
|
||||
CONSTRAINT message_attachments_attachment_chat_fk
|
||||
FOREIGN KEY (attachment_id, chat_id) REFERENCES attachments(id, chat_id)
|
||||
);
|
||||
|
||||
|
||||
-- +goose Down
|
||||
DROP TABLE IF EXISTS messages_attachments;
|
||||
DROP TABLE IF EXISTS attachments;
|
||||
ALTER TABLE messages DROP CONSTRAINT IF EXISTS messages_id_chat_unique;
|
||||
@@ -0,0 +1,18 @@
|
||||
-- +goose Up
|
||||
ALTER TABLE messages
|
||||
DROP CONSTRAINT messages_text_length_check,
|
||||
DROP CONSTRAINT messages_text_not_blank_check;
|
||||
|
||||
ALTER TABLE messages
|
||||
ADD CONSTRAINT messages_text_length_check
|
||||
CHECK (char_length(text) <= 4000);
|
||||
|
||||
-- +goose Down
|
||||
ALTER TABLE messages
|
||||
DROP CONSTRAINT messages_text_length_check;
|
||||
|
||||
ALTER TABLE messages
|
||||
ADD CONSTRAINT messages_text_length_check
|
||||
CHECK (char_length(text) BETWEEN 1 AND 4000),
|
||||
ADD CONSTRAINT messages_text_not_blank_check
|
||||
CHECK (char_length(btrim(text)) > 0);
|
||||
@@ -0,0 +1,9 @@
|
||||
package migrations
|
||||
|
||||
import "embed"
|
||||
|
||||
// Files contains the SQL shipped with this exact migrator build.
|
||||
// Add schema changes as new numbered files; do not edit applied migrations.
|
||||
//
|
||||
//go:embed *.sql
|
||||
var Files embed.FS
|
||||
@@ -0,0 +1,41 @@
|
||||
-- name: CreateAttachment :one
|
||||
INSERT INTO attachments
|
||||
(chat_id, owner_id, filename, object_key, content_type, size_bytes, sha256)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
RETURNING *;
|
||||
|
||||
-- name: GetAttachmentForUser :one
|
||||
SELECT a.*
|
||||
FROM attachments AS a
|
||||
JOIN chat_members AS cm ON cm.chat_id = a.chat_id
|
||||
WHERE a.id = sqlc.arg(attachment_id)
|
||||
AND cm.user_id = sqlc.arg(user_id);
|
||||
|
||||
|
||||
-- name: GetAttachmentsForMessages :many
|
||||
SELECT message_id, attachment_id, position
|
||||
FROM messages_attachments
|
||||
WHERE message_id = ANY(sqlc.arg(message_ids)::uuid[])
|
||||
ORDER BY message_id, position;
|
||||
|
||||
|
||||
-- name: AttachmentExistsByObjectKey :one
|
||||
SELECT EXISTS (
|
||||
SELECT 1 FROM attachments
|
||||
WHERE object_key = sqlc.arg(object_key)
|
||||
);
|
||||
|
||||
-- name: TryLockAttachmentObjectKey :one
|
||||
SELECT pg_try_advisory_xact_lock(
|
||||
hashtextextended(sqlc.arg(object_key)::text, 0)
|
||||
)::boolean AS locked;
|
||||
|
||||
-- name: ListAttachmentKeys :many
|
||||
SELECT id, object_key, created_at
|
||||
FROM attachments
|
||||
WHERE object_key > sqlc.arg(after_key)
|
||||
ORDER BY object_key
|
||||
LIMIT sqlc.arg(page_size);
|
||||
|
||||
-- name: GetAttachmentByID :one
|
||||
SELECT * FROM attachments WHERE id = sqlc.arg(attachment_id);
|
||||
@@ -0,0 +1,3 @@
|
||||
-- name: CreateChatMember :exec
|
||||
INSERT INTO chat_members(chat_id, user_id)
|
||||
VALUES ($1, $2);
|
||||
@@ -0,0 +1,34 @@
|
||||
-- name: CreateChat :one
|
||||
INSERT INTO chats (title)
|
||||
VALUES ($1)
|
||||
RETURNING id, title, created_at;
|
||||
|
||||
|
||||
-- name: GetChatByID :one
|
||||
|
||||
SELECT c.id, c.title, c.created_at, array_agg(cm.user_id)::uuid[] AS member_ids
|
||||
FROM chats c
|
||||
JOIN chat_members cm ON cm.chat_id = c.id
|
||||
WHERE c.id = $1
|
||||
GROUP BY c.id;
|
||||
|
||||
|
||||
-- name: GetChatsByUserID :many
|
||||
SELECT c.id, c.title, c.created_at, array_agg(cm.user_id)::uuid[] AS member_ids
|
||||
FROM chats c
|
||||
JOIN chat_members cm ON cm.chat_id = c.id
|
||||
WHERE EXISTS (SELECT 1
|
||||
FROM chat_members membership
|
||||
WHERE membership.chat_id = c.id
|
||||
AND membership.user_id = sqlc.arg(user_id))
|
||||
AND (
|
||||
sqlc.narg(after_created_at)::timestamptz IS NULL
|
||||
OR (c.created_at, c.id) >
|
||||
(
|
||||
sqlc.narg(after_created_at)::timestamptz,
|
||||
sqlc.narg(after_id)::uuid
|
||||
)
|
||||
)
|
||||
GROUP BY c.id
|
||||
ORDER BY c.created_at ASC, c.id ASC
|
||||
LIMIT sqlc.arg(limit_count);
|
||||
@@ -0,0 +1,13 @@
|
||||
-- name: InsertMessageIdempotency :execrows
|
||||
INSERT INTO message_idempotency(user_id, chat_id, idempotency_key, message_id)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
ON CONFLICT (user_id, chat_id, idempotency_key) DO NOTHING;
|
||||
|
||||
|
||||
-- name: GetIdempotentMessage :one
|
||||
SELECT m.id, m.chat_id, m.sender_id, m.text, m.created_at
|
||||
FROM message_idempotency AS i
|
||||
JOIN messages AS m ON m.id = i.message_id
|
||||
WHERE i.user_id = $1
|
||||
AND i.chat_id = $2
|
||||
AND i.idempotency_key = $3;
|
||||
@@ -0,0 +1,19 @@
|
||||
-- name: CreateMessage :one
|
||||
INSERT INTO messages (chat_id, sender_id, text)
|
||||
VALUES ($1, $2, $3)
|
||||
RETURNING *;
|
||||
|
||||
-- name: GetMessagesByChatID :many
|
||||
|
||||
SELECT id, chat_id, sender_id, text, created_at
|
||||
FROM messages
|
||||
WHERE chat_id = sqlc.arg(chat_id)
|
||||
AND (
|
||||
sqlc.narg(after_created_at)::timestamptz IS NULL
|
||||
OR (created_at, id) >
|
||||
(sqlc.narg(after_created_at)::timestamptz,
|
||||
sqlc.narg(after_id)::uuid
|
||||
)
|
||||
)
|
||||
ORDER BY created_at ASC, id ASC
|
||||
LIMIT sqlc.arg(limit_count);
|
||||
@@ -0,0 +1,11 @@
|
||||
-- name: CreateMessagesAttachments :execrows
|
||||
INSERT INTO messages_attachments (message_id, attachment_id, chat_id, position)
|
||||
SELECT $1, $2, $3, $4
|
||||
FROM attachments
|
||||
WHERE id = $2 AND chat_id = $3;
|
||||
|
||||
-- name: GetMessagesAttachments :many
|
||||
SELECT attachment_id, position
|
||||
FROM messages_attachments
|
||||
WHERE message_id = sqlc.arg(message_id)
|
||||
ORDER BY position ASC;
|
||||
@@ -0,0 +1,21 @@
|
||||
|
||||
-- name: CreateUser :one
|
||||
|
||||
INSERT INTO users (username, display_name, password_hash)
|
||||
VALUES ($1, $2, $3)
|
||||
RETURNING id, username, display_name, password_hash, created_at;
|
||||
|
||||
|
||||
|
||||
-- name: GetUserByID :one
|
||||
|
||||
SELECT id, username, display_name, password_hash, created_at
|
||||
FROM users
|
||||
WHERE id = $1;
|
||||
|
||||
|
||||
|
||||
-- name: GetUserByUsername :one
|
||||
SELECT id, username, display_name, password_hash, created_at
|
||||
FROM users
|
||||
WHERE username = $1;
|
||||
Reference in New Issue
Block a user