Implement lab 3 level 4 failover and idempotency

This commit is contained in:
2026-10-29 23:35:04 +03:00
parent 0ab3a341aa
commit a2cb1ad2ae
338 changed files with 37821 additions and 22 deletions
+80
View File
@@ -0,0 +1,80 @@
-- +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;
+9
View File
@@ -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
+4
View File
@@ -0,0 +1,4 @@
-- name: CreateChatMember :exec
INSERT INTO chat_members(chat_id, user_id)
VALUES ($1, $2);
+34
View File
@@ -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);
+13
View File
@@ -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;
+20
View File
@@ -0,0 +1,20 @@
-- 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);
+21
View File
@@ -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;