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.
This commit is contained in:
5
backend/migrations/README
Normal file
5
backend/migrations/README
Normal file
@@ -0,0 +1,5 @@
|
||||
Alembic migrations for the backend database.
|
||||
|
||||
Run from backend/:
|
||||
|
||||
alembic upgrade head
|
||||
74
backend/migrations/env.py
Normal file
74
backend/migrations/env.py
Normal file
@@ -0,0 +1,74 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from logging.config import fileConfig
|
||||
|
||||
from alembic import context
|
||||
from sqlalchemy import pool
|
||||
from sqlalchemy.engine import Connection
|
||||
from sqlalchemy.ext.asyncio import async_engine_from_config
|
||||
|
||||
import config as app_config
|
||||
from db.models import Base
|
||||
|
||||
config = context.config
|
||||
|
||||
if config.config_file_name is not None:
|
||||
fileConfig(config.config_file_name)
|
||||
|
||||
target_metadata = Base.metadata
|
||||
|
||||
|
||||
def get_url() -> str:
|
||||
return app_config.DATABASE_URL
|
||||
|
||||
|
||||
def run_migrations_offline() -> None:
|
||||
context.configure(
|
||||
url=get_url(),
|
||||
target_metadata=target_metadata,
|
||||
literal_binds=True,
|
||||
dialect_opts={"paramstyle": "named"},
|
||||
compare_type=True,
|
||||
)
|
||||
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
def do_run_migrations(connection: Connection) -> None:
|
||||
context.configure(
|
||||
connection=connection,
|
||||
target_metadata=target_metadata,
|
||||
compare_type=True,
|
||||
)
|
||||
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
async def run_async_migrations() -> None:
|
||||
configuration = config.get_section(config.config_ini_section, {})
|
||||
configuration["sqlalchemy.url"] = get_url()
|
||||
|
||||
connectable = async_engine_from_config(
|
||||
configuration,
|
||||
prefix="sqlalchemy.",
|
||||
poolclass=pool.NullPool,
|
||||
)
|
||||
|
||||
async with connectable.connect() as connection:
|
||||
await connection.run_sync(do_run_migrations)
|
||||
|
||||
await connectable.dispose()
|
||||
|
||||
|
||||
def run_migrations_online() -> None:
|
||||
import asyncio
|
||||
|
||||
asyncio.run(run_async_migrations())
|
||||
|
||||
|
||||
if context.is_offline_mode():
|
||||
run_migrations_offline()
|
||||
else:
|
||||
run_migrations_online()
|
||||
26
backend/migrations/script.py.mako
Normal file
26
backend/migrations/script.py.mako
Normal file
@@ -0,0 +1,26 @@
|
||||
"""${message}
|
||||
|
||||
Revision ID: ${up_revision}
|
||||
Revises: ${down_revision | comma,n}
|
||||
Create Date: ${create_date}
|
||||
|
||||
"""
|
||||
from collections.abc import Sequence
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
${imports if imports else ""}
|
||||
|
||||
|
||||
revision: str = ${repr(up_revision)}
|
||||
down_revision: str | Sequence[str] | None = ${repr(down_revision)}
|
||||
branch_labels: str | Sequence[str] | None = ${repr(branch_labels)}
|
||||
depends_on: str | Sequence[str] | None = ${repr(depends_on)}
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
${upgrades if upgrades else "pass"}
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
${downgrades if downgrades else "pass"}
|
||||
124
backend/migrations/versions/20260709_0001_initial_schema.py
Normal file
124
backend/migrations/versions/20260709_0001_initial_schema.py
Normal file
@@ -0,0 +1,124 @@
|
||||
"""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")
|
||||
Reference in New Issue
Block a user