Add alembic migrations
Browse files
alembic/README
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
Generic single-database configuration.
|
alembic/env.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from logging.config import fileConfig
|
| 2 |
+
from sqlalchemy import engine_from_config
|
| 3 |
+
from sqlalchemy import pool
|
| 4 |
+
from alembic import context
|
| 5 |
+
import sys
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
|
| 8 |
+
# Add the app directory to path
|
| 9 |
+
sys.path.append(str(Path(__file__).parent.parent))
|
| 10 |
+
|
| 11 |
+
from app.database.base import Base
|
| 12 |
+
from app.database import models_intents
|
| 13 |
+
from app.core.config import settings
|
| 14 |
+
|
| 15 |
+
config = context.config
|
| 16 |
+
if config.config_file_name is not None:
|
| 17 |
+
fileConfig(config.config_file_name)
|
| 18 |
+
|
| 19 |
+
target_metadata = Base.metadata
|
| 20 |
+
|
| 21 |
+
def run_migrations_offline() -> None:
|
| 22 |
+
url = settings.database_url
|
| 23 |
+
context.configure(
|
| 24 |
+
url=url,
|
| 25 |
+
target_metadata=target_metadata,
|
| 26 |
+
literal_binds=True,
|
| 27 |
+
dialect_opts={"paramstyle": "named"},
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
with context.begin_transaction():
|
| 31 |
+
context.run_migrations()
|
| 32 |
+
|
| 33 |
+
def run_migrations_online() -> None:
|
| 34 |
+
configuration = config.get_section(config.config_ini_section)
|
| 35 |
+
configuration["sqlalchemy.url"] = settings.database_url
|
| 36 |
+
connectable = engine_from_config(
|
| 37 |
+
configuration,
|
| 38 |
+
prefix="sqlalchemy.",
|
| 39 |
+
poolclass=pool.NullPool,
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
with connectable.connect() as connection:
|
| 43 |
+
context.configure(
|
| 44 |
+
connection=connection, target_metadata=target_metadata
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
with context.begin_transaction():
|
| 48 |
+
context.run_migrations()
|
| 49 |
+
|
| 50 |
+
if context.is_offline_mode():
|
| 51 |
+
run_migrations_offline()
|
| 52 |
+
else:
|
| 53 |
+
run_migrations_online()
|
alembic/script.py.mako
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""${message}
|
| 2 |
+
|
| 3 |
+
Revision ID: ${up_revision}
|
| 4 |
+
Revises: ${down_revision | comma,n}
|
| 5 |
+
Create Date: ${create_date}
|
| 6 |
+
|
| 7 |
+
"""
|
| 8 |
+
from typing import Sequence, Union
|
| 9 |
+
|
| 10 |
+
from alembic import op
|
| 11 |
+
import sqlalchemy as sa
|
| 12 |
+
${imports if imports else ""}
|
| 13 |
+
|
| 14 |
+
# revision identifiers, used by Alembic.
|
| 15 |
+
revision: str = ${repr(up_revision)}
|
| 16 |
+
down_revision: Union[str, Sequence[str], None] = ${repr(down_revision)}
|
| 17 |
+
branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
|
| 18 |
+
depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def upgrade() -> None:
|
| 22 |
+
"""Upgrade schema."""
|
| 23 |
+
${upgrades if upgrades else "pass"}
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def downgrade() -> None:
|
| 27 |
+
"""Downgrade schema."""
|
| 28 |
+
${downgrades if downgrades else "pass"}
|
alembic/versions/b2218948f541_create_intents_and_intent_outcomes_.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""create intents and intent_outcomes tables
|
| 2 |
+
|
| 3 |
+
Revision ID: b2218948f541
|
| 4 |
+
Revises: c987997c1e9a
|
| 5 |
+
Create Date: 2026-03-08 19:58:46.907147
|
| 6 |
+
|
| 7 |
+
"""
|
| 8 |
+
from typing import Sequence, Union
|
| 9 |
+
|
| 10 |
+
from alembic import op
|
| 11 |
+
import sqlalchemy as sa
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
# revision identifiers, used by Alembic.
|
| 15 |
+
revision: str = 'b2218948f541'
|
| 16 |
+
down_revision: Union[str, Sequence[str], None] = 'c987997c1e9a'
|
| 17 |
+
branch_labels: Union[str, Sequence[str], None] = None
|
| 18 |
+
depends_on: Union[str, Sequence[str], None] = None
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def upgrade() -> None:
|
| 22 |
+
"""Upgrade schema."""
|
| 23 |
+
# ### commands auto generated by Alembic - please adjust! ###
|
| 24 |
+
op.create_table('intents',
|
| 25 |
+
sa.Column('id', sa.Integer(), nullable=False),
|
| 26 |
+
sa.Column('deterministic_id', sa.String(length=64), nullable=False),
|
| 27 |
+
sa.Column('intent_type', sa.String(length=64), nullable=False),
|
| 28 |
+
sa.Column('payload', sa.JSON(), nullable=False),
|
| 29 |
+
sa.Column('oss_payload', sa.JSON(), nullable=True),
|
| 30 |
+
sa.Column('environment', sa.String(length=32), nullable=True),
|
| 31 |
+
sa.Column('created_at', sa.DateTime(), nullable=False),
|
| 32 |
+
sa.Column('evaluated_at', sa.DateTime(), nullable=True),
|
| 33 |
+
sa.Column('risk_score', sa.String(length=32), nullable=True),
|
| 34 |
+
sa.PrimaryKeyConstraint('id')
|
| 35 |
+
)
|
| 36 |
+
op.create_index(op.f('ix_intents_deterministic_id'), 'intents', ['deterministic_id'], unique=True)
|
| 37 |
+
op.create_index(op.f('ix_intents_id'), 'intents', ['id'], unique=False)
|
| 38 |
+
op.create_table('intent_outcomes',
|
| 39 |
+
sa.Column('id', sa.Integer(), nullable=False),
|
| 40 |
+
sa.Column('intent_id', sa.Integer(), nullable=False),
|
| 41 |
+
sa.Column('success', sa.Boolean(), nullable=False),
|
| 42 |
+
sa.Column('recorded_by', sa.String(length=128), nullable=True),
|
| 43 |
+
sa.Column('notes', sa.Text(), nullable=True),
|
| 44 |
+
sa.Column('recorded_at', sa.DateTime(), nullable=False),
|
| 45 |
+
sa.ForeignKeyConstraint(['intent_id'], ['intents.id'], ondelete='CASCADE'),
|
| 46 |
+
sa.PrimaryKeyConstraint('id'),
|
| 47 |
+
sa.UniqueConstraint('intent_id', name='uq_outcome_intentid')
|
| 48 |
+
)
|
| 49 |
+
op.create_index(op.f('ix_intent_outcomes_id'), 'intent_outcomes', ['id'], unique=False)
|
| 50 |
+
# ### end Alembic commands ###
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def downgrade() -> None:
|
| 54 |
+
"""Downgrade schema."""
|
| 55 |
+
# ### commands auto generated by Alembic - please adjust! ###
|
| 56 |
+
op.drop_index(op.f('ix_intent_outcomes_id'), table_name='intent_outcomes')
|
| 57 |
+
op.drop_table('intent_outcomes')
|
| 58 |
+
op.drop_index(op.f('ix_intents_id'), table_name='intents')
|
| 59 |
+
op.drop_index(op.f('ix_intents_deterministic_id'), table_name='intents')
|
| 60 |
+
op.drop_table('intents')
|
| 61 |
+
# ### end Alembic commands ###
|
alembic/versions/c987997c1e9a_init.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""init
|
| 2 |
+
|
| 3 |
+
Revision ID: c987997c1e9a
|
| 4 |
+
Revises:
|
| 5 |
+
Create Date: 2026-03-07 21:33:04.583939
|
| 6 |
+
|
| 7 |
+
"""
|
| 8 |
+
from typing import Sequence, Union
|
| 9 |
+
|
| 10 |
+
from alembic import op
|
| 11 |
+
import sqlalchemy as sa
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
# revision identifiers, used by Alembic.
|
| 15 |
+
revision: str = 'c987997c1e9a'
|
| 16 |
+
down_revision: Union[str, Sequence[str], None] = None
|
| 17 |
+
branch_labels: Union[str, Sequence[str], None] = None
|
| 18 |
+
depends_on: Union[str, Sequence[str], None] = None
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def upgrade() -> None:
|
| 22 |
+
"""Upgrade schema."""
|
| 23 |
+
# ### commands auto generated by Alembic - please adjust! ###
|
| 24 |
+
pass
|
| 25 |
+
# ### end Alembic commands ###
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def downgrade() -> None:
|
| 29 |
+
"""Downgrade schema."""
|
| 30 |
+
# ### commands auto generated by Alembic - please adjust! ###
|
| 31 |
+
pass
|
| 32 |
+
# ### end Alembic commands ###
|