hoàn thành bước tạo docker compose

This commit is contained in:
2026-03-31 14:47:29 +07:00
parent c362692c51
commit 5d5ec7fb1a
16 changed files with 427 additions and 3 deletions
+58
View File
@@ -0,0 +1,58 @@
"""Alembic environment — wires SQLAlchemy models into the migration engine."""
import os
from logging.config import fileConfig
from alembic import context
from sqlalchemy import engine_from_config, pool
# Load app models so Alembic can inspect metadata
from app.database import Base # noqa: F401 — registers Base.metadata
import app.models # noqa: F401 — registers all ORM classes
# ── Alembic Config object ─────────────────────────────────────────────────────
config = context.config
if config.config_file_name is not None:
fileConfig(config.config_file_name)
# Inject DATABASE_URL from environment so it is never hard-coded
database_url = os.environ["DATABASE_URL"]
config.set_main_option("sqlalchemy.url", database_url)
target_metadata = Base.metadata
# ── Offline migrations (no live DB connection) ────────────────────────────────
def run_migrations_offline() -> None:
context.configure(
url=database_url,
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
compare_type=True,
)
with context.begin_transaction():
context.run_migrations()
# ── Online migrations (live DB connection) ────────────────────────────────────
def run_migrations_online() -> None:
connectable = engine_from_config(
config.get_section(config.config_ini_section, {}),
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)
with connectable.connect() as connection:
context.configure(
connection=connection,
target_metadata=target_metadata,
compare_type=True,
)
with context.begin_transaction():
context.run_migrations()
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()
+17
View File
@@ -0,0 +1,17 @@
"""Alembic migration script template."""
# revision identifiers, used by Alembic.
revision = None
down_revision = None
branch_labels = None
depends_on = None
from alembic import op # noqa
def upgrade() -> None:
pass
def downgrade() -> None:
pass
@@ -0,0 +1,102 @@
"""initial schema: users, pdfs, annotations
Revision ID: 0001
Revises:
Create Date: 2026-03-31
"""
revision = "0001"
down_revision = None
branch_labels = None
depends_on = None
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
def upgrade() -> None:
# ── users ─────────────────────────────────────────────────────────────────
op.create_table(
"users",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("username", sa.String(length=64), nullable=False),
sa.Column("email", sa.String(length=255), nullable=False),
sa.Column("password_hash", sa.String(length=255), nullable=False),
sa.Column(
"created_at",
sa.DateTime(timezone=True),
nullable=False,
server_default=sa.text("now()"),
),
sa.PrimaryKeyConstraint("id"),
)
op.create_index("ix_users_id", "users", ["id"])
op.create_index("ix_users_username", "users", ["username"], unique=True)
op.create_index("ix_users_email", "users", ["email"], unique=True)
# ── pdfs ──────────────────────────────────────────────────────────────────
op.create_table(
"pdfs",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("user_id", sa.Integer(), nullable=False),
sa.Column("title", sa.String(length=255), nullable=False),
sa.Column("file_path", sa.Text(), nullable=False),
sa.Column("total_pages", sa.Integer(), nullable=True),
sa.Column(
"created_at",
sa.DateTime(timezone=True),
nullable=False,
server_default=sa.text("now()"),
),
sa.ForeignKeyConstraint(["user_id"], ["users.id"], ondelete="CASCADE"),
sa.PrimaryKeyConstraint("id"),
)
op.create_index("ix_pdfs_id", "pdfs", ["id"])
op.create_index("ix_pdfs_user_id", "pdfs", ["user_id"])
# ── annotations ───────────────────────────────────────────────────────────
op.create_table(
"annotations",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("pdf_id", sa.Integer(), nullable=False),
sa.Column("user_id", sa.Integer(), nullable=False),
sa.Column("page_number", sa.Integer(), nullable=False),
sa.Column(
"canvas_data",
postgresql.JSONB(astext_type=sa.Text()),
nullable=False,
server_default="{}",
),
sa.Column(
"updated_at",
sa.DateTime(timezone=True),
nullable=False,
server_default=sa.text("now()"),
),
sa.ForeignKeyConstraint(["pdf_id"], ["pdfs.id"], ondelete="CASCADE"),
sa.ForeignKeyConstraint(["user_id"], ["users.id"], ondelete="CASCADE"),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint(
"pdf_id", "user_id", "page_number", name="uq_annotation_pdf_user_page"
),
)
op.create_index("ix_annotations_id", "annotations", ["id"])
op.create_index("ix_annotations_pdf_id", "annotations", ["pdf_id"])
op.create_index("ix_annotations_user_id", "annotations", ["user_id"])
def downgrade() -> None:
op.drop_index("ix_annotations_user_id", table_name="annotations")
op.drop_index("ix_annotations_pdf_id", table_name="annotations")
op.drop_index("ix_annotations_id", table_name="annotations")
op.drop_table("annotations")
op.drop_index("ix_pdfs_user_id", table_name="pdfs")
op.drop_index("ix_pdfs_id", table_name="pdfs")
op.drop_table("pdfs")
op.drop_index("ix_users_email", table_name="users")
op.drop_index("ix_users_username", table_name="users")
op.drop_index("ix_users_id", table_name="users")
op.drop_table("users")