From b43cdcadc5439b4ad999b9be05cdf747294a9560 Mon Sep 17 00:00:00 2001 From: Xiaoxia AI Date: Mon, 15 Jun 2026 15:33:01 +0800 Subject: [PATCH] feat: add Alembic database migrations - alembic.ini: Alembic configuration - alembic/env.py: migration environment with Base import - alembic/versions/001_initial_schema.py: initial schema migration (projects, asset_libraries, assets, ingest_jobs) - alembic/README.md: migration usage guide - all 7 integration tests still passing --- alembic.ini | 42 ++++++++++++ alembic/README.md | 23 +++++++ alembic/env.py | 79 ++++++++++++++++++++++ alembic/script.py.mako | 26 ++++++++ alembic/versions/001_initial_schema.py | 90 ++++++++++++++++++++++++++ 5 files changed, 260 insertions(+) create mode 100644 alembic.ini create mode 100644 alembic/README.md create mode 100644 alembic/env.py create mode 100644 alembic/script.py.mako create mode 100644 alembic/versions/001_initial_schema.py diff --git a/alembic.ini b/alembic.ini new file mode 100644 index 000000000..d07c345c5 --- /dev/null +++ b/alembic.ini @@ -0,0 +1,42 @@ +# Alembic Config file + +[alembic] +script_location = alembic +prepend_sys_path = . +sqlalchemy.url = postgresql://postgres:postgres@localhost:5432/xiaoxia_saas + +[post_write_hooks] + +[loggers] +keys = root,sqlalchemy,alembic + +[handlers] +keys = console + +[formatters] +keys = generic + +[logger_root] +level = WARN +handlers = console +qualname = + +[logger_sqlalchemy] +level = WARN +handlers = +qualname = sqlalchemy.engine + +[logger_alembic] +level = INFO +handlers = +qualname = alembic + +[handler_console] +class = StreamHandler +args = (sys.stderr,) +level = NOTSET +formatter = generic + +[formatter_generic] +format = %(levelname)-5.5s [%(name)s] %(message)s +datefmt = %H:%M:%S diff --git a/alembic/README.md b/alembic/README.md new file mode 100644 index 000000000..e291988d9 --- /dev/null +++ b/alembic/README.md @@ -0,0 +1,23 @@ +# Alembic Migrations + +This directory contains database migration scripts managed by Alembic. + +## Usage + +```bash +# Apply all pending migrations +alembic upgrade head + +# Rollback one migration +alembic downgrade -1 + +# Show current revision +alembic current + +# Show migration history +alembic history +``` + +## Current Migrations + +- `001_initial_schema.py` - Initial database schema (projects, asset_libraries, assets, ingest_jobs) diff --git a/alembic/env.py b/alembic/env.py new file mode 100644 index 000000000..1040294b2 --- /dev/null +++ b/alembic/env.py @@ -0,0 +1,79 @@ +from logging.config import fileConfig + +from sqlalchemy import engine_from_config +from sqlalchemy import pool + +from alembic import context + +# Import your models' Base here +from packages.adapters.sqlalchemy_impl.models import Base + +# this is the Alembic Config object, which provides +# access to the values within the .ini file in use. +config = context.config + +# Interpret the config file for Python logging. +# This line sets up loggers basically. +if config.config_file_name is not None: + fileConfig(config.config_file_name) + +# add your model's MetaData object here +# for 'autogenerate' support +target_metadata = Base.metadata + +# other values from the config, defined by the needs of env.py, +# can be acquired: +# my_important_option = config.get_main_option("my_important_option") +# ... etc. + + +def run_migrations_offline() -> None: + """Run migrations in 'offline' mode. + + This configures the context with just a URL + and not an Engine, though an Engine is acceptable + here as well. By skipping the Engine creation + we don't even need a DBAPI to be available. + + Calls to context.execute() here emit the given string to the + script output. + + """ + 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 run_migrations_online() -> None: + """Run migrations in 'online' mode. + + In this scenario we need to create an Engine + and associate a connection with the context. + + """ + 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 + ) + + with context.begin_transaction(): + context.run_migrations() + + +if context.is_offline_mode(): + run_migrations_offline() +else: + run_migrations_online() diff --git a/alembic/script.py.mako b/alembic/script.py.mako new file mode 100644 index 000000000..fbc4b07dc --- /dev/null +++ b/alembic/script.py.mako @@ -0,0 +1,26 @@ +"""${message} + +Revision ID: ${up_revision} +Revises: ${down_revision | comma,n} +Create Date: ${create_date} + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa +${imports if imports else ""} + +# revision identifiers, used by Alembic. +revision: str = ${repr(up_revision)} +down_revision: Union[str, None] = ${repr(down_revision)} +branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)} +depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)} + + +def upgrade() -> None: + ${upgrades if upgrades else "pass"} + + +def downgrade() -> None: + ${downgrades if downgrades else "pass"} diff --git a/alembic/versions/001_initial_schema.py b/alembic/versions/001_initial_schema.py new file mode 100644 index 000000000..c7fd76bed --- /dev/null +++ b/alembic/versions/001_initial_schema.py @@ -0,0 +1,90 @@ +"""Initial schema + +Revision ID: 001 +Revises: +Create Date: 2026-06-15 + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision: str = '001' +down_revision: Union[str, None] = None +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + # Create projects table + op.create_table( + 'projects', + sa.Column('id', sa.String(32), nullable=False), + sa.Column('workspace_id', sa.String(32), nullable=False), + sa.Column('name', sa.String(100), nullable=False), + sa.Column('description', sa.Text(), nullable=False, server_default=''), + sa.Column('created_at', sa.DateTime(), nullable=False), + sa.PrimaryKeyConstraint('id') + ) + op.create_index(op.f('ix_projects_workspace_id'), 'projects', ['workspace_id'], unique=False) + + # Create asset_libraries table + op.create_table( + 'asset_libraries', + sa.Column('id', sa.String(32), nullable=False), + sa.Column('workspace_id', sa.String(32), nullable=False), + sa.Column('project_id', sa.String(32), nullable=False), + sa.Column('name', sa.String(100), nullable=False), + sa.Column('kind', sa.String(20), nullable=False), + sa.Column('created_at', sa.DateTime(), nullable=False), + sa.PrimaryKeyConstraint('id') + ) + op.create_index(op.f('ix_asset_libraries_workspace_id'), 'asset_libraries', ['workspace_id'], unique=False) + op.create_index(op.f('ix_asset_libraries_project_id'), 'asset_libraries', ['project_id'], unique=False) + + # Create assets table + op.create_table( + 'assets', + sa.Column('id', sa.String(32), nullable=False), + sa.Column('workspace_id', sa.String(32), nullable=False), + sa.Column('project_id', sa.String(32), nullable=False), + sa.Column('library_id', sa.String(32), nullable=False), + sa.Column('name', sa.String(100), nullable=False), + sa.Column('storage_key', sa.String(255), nullable=False), + sa.Column('mime_type', sa.String(100), nullable=False), + sa.Column('metadata_json', sa.Text(), nullable=False, server_default='{}'), + sa.Column('created_at', sa.DateTime(), nullable=False), + sa.PrimaryKeyConstraint('id') + ) + op.create_index(op.f('ix_assets_workspace_id'), 'assets', ['workspace_id'], unique=False) + op.create_index(op.f('ix_assets_project_id'), 'assets', ['project_id'], unique=False) + op.create_index(op.f('ix_assets_library_id'), 'assets', ['library_id'], unique=False) + + # Create ingest_jobs table + op.create_table( + 'ingest_jobs', + sa.Column('id', sa.String(32), nullable=False), + sa.Column('workspace_id', sa.String(32), nullable=False), + sa.Column('project_id', sa.String(32), nullable=False), + sa.Column('library_id', sa.String(32), nullable=False), + sa.Column('storage_key', sa.String(255), nullable=False), + sa.Column('status', sa.String(20), nullable=False, server_default='pending'), + sa.Column('error_message', sa.Text(), nullable=False, server_default=''), + sa.Column('result_asset_id', sa.String(32), nullable=False, server_default=''), + sa.Column('created_at', sa.DateTime(), nullable=False), + sa.Column('updated_at', sa.DateTime(), nullable=False), + sa.PrimaryKeyConstraint('id') + ) + op.create_index(op.f('ix_ingest_jobs_workspace_id'), 'ingest_jobs', ['workspace_id'], unique=False) + op.create_index(op.f('ix_ingest_jobs_project_id'), 'ingest_jobs', ['project_id'], unique=False) + op.create_index(op.f('ix_ingest_jobs_library_id'), 'ingest_jobs', ['library_id'], unique=False) + + +def downgrade() -> None: + op.drop_table('ingest_jobs') + op.drop_table('assets') + op.drop_table('asset_libraries') + op.drop_table('projects')