Files
ai-video-fullstack/backend/migrations/versions/20260709_0001_initial_schema.py
Xin Wang 03b532dd09 Implement Alembic for database migrations and update FastAPI initialization
- Integrate Alembic for managing database schema migrations, replacing the previous SQL schema management.
- Update the FastAPI application to synchronize interface definitions at startup.
- Modify the Docker Compose command to run Alembic migrations before starting the API.
- Enhance the Makefile with new commands for database migration and revision management.
- Remove outdated SQL schema and seed files, transitioning to a more dynamic migration approach.
- Add initial migration scripts and configuration for Alembic, ensuring a structured database evolution.
2026-07-09 16:29:10 +08:00

125 lines
7.2 KiB
Python

"""initial schema
Revision ID: 20260709_0001
Revises:
Create Date: 2026-07-09 00:00:00.000000
"""
from collections.abc import Sequence
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
revision: str = "20260709_0001"
down_revision: str | Sequence[str] | None = None
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None
def upgrade() -> None:
op.create_table(
"interface_definitions",
sa.Column("interface_type", sa.String(length=64), nullable=False),
sa.Column("name", sa.String(length=128), nullable=False),
sa.Column("capability", sa.String(length=16), nullable=False),
sa.Column(
"field_schema",
postgresql.JSONB(astext_type=sa.Text()),
server_default=sa.text("'{}'::jsonb"),
nullable=False,
),
sa.Column("enabled", sa.Boolean(), server_default=sa.text("true"), nullable=False),
sa.Column("version", sa.Integer(), server_default=sa.text("1"), nullable=False),
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
sa.PrimaryKeyConstraint("interface_type"),
)
op.create_index("ix_interface_definitions_capability", "interface_definitions", ["capability"])
op.create_table(
"model_resources",
sa.Column("id", sa.String(length=40), nullable=False),
sa.Column("name", sa.String(length=128), server_default="", nullable=False),
sa.Column("capability", sa.String(length=16), nullable=False),
sa.Column("interface_type", sa.String(length=64), nullable=False),
sa.Column("values", postgresql.JSONB(astext_type=sa.Text()), server_default=sa.text("'{}'::jsonb"), nullable=False),
sa.Column("secrets", postgresql.JSONB(astext_type=sa.Text()), server_default=sa.text("'{}'::jsonb"), nullable=False),
sa.Column("support_image_input", sa.Boolean(), server_default=sa.text("false"), nullable=False),
sa.Column("enabled", sa.Boolean(), server_default=sa.text("true"), nullable=False),
sa.Column("is_default", sa.Boolean(), server_default=sa.text("false"), nullable=False),
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
sa.ForeignKeyConstraint(["interface_type"], ["interface_definitions.interface_type"], ondelete="RESTRICT"),
sa.PrimaryKeyConstraint("id"),
)
op.create_index("ix_model_resources_capability", "model_resources", ["capability"])
op.create_index("ix_model_resources_interface_type", "model_resources", ["interface_type"])
op.create_table(
"knowledge_bases",
sa.Column("id", sa.String(length=40), nullable=False),
sa.Column("name", sa.String(length=128), nullable=False),
sa.Column("description", sa.String(length=2048), server_default="", nullable=False),
sa.Column("embedding_model_resource_id", sa.String(length=40), nullable=True),
sa.Column("status", sa.String(length=16), server_default="active", nullable=False),
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
sa.ForeignKeyConstraint(["embedding_model_resource_id"], ["model_resources.id"], ondelete="SET NULL"),
sa.PrimaryKeyConstraint("id"),
)
op.create_index("ix_knowledge_bases_embedding_model_resource_id", "knowledge_bases", ["embedding_model_resource_id"])
op.create_table(
"assistants",
sa.Column("id", sa.String(length=40), nullable=False),
sa.Column("name", sa.String(length=128), nullable=False),
sa.Column("type", sa.String(length=16), server_default="prompt", nullable=False),
sa.Column("runtime_mode", sa.String(length=16), server_default="pipeline", nullable=False),
sa.Column("greeting", sa.String(length=2048), server_default="", nullable=False),
sa.Column("enable_interrupt", sa.Boolean(), server_default=sa.text("true"), nullable=False),
sa.Column("vision_enabled", sa.Boolean(), server_default=sa.text("false"), nullable=False),
sa.Column("vision_model_resource_id", sa.String(length=40), nullable=True),
sa.Column("knowledge_base_id", sa.String(length=40), nullable=True),
sa.Column("prompt", sa.String(length=8192), server_default="", nullable=False),
sa.Column("api_url", sa.String(length=512), server_default="", nullable=False),
sa.Column("api_key", sa.String(length=512), server_default="", nullable=False),
sa.Column("app_id", sa.String(length=128), server_default="", nullable=False),
sa.Column("graph", sa.JSON(), server_default=sa.text("'{}'::json"), nullable=False),
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
sa.ForeignKeyConstraint(["knowledge_base_id"], ["knowledge_bases.id"], ondelete="RESTRICT"),
sa.ForeignKeyConstraint(["vision_model_resource_id"], ["model_resources.id"], ondelete="SET NULL"),
sa.PrimaryKeyConstraint("id"),
)
op.create_index("ix_assistants_type", "assistants", ["type"])
op.create_table(
"assistant_model_bindings",
sa.Column("assistant_id", sa.String(length=40), nullable=False),
sa.Column("capability", sa.String(length=16), nullable=False),
sa.Column("model_resource_id", sa.String(length=40), nullable=False),
sa.Column("config", postgresql.JSONB(astext_type=sa.Text()), server_default=sa.text("'{}'::jsonb"), nullable=False),
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
sa.ForeignKeyConstraint(["assistant_id"], ["assistants.id"], ondelete="CASCADE"),
sa.ForeignKeyConstraint(["model_resource_id"], ["model_resources.id"], ondelete="RESTRICT"),
sa.PrimaryKeyConstraint("assistant_id", "capability"),
)
op.create_index("ix_assistant_model_bindings_model_resource_id", "assistant_model_bindings", ["model_resource_id"])
def downgrade() -> None:
op.drop_index("ix_assistant_model_bindings_model_resource_id", table_name="assistant_model_bindings")
op.drop_table("assistant_model_bindings")
op.drop_index("ix_assistants_type", table_name="assistants")
op.drop_table("assistants")
op.drop_index("ix_knowledge_bases_embedding_model_resource_id", table_name="knowledge_bases")
op.drop_table("knowledge_bases")
op.drop_index("ix_model_resources_interface_type", table_name="model_resources")
op.drop_index("ix_model_resources_capability", table_name="model_resources")
op.drop_table("model_resources")
op.drop_index("ix_interface_definitions_capability", table_name="interface_definitions")
op.drop_table("interface_definitions")