feat(app) implemented lab 4

This commit is contained in:
imadev
2026-11-06 19:06:21 +03:00
parent 68968c4a3a
commit b4239f8cf0
63 changed files with 3944 additions and 137 deletions
@@ -0,0 +1,6 @@
DROP TABLE IF EXISTS message_attachments;
DROP TABLE IF EXISTS attachments;
ALTER TABLE messages DROP CONSTRAINT IF EXISTS messages_text_len;
ALTER TABLE messages
ADD CONSTRAINT messages_text_len CHECK (char_length(text) BETWEEN 1 AND 4000);
@@ -0,0 +1,46 @@
ALTER TABLE messages DROP CONSTRAINT IF EXISTS messages_text_len;
ALTER TABLE messages
ADD CONSTRAINT messages_text_len CHECK (char_length(text) BETWEEN 0 AND 4000);
CREATE TABLE IF NOT EXISTS 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,
updated_at timestamptz NOT NULL,
CONSTRAINT attachments_filename_len CHECK (char_length(filename) BETWEEN 1 AND 255),
CONSTRAINT attachments_content_type CHECK (content_type IN ('image/png', 'image/jpeg')),
CONSTRAINT attachments_size CHECK (size_bytes BETWEEN 1 AND 10485760),
CONSTRAINT attachments_sha256_len CHECK (octet_length(sha256) = 32),
CONSTRAINT attachments_status CHECK (status IN ('pending', 'ready', 'failed')),
CONSTRAINT attachments_error_state CHECK (
(status = 'pending' AND error_code IS NULL) OR
(status = 'ready' AND error_code IS NULL) OR
(status = 'failed' AND error_code IS NOT NULL)
),
CONSTRAINT attachments_object_key_key UNIQUE (object_key)
);
CREATE INDEX IF NOT EXISTS attachments_chat_created_idx
ON attachments (chat_id, created_at, id);
CREATE INDEX IF NOT EXISTS attachments_status_created_idx
ON attachments (status, created_at);
CREATE TABLE IF NOT EXISTS 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 integer NOT NULL,
PRIMARY KEY (message_id, attachment_id),
CONSTRAINT message_attachments_position CHECK (position >= 0),
CONSTRAINT message_attachments_position_key UNIQUE (message_id, position)
);
CREATE INDEX IF NOT EXISTS message_attachments_attachment_idx
ON message_attachments (attachment_id);
+56 -21
View File
@@ -3,27 +3,59 @@ package database
import (
"errors"
"io/fs"
"regexp"
"strconv"
"testing"
"github.com/golang-migrate/migrate/v4/source/iofs"
)
func TestEmbeddedMigrationsParse(t *testing.T) {
// latestMigrationVersion is the only value to bump when a migration is added.
// The rest of the test derives filenames and ordering from the directory.
const latestMigrationVersion = 3
var migrationFilePattern = regexp.MustCompile(`^(\d{4})_[a-z0-9_]+\.(up|down)\.sql$`)
func TestEmbeddedMigrationsAreCompleteAndContiguous(t *testing.T) {
entries, err := fs.ReadDir(migrationFiles, "migrations")
if err != nil {
t.Fatalf("read embedded migrations: %v", err)
}
names := make(map[string]bool, len(entries))
up := make(map[int]string, latestMigrationVersion)
down := make(map[int]string, latestMigrationVersion)
for _, entry := range entries {
names[entry.Name()] = true
if entry.IsDir() {
continue
}
name := entry.Name()
match := migrationFilePattern.FindStringSubmatch(name)
if match == nil {
t.Fatalf("migration %q does not match NNNN_name.{up,down}.sql", name)
}
version, parseErr := strconv.Atoi(match[1])
if parseErr != nil || version < 1 || version > latestMigrationVersion {
t.Fatalf("migration %q has version outside 1..%d", name, latestMigrationVersion)
}
files := up
if match[2] == "down" {
files = down
}
if previous, ok := files[version]; ok {
t.Fatalf("version %d has two %s files: %q and %q", version, match[2], previous, name)
}
files[version] = name
}
for _, want := range []string{
"0001_init.up.sql", "0001_init.down.sql",
"0002_message_idempotency.up.sql", "0002_message_idempotency.down.sql",
} {
if !names[want] {
t.Fatalf("migration %q is missing from the embedded filesystem", want)
for version := 1; version <= latestMigrationVersion; version++ {
if up[version] == "" {
t.Fatalf("version %d is missing an up migration", version)
}
if down[version] == "" {
t.Fatalf("version %d is missing a down migration", version)
}
}
@@ -37,17 +69,20 @@ func TestEmbeddedMigrationsParse(t *testing.T) {
if err != nil {
t.Fatalf("read first migration: %v", err)
}
if version != 1 {
t.Fatalf("first migration version = %d, want 1", version)
}
version, err = source.Next(version)
if err != nil {
t.Fatalf("read second migration: %v", err)
}
if version != 2 {
t.Fatalf("second migration version = %d, want 2", version)
}
if _, err := source.Next(version); !errors.Is(err, fs.ErrNotExist) {
t.Fatalf("expected 0002 to be the last migration, got %v", err)
for expected := 1; expected <= latestMigrationVersion; expected++ {
if version != uint(expected) {
t.Fatalf("migration order = %d, want %d", version, expected)
}
next, nextErr := source.Next(version)
if expected == latestMigrationVersion {
if !errors.Is(nextErr, fs.ErrNotExist) {
t.Fatalf("expected %d to be the last migration, got %v", version, nextErr)
}
break
}
if nextErr != nil {
t.Fatalf("read migration after %d: %v", version, nextErr)
}
version = next
}
}