From 4c1d08f8a8727a1de03dfd78c0bcfae4a5d13a07 Mon Sep 17 00:00:00 2001 From: kk10 Date: Sat, 3 Oct 2026 11:40:26 +0300 Subject: [PATCH] feat(db): add postgres, goose migrations and initial schema --- Dockerfile.migrate | 8 ++++++ docker-compose.yml | 47 +++++++++++++++++++++++++++++++++--- migrations/00001_init.sql | 51 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+), 3 deletions(-) create mode 100644 Dockerfile.migrate create mode 100644 migrations/00001_init.sql diff --git a/Dockerfile.migrate b/Dockerfile.migrate new file mode 100644 index 00000000..8bcb4171 --- /dev/null +++ b/Dockerfile.migrate @@ -0,0 +1,8 @@ +FROM golang:1.27.1-alpine3.24 AS build +ENV CGO_ENABLED=0 +RUN go install github.com/pressly/goose/v3/cmd/goose@v3.28.0 + +FROM alpine:3.24 +COPY --from=build /go/bin/goose /usr/local/bin/goose +COPY migrations /migrations +ENTRYPOINT ["sh", "-c", "goose -dir /migrations postgres \"$(cat /run/secrets/database_url)\" up"] diff --git a/docker-compose.yml b/docker-compose.yml index 08adff47..02e19ac0 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,12 +1,53 @@ services: - messenger: + backend: build: context: . dockerfile: Dockerfile - image: mtusi-messenger:lab1 - container_name: mtusi-messenger-lab1 + image: mtusi-messenger:lab2 ports: - "127.0.0.1:8080:${APP_PORT}" env_file: - .env + secrets: + - database_url + depends_on: + migrate: + condition: service_completed_successfully restart: unless-stopped + + postgres: + image: postgres:18.6 + environment: + POSTGRES_USER: messenger + POSTGRES_DB: messenger + POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password + secrets: + - postgres_password + volumes: + - pgdata:/var/lib/postgresql + healthcheck: + test: ["CMD-SHELL", "pg_isready -h 127.0.0.1 -U $${POSTGRES_USER} -d $${POSTGRES_DB}"] + interval: 5s + timeout: 3s + retries: 10 + restart: unless-stopped + + migrate: + build: + context: . + dockerfile: Dockerfile.migrate + secrets: + - database_url + depends_on: + postgres: + condition: service_healthy + restart: "no" + +secrets: + postgres_password: + file: ./secrets/postgres_password.txt + database_url: + file: ./secrets/database_url.txt + +volumes: + pgdata: diff --git a/migrations/00001_init.sql b/migrations/00001_init.sql new file mode 100644 index 00000000..ae4b6415 --- /dev/null +++ b/migrations/00001_init.sql @@ -0,0 +1,51 @@ +-- +goose Up +CREATE TABLE users ( + id uuid PRIMARY KEY, + username text NOT NULL UNIQUE + CONSTRAINT users_username_format CHECK (username ~ '^[a-z0-9_]{3,32}$'), + display_name text NOT NULL + CONSTRAINT users_display_name_not_blank CHECK (display_name ~ '\S'), + password_hash text NOT NULL, + created_at timestamptz NOT NULL DEFAULT now() +); + +CREATE TABLE sessions ( + token_hash text PRIMARY KEY, + user_id uuid NOT NULL REFERENCES users (id) ON DELETE CASCADE, + created_at timestamptz NOT NULL DEFAULT now(), + expires_at timestamptz NOT NULL +); + +CREATE INDEX idx_sessions_expires_at ON sessions (expires_at); + +CREATE TABLE chats ( + id uuid PRIMARY KEY, + title text NOT NULL + CONSTRAINT chats_title_not_blank CHECK (title ~ '\S'), + created_at timestamptz NOT NULL DEFAULT now() +); + +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 CASCADE, + PRIMARY KEY (chat_id, user_id) +); + +CREATE INDEX idx_chat_members_user_id ON chat_members (user_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() +); + +CREATE INDEX idx_messages_chat_history ON messages (chat_id, created_at, id); + +-- +goose Down +DROP TABLE messages; +DROP TABLE chat_members; +DROP TABLE chats; +DROP TABLE sessions; +DROP TABLE users;