init: new structure + fix lint errors

This commit is contained in:
Daniil
2026-02-03 02:15:07 +03:00
commit 67e0f22b4f
89 changed files with 7654 additions and 0 deletions
+67
View File
@@ -0,0 +1,67 @@
from __future__ import annotations
import asyncio
from logging.config import fileConfig
from alembic import context
from sqlalchemy import pool
from sqlalchemy.ext.asyncio import async_engine_from_config
from cpv3.infrastructure.settings import get_settings
from cpv3.db.models import Base # noqa: F401
# Alembic Config object
config = context.config
if config.config_file_name is not None:
fileConfig(config.config_file_name)
target_metadata = Base.metadata
def _set_sqlalchemy_url() -> None:
settings = get_settings()
config.set_main_option("sqlalchemy.url", settings.get_database_url())
def run_migrations_offline() -> None:
_set_sqlalchemy_url()
url = config.get_main_option("sqlalchemy.url")
context.configure(
url=url,
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
)
with context.begin_transaction():
context.run_migrations()
def do_run_migrations(connection) -> None: # type: ignore[no-untyped-def]
context.configure(connection=connection, target_metadata=target_metadata)
with context.begin_transaction():
context.run_migrations()
async def run_migrations_online() -> None:
_set_sqlalchemy_url()
connectable = async_engine_from_config(
config.get_section(config.config_ini_section, {}),
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)
async with connectable.connect() as connection:
await connection.run_sync(do_run_migrations)
await connectable.dispose()
if context.is_offline_mode():
run_migrations_offline()
else:
asyncio.run(run_migrations_online())
+250
View File
@@ -0,0 +1,250 @@
"""initial schema
Revision ID: 0001
Revises:
Create Date: 2026-01-14
"""
from __future__ import annotations
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
# revision identifiers, used by Alembic.
revision = "0001"
down_revision = None
branch_labels = None
depends_on = None
def upgrade() -> None:
op.create_table(
"users",
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True, nullable=False),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("is_active", sa.Boolean(), nullable=False),
sa.Column("username", sa.String(length=150), nullable=False),
sa.Column("email", sa.String(length=254), nullable=False),
sa.Column("password_hash", sa.String(length=255), nullable=False),
sa.Column("first_name", sa.String(length=150), nullable=False),
sa.Column("last_name", sa.String(length=150), nullable=False),
sa.Column("phone_number", sa.String(length=15), nullable=True),
sa.Column("avatar", sa.String(length=2048), nullable=True),
sa.Column("email_verified", sa.Boolean(), nullable=False),
sa.Column("phone_verified", sa.Boolean(), nullable=False),
sa.Column("is_staff", sa.Boolean(), nullable=False),
sa.Column("is_superuser", sa.Boolean(), nullable=False),
sa.Column("date_joined", sa.DateTime(timezone=True), nullable=False),
sa.Column("last_login", sa.DateTime(timezone=True), nullable=True),
sa.UniqueConstraint("username", name="uq_users_username"),
sa.UniqueConstraint("phone_number", name="uq_users_phone_number"),
)
op.create_index("ix_users_username", "users", ["username"], unique=False)
op.create_table(
"projects",
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True, nullable=False),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("is_active", sa.Boolean(), nullable=False),
sa.Column("owner_id", postgresql.UUID(as_uuid=True), nullable=False),
sa.Column("name", sa.String(length=255), nullable=False),
sa.Column("description", sa.Text(), nullable=True),
sa.Column("language", sa.String(length=4), nullable=False),
sa.Column("folder", sa.String(length=1024), nullable=True),
sa.Column("status", sa.String(length=16), nullable=False),
sa.ForeignKeyConstraint(["owner_id"], ["users.id"], ondelete="RESTRICT"),
)
op.create_index("ix_projects_owner_id", "projects", ["owner_id"], unique=False)
op.create_table(
"files",
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True, nullable=False),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("is_active", sa.Boolean(), nullable=False),
sa.Column("project_id", postgresql.UUID(as_uuid=True), nullable=True),
sa.Column("owner_id", postgresql.UUID(as_uuid=True), nullable=True),
sa.Column("original_filename", sa.String(length=255), nullable=False),
sa.Column("path", sa.String(length=1024), nullable=False),
sa.Column("storage_backend", sa.String(length=16), nullable=False),
sa.Column("mime_type", sa.String(length=128), nullable=False),
sa.Column("size_bytes", sa.BigInteger(), nullable=False),
sa.Column("checksum", sa.String(length=64), nullable=True),
sa.Column("file_format", sa.String(length=32), nullable=True),
sa.Column("is_uploaded", sa.Boolean(), nullable=False),
sa.Column("is_deleted", sa.Boolean(), nullable=False),
sa.ForeignKeyConstraint(["project_id"], ["projects.id"], ondelete="RESTRICT"),
sa.ForeignKeyConstraint(["owner_id"], ["users.id"], ondelete="RESTRICT"),
)
op.create_index("ix_files_project_id", "files", ["project_id"], unique=False)
op.create_index("ix_files_owner_id", "files", ["owner_id"], unique=False)
op.create_table(
"media_files",
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True, nullable=False),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("is_active", sa.Boolean(), nullable=False),
sa.Column("owner_id", postgresql.UUID(as_uuid=True), nullable=False),
sa.Column("project_id", postgresql.UUID(as_uuid=True), nullable=True),
sa.Column("duration_seconds", sa.Float(), nullable=False),
sa.Column("frame_rate", sa.Float(), nullable=True),
sa.Column("width", sa.Integer(), nullable=True),
sa.Column("height", sa.Integer(), nullable=True),
sa.Column("probe_json", sa.JSON(), nullable=True),
sa.Column("notes", sa.Text(), nullable=True),
sa.Column("meta", sa.JSON(), nullable=True),
sa.Column("is_deleted", sa.Boolean(), nullable=False),
sa.ForeignKeyConstraint(["owner_id"], ["users.id"], ondelete="RESTRICT"),
sa.ForeignKeyConstraint(["project_id"], ["projects.id"], ondelete="RESTRICT"),
)
op.create_index("ix_media_files_owner_id", "media_files", ["owner_id"], unique=False)
op.create_index("ix_media_files_project_id", "media_files", ["project_id"], unique=False)
op.create_table(
"artifact_media_files",
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True, nullable=False),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("is_active", sa.Boolean(), nullable=False),
sa.Column("project_id", postgresql.UUID(as_uuid=True), nullable=True),
sa.Column("file_id", postgresql.UUID(as_uuid=True), nullable=True),
sa.Column("media_file_id", postgresql.UUID(as_uuid=True), nullable=False),
sa.Column("artifact_type", sa.String(length=32), nullable=False),
sa.Column("is_deleted", sa.Boolean(), nullable=False),
sa.ForeignKeyConstraint(["project_id"], ["projects.id"], ondelete="RESTRICT"),
sa.ForeignKeyConstraint(["file_id"], ["files.id"], ondelete="RESTRICT"),
sa.ForeignKeyConstraint(["media_file_id"], ["media_files.id"], ondelete="RESTRICT"),
)
op.create_index(
"ix_artifact_media_files_project_id", "artifact_media_files", ["project_id"], unique=False
)
op.create_index(
"ix_artifact_media_files_file_id", "artifact_media_files", ["file_id"], unique=False
)
op.create_index(
"ix_artifact_media_files_media_file_id",
"artifact_media_files",
["media_file_id"],
unique=False,
)
op.create_table(
"transcriptions",
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True, nullable=False),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("is_active", sa.Boolean(), nullable=False),
sa.Column("project_id", postgresql.UUID(as_uuid=True), nullable=True),
sa.Column("source_file_id", postgresql.UUID(as_uuid=True), nullable=False),
sa.Column("artifact_id", postgresql.UUID(as_uuid=True), nullable=True),
sa.Column("engine", sa.String(length=32), nullable=False),
sa.Column("language", sa.String(length=3), nullable=True),
sa.Column("document", sa.JSON(), nullable=False),
sa.Column("transcribe_options", sa.JSON(), nullable=True),
sa.ForeignKeyConstraint(["project_id"], ["projects.id"], ondelete="RESTRICT"),
sa.ForeignKeyConstraint(["source_file_id"], ["files.id"], ondelete="RESTRICT"),
sa.ForeignKeyConstraint(["artifact_id"], ["artifact_media_files.id"], ondelete="RESTRICT"),
)
op.create_index("ix_transcriptions_project_id", "transcriptions", ["project_id"], unique=False)
op.create_index(
"ix_transcriptions_source_file_id", "transcriptions", ["source_file_id"], unique=False
)
op.create_index(
"ix_transcriptions_artifact_id", "transcriptions", ["artifact_id"], unique=False
)
op.create_table(
"jobs",
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True, nullable=False),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("is_active", sa.Boolean(), nullable=False),
sa.Column("broker_id", sa.String(length=255), nullable=False),
sa.Column("user_id", postgresql.UUID(as_uuid=True), nullable=True),
sa.Column("project_id", postgresql.UUID(as_uuid=True), nullable=True),
sa.Column("input_data", sa.JSON(), nullable=True),
sa.Column("output_data", sa.JSON(), nullable=True),
sa.Column("status", sa.String(length=16), nullable=False),
sa.Column("job_type", sa.String(length=32), nullable=False),
sa.Column("project_pct", sa.Float(), nullable=True),
sa.Column("error_message", sa.Text(), nullable=True),
sa.Column("current_message", sa.Text(), nullable=True),
sa.Column("started_at", sa.DateTime(timezone=True), nullable=True),
sa.Column("finished_at", sa.DateTime(timezone=True), nullable=True),
sa.ForeignKeyConstraint(["user_id"], ["users.id"], ondelete="RESTRICT"),
sa.ForeignKeyConstraint(["project_id"], ["projects.id"], ondelete="RESTRICT"),
)
op.create_index("ix_jobs_user_id", "jobs", ["user_id"], unique=False)
op.create_index("ix_jobs_project_id", "jobs", ["project_id"], unique=False)
op.create_table(
"job_events",
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True, nullable=False),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("is_active", sa.Boolean(), nullable=False),
sa.Column("job_id", postgresql.UUID(as_uuid=True), nullable=False),
sa.Column("event_type", sa.String(length=64), nullable=False),
sa.Column("payload", sa.JSON(), nullable=True),
sa.ForeignKeyConstraint(["job_id"], ["jobs.id"], ondelete="CASCADE"),
)
op.create_index("ix_job_events_job_id", "job_events", ["job_id"], unique=False)
op.create_table(
"webhooks",
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True, nullable=False),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("is_active", sa.Boolean(), nullable=False),
sa.Column("project_id", postgresql.UUID(as_uuid=True), nullable=True),
sa.Column("user_id", postgresql.UUID(as_uuid=True), nullable=True),
sa.Column("event", sa.String(length=255), nullable=True),
sa.Column("url", sa.String(length=1024), nullable=False),
sa.Column("secret", sa.String(length=255), nullable=True),
sa.ForeignKeyConstraint(["project_id"], ["projects.id"], ondelete="RESTRICT"),
sa.ForeignKeyConstraint(["user_id"], ["users.id"], ondelete="RESTRICT"),
)
op.create_index("ix_webhooks_project_id", "webhooks", ["project_id"], unique=False)
op.create_index("ix_webhooks_user_id", "webhooks", ["user_id"], unique=False)
def downgrade() -> None:
op.drop_index("ix_webhooks_user_id", table_name="webhooks")
op.drop_index("ix_webhooks_project_id", table_name="webhooks")
op.drop_table("webhooks")
op.drop_index("ix_job_events_job_id", table_name="job_events")
op.drop_table("job_events")
op.drop_index("ix_jobs_project_id", table_name="jobs")
op.drop_index("ix_jobs_user_id", table_name="jobs")
op.drop_table("jobs")
op.drop_index("ix_transcriptions_artifact_id", table_name="transcriptions")
op.drop_index("ix_transcriptions_source_file_id", table_name="transcriptions")
op.drop_index("ix_transcriptions_project_id", table_name="transcriptions")
op.drop_table("transcriptions")
op.drop_index("ix_artifact_media_files_media_file_id", table_name="artifact_media_files")
op.drop_index("ix_artifact_media_files_file_id", table_name="artifact_media_files")
op.drop_index("ix_artifact_media_files_project_id", table_name="artifact_media_files")
op.drop_table("artifact_media_files")
op.drop_index("ix_media_files_project_id", table_name="media_files")
op.drop_index("ix_media_files_owner_id", table_name="media_files")
op.drop_table("media_files")
op.drop_index("ix_files_owner_id", table_name="files")
op.drop_index("ix_files_project_id", table_name="files")
op.drop_table("files")
op.drop_index("ix_projects_owner_id", table_name="projects")
op.drop_table("projects")
op.drop_index("ix_users_username", table_name="users")
op.drop_table("users")