Task 003: add postgres schema and seed data
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import sys
|
||||
from logging.config import fileConfig
|
||||
from pathlib import Path
|
||||
|
||||
from alembic import context
|
||||
from sqlalchemy import engine_from_config, pool
|
||||
|
||||
|
||||
BACKEND_ROOT = Path(__file__).resolve().parents[1]
|
||||
for import_root in (BACKEND_ROOT, BACKEND_ROOT.parents[1]):
|
||||
if (import_root / "src").exists():
|
||||
sys.path.insert(0, str(import_root))
|
||||
|
||||
config = context.config
|
||||
|
||||
if config.config_file_name is not None:
|
||||
fileConfig(config.config_file_name)
|
||||
|
||||
target_metadata = None
|
||||
|
||||
|
||||
def _database_url() -> str:
|
||||
configured = os.environ.get("DATABASE_URL") or os.environ.get("POSTGRES_DSN")
|
||||
if not configured:
|
||||
return config.get_main_option("sqlalchemy.url")
|
||||
|
||||
if configured.startswith("postgresql://"):
|
||||
return "postgresql+psycopg://" + configured.removeprefix("postgresql://")
|
||||
if configured.startswith("postgres://"):
|
||||
return "postgresql+psycopg://" + configured.removeprefix("postgres://")
|
||||
return configured
|
||||
|
||||
|
||||
def run_migrations_offline() -> None:
|
||||
context.configure(
|
||||
url=_database_url(),
|
||||
target_metadata=target_metadata,
|
||||
literal_binds=True,
|
||||
dialect_opts={"paramstyle": "named"},
|
||||
)
|
||||
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
def run_migrations_online() -> None:
|
||||
configuration = config.get_section(config.config_ini_section, {})
|
||||
configuration["sqlalchemy.url"] = _database_url()
|
||||
connectable = engine_from_config(
|
||||
configuration,
|
||||
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,26 @@
|
||||
"""${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 = ${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,45 @@
|
||||
"""initial postgres schema
|
||||
|
||||
Revision ID: 202605210003
|
||||
Revises:
|
||||
Create Date: 2026-05-21 00:03:00.000000
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from alembic import op
|
||||
|
||||
from src.infrastructure.schema import POSTGRES_SCHEMA_STATEMENTS
|
||||
|
||||
|
||||
revision = "202605210003"
|
||||
down_revision = None
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
for statement in POSTGRES_SCHEMA_STATEMENTS:
|
||||
op.execute(statement)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
for table_name in (
|
||||
"prompt_versions",
|
||||
"publish_commits",
|
||||
"research_run_manifests",
|
||||
"agent_jobs",
|
||||
"workflow_events",
|
||||
"assets",
|
||||
"article_drafts",
|
||||
"claims",
|
||||
"evidence_items",
|
||||
"plan_sections",
|
||||
"article_plans",
|
||||
"boundary_questions",
|
||||
"articles",
|
||||
"script_config_versions",
|
||||
"target_sites",
|
||||
"users",
|
||||
):
|
||||
op.execute(f"DROP TABLE IF EXISTS {table_name} CASCADE")
|
||||
Reference in New Issue
Block a user