Add SQLAlchemy persistence layer
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from logging.config import fileConfig
|
||||
|
||||
from alembic import context
|
||||
from sqlalchemy import engine_from_config, pool
|
||||
|
||||
from ai_orchestrator.infrastructure.storage.sqlalchemy import Base
|
||||
|
||||
config = context.config
|
||||
|
||||
if config.config_file_name is not None:
|
||||
fileConfig(config.config_file_name)
|
||||
|
||||
target_metadata = Base.metadata
|
||||
|
||||
|
||||
def run_migrations_offline() -> None:
|
||||
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:
|
||||
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()
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
"""${message}
|
||||
|
||||
Revision ID: ${up_revision}
|
||||
Revises: ${down_revision | comma,n}
|
||||
Create Date: ${create_date}
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
${imports if imports else ""}
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = ${repr(up_revision)}
|
||||
down_revision = ${repr(down_revision)}
|
||||
branch_labels = ${repr(branch_labels)}
|
||||
depends_on = ${repr(depends_on)}
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
${upgrades if upgrades else "pass"}
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
${downgrades if downgrades else "pass"}
|
||||
|
||||
@@ -0,0 +1,143 @@
|
||||
"""Initial schema.
|
||||
|
||||
Revision ID: 0001_initial_schema
|
||||
Revises:
|
||||
Create Date: 2026-07-03 00:00:00
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
revision = "0001_initial_schema"
|
||||
down_revision = None
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.create_table(
|
||||
"tasks",
|
||||
sa.Column("task_id", sa.String(length=64), primary_key=True),
|
||||
sa.Column("project_id", sa.String(length=128), nullable=False),
|
||||
sa.Column("conversation_id", sa.String(length=128), nullable=True),
|
||||
sa.Column("goal", sa.Text(), nullable=False),
|
||||
sa.Column("inputs_json", sa.JSON(), nullable=False),
|
||||
sa.Column("status", sa.String(length=64), nullable=False),
|
||||
sa.Column("requested_mode", sa.String(length=64), nullable=False),
|
||||
sa.Column("effective_mode", sa.String(length=64), nullable=False),
|
||||
sa.Column("current_node_id", sa.String(length=64), nullable=True),
|
||||
sa.Column("result_summary_json", sa.JSON(), nullable=False),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
|
||||
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
|
||||
)
|
||||
op.create_index("ix_tasks_project_id", "tasks", ["project_id"])
|
||||
op.create_index("ix_tasks_conversation_id", "tasks", ["conversation_id"])
|
||||
op.create_index("ix_tasks_status", "tasks", ["status"])
|
||||
op.create_index("ix_tasks_updated_at", "tasks", ["updated_at"])
|
||||
|
||||
op.create_table(
|
||||
"task_graphs",
|
||||
sa.Column("task_id", sa.String(length=64), primary_key=True),
|
||||
sa.Column("graph_version", sa.Integer(), nullable=False),
|
||||
sa.Column("graph_metadata_json", sa.JSON(), nullable=False),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
|
||||
)
|
||||
|
||||
op.create_table(
|
||||
"task_nodes",
|
||||
sa.Column("node_id", sa.String(length=64), primary_key=True),
|
||||
sa.Column("task_id", sa.String(length=64), nullable=False),
|
||||
sa.Column("node_type", sa.String(length=64), nullable=False),
|
||||
sa.Column("status", sa.String(length=64), nullable=False),
|
||||
sa.Column("input_json", sa.JSON(), nullable=False),
|
||||
sa.Column("output_json", sa.JSON(), nullable=False),
|
||||
sa.Column("dependencies_json", sa.JSON(), nullable=False),
|
||||
sa.Column("assigned_runner", sa.String(length=128), nullable=True),
|
||||
sa.Column("attempts", sa.Integer(), nullable=False),
|
||||
sa.Column("retryable", sa.Boolean(), nullable=False),
|
||||
sa.Column("timeout_ms", sa.Integer(), nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
|
||||
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
|
||||
)
|
||||
op.create_index("ix_task_nodes_task_id", "task_nodes", ["task_id"])
|
||||
op.create_index("ix_task_nodes_status", "task_nodes", ["status"])
|
||||
|
||||
op.create_table(
|
||||
"confirmations",
|
||||
sa.Column("confirmation_id", sa.String(length=64), primary_key=True),
|
||||
sa.Column("task_id", sa.String(length=64), nullable=False),
|
||||
sa.Column("node_id", sa.String(length=64), nullable=False),
|
||||
sa.Column("status", sa.String(length=64), nullable=False),
|
||||
sa.Column("scope", sa.String(length=32), nullable=False),
|
||||
sa.Column("decision_json", sa.JSON(), nullable=False),
|
||||
sa.Column("preview_json", sa.JSON(), nullable=False),
|
||||
sa.Column("comment", sa.Text(), nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
|
||||
sa.Column("resolved_at", sa.DateTime(timezone=True), nullable=True),
|
||||
)
|
||||
op.create_index("ix_confirmations_task_id", "confirmations", ["task_id"])
|
||||
op.create_index("ix_confirmations_node_id", "confirmations", ["node_id"])
|
||||
op.create_index("ix_confirmations_status", "confirmations", ["status"])
|
||||
|
||||
op.create_table(
|
||||
"worker_sessions",
|
||||
sa.Column("session_id", sa.String(length=64), primary_key=True),
|
||||
sa.Column("worker_id", sa.String(length=128), nullable=False),
|
||||
sa.Column("name", sa.String(length=255), nullable=False),
|
||||
sa.Column("machine", sa.String(length=255), nullable=False),
|
||||
sa.Column("os", sa.String(length=64), nullable=False),
|
||||
sa.Column("version", sa.String(length=64), nullable=False),
|
||||
sa.Column("status", sa.String(length=64), nullable=False),
|
||||
sa.Column("capabilities_json", sa.JSON(), nullable=False),
|
||||
sa.Column("last_heartbeat_at", sa.DateTime(timezone=True), nullable=False),
|
||||
sa.Column("current_task_id", sa.String(length=64), nullable=True),
|
||||
sa.Column("connected_at", sa.DateTime(timezone=True), nullable=False),
|
||||
sa.Column("disconnected_at", sa.DateTime(timezone=True), nullable=True),
|
||||
)
|
||||
op.create_index("ix_worker_sessions_worker_id", "worker_sessions", ["worker_id"])
|
||||
op.create_index("ix_worker_sessions_status", "worker_sessions", ["status"])
|
||||
op.create_index(
|
||||
"ix_worker_sessions_last_heartbeat_at",
|
||||
"worker_sessions",
|
||||
["last_heartbeat_at"],
|
||||
)
|
||||
|
||||
op.create_table(
|
||||
"task_events",
|
||||
sa.Column("event_id", sa.String(length=64), primary_key=True),
|
||||
sa.Column("task_id", sa.String(length=64), nullable=False),
|
||||
sa.Column("conversation_id", sa.String(length=128), nullable=True),
|
||||
sa.Column("node_id", sa.String(length=64), nullable=True),
|
||||
sa.Column("event_type", sa.String(length=128), nullable=False),
|
||||
sa.Column("correlation_id", sa.String(length=64), nullable=True),
|
||||
sa.Column("causation_id", sa.String(length=64), nullable=True),
|
||||
sa.Column("payload_json", sa.JSON(), nullable=False),
|
||||
sa.Column("occurred_at", sa.DateTime(timezone=True), nullable=False),
|
||||
)
|
||||
op.create_index("ix_task_events_task_id", "task_events", ["task_id"])
|
||||
op.create_index("ix_task_events_occurred_at", "task_events", ["occurred_at"])
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_index("ix_task_events_occurred_at", table_name="task_events")
|
||||
op.drop_index("ix_task_events_task_id", table_name="task_events")
|
||||
op.drop_table("task_events")
|
||||
op.drop_index("ix_worker_sessions_last_heartbeat_at", table_name="worker_sessions")
|
||||
op.drop_index("ix_worker_sessions_status", table_name="worker_sessions")
|
||||
op.drop_index("ix_worker_sessions_worker_id", table_name="worker_sessions")
|
||||
op.drop_table("worker_sessions")
|
||||
op.drop_index("ix_confirmations_status", table_name="confirmations")
|
||||
op.drop_index("ix_confirmations_node_id", table_name="confirmations")
|
||||
op.drop_index("ix_confirmations_task_id", table_name="confirmations")
|
||||
op.drop_table("confirmations")
|
||||
op.drop_index("ix_task_nodes_status", table_name="task_nodes")
|
||||
op.drop_index("ix_task_nodes_task_id", table_name="task_nodes")
|
||||
op.drop_table("task_nodes")
|
||||
op.drop_table("task_graphs")
|
||||
op.drop_index("ix_tasks_updated_at", table_name="tasks")
|
||||
op.drop_index("ix_tasks_status", table_name="tasks")
|
||||
op.drop_index("ix_tasks_conversation_id", table_name="tasks")
|
||||
op.drop_index("ix_tasks_project_id", table_name="tasks")
|
||||
op.drop_table("tasks")
|
||||
Reference in New Issue
Block a user