0001 called Base.metadata.create_all(), building the schema from the current models, so on a fresh PostgreSQL 0002 collided on datasets.raw_sha256 and `alembic upgrade head` could never run from zero. 0001 is now an explicit, frozen baseline: the seven base tables exactly as the real history created them, before 0002/0006 extended them. Databases already past 0001 never re-run it. Proof: a fresh `upgrade head` produces a schema identical (pg_dump --schema-only) to the long-lived dev database. 0019 repairs a latent bug found by that comparison: 0006 used server_default="now()" as a string, which PostgreSQL froze at the instant the column was created, so every Agent and Job got the same created_at. It also adds three indexes the models declare but no migration created. central/tests/test_migration_baseline.py guards (via AST) against migrations importing the models or using string timestamp defaults. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
100 lines
4.8 KiB
Python
100 lines
4.8 KiB
Python
"""initial hub persistence
|
|
|
|
Revision ID: 0001
|
|
|
|
Frozen, explicit baseline. This revision used to run
|
|
``Base.metadata.create_all()``, which builds the schema from the *current*
|
|
models: on an empty database it created tables/columns that 0002..0018 then
|
|
tried to create again (first collision: ``datasets.raw_sha256`` in 0002), so
|
|
``alembic upgrade head`` could never run from zero.
|
|
|
|
This body reproduces exactly what 0001 created on the databases that went
|
|
through the real history: the seven base tables, *before* 0002 added
|
|
``datasets.raw_sha256`` and 0006 added the agent status/timestamp columns.
|
|
Constraint and index names match the ones those databases already have
|
|
(PostgreSQL defaults for PK/FK/UNIQUE, ``ix_<table>_<column>`` for indexes).
|
|
|
|
Databases already at 0001 or later never re-run this revision, so changing
|
|
its body only affects new installations. It must never import the ORM
|
|
models again: a migration describes one point in history, not the present.
|
|
"""
|
|
from alembic import op
|
|
import sqlalchemy as sa
|
|
|
|
revision = "0001"
|
|
down_revision = None
|
|
branch_labels = depends_on = None
|
|
|
|
|
|
def upgrade():
|
|
op.create_table(
|
|
"tenants",
|
|
sa.Column("id", sa.Integer(), primary_key=True),
|
|
sa.Column("name", sa.String(128), nullable=False, unique=True),
|
|
)
|
|
op.create_table(
|
|
"datasets",
|
|
sa.Column("id", sa.Integer(), primary_key=True),
|
|
sa.Column("kind", sa.String(32), nullable=False),
|
|
sa.Column("provider", sa.String(128), nullable=False),
|
|
sa.Column("sha256", sa.String(64), nullable=False),
|
|
sa.Column("version_label", sa.String(256), nullable=False),
|
|
sa.Column("act", sa.Text(), nullable=False),
|
|
sa.Column("source_url", sa.Text(), nullable=False),
|
|
sa.Column("downloaded_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
|
|
sa.Column("status", sa.String(32), nullable=False),
|
|
sa.Column("record_count", sa.Integer(), nullable=False),
|
|
sa.UniqueConstraint("provider", "sha256", name="uq_dataset_provider_hash"),
|
|
)
|
|
op.create_index("ix_datasets_kind", "datasets", ["kind"])
|
|
op.create_index("ix_datasets_status", "datasets", ["status"])
|
|
op.create_table(
|
|
"ncm_entries",
|
|
sa.Column("id", sa.Integer(), primary_key=True),
|
|
sa.Column("dataset_id", sa.Integer(), sa.ForeignKey("datasets.id"), nullable=False),
|
|
sa.Column("code", sa.String(16), nullable=False),
|
|
sa.Column("description", sa.Text(), nullable=False),
|
|
sa.Column("valid_from", sa.String(32), nullable=False),
|
|
sa.Column("valid_to", sa.String(32), nullable=False),
|
|
sa.UniqueConstraint("dataset_id", "code", "valid_from", "valid_to", name="uq_ncm_versioned_entry"),
|
|
)
|
|
op.create_index("ix_ncm_entries_dataset_id", "ncm_entries", ["dataset_id"])
|
|
op.create_index("ix_ncm_entries_code", "ncm_entries", ["code"])
|
|
op.create_table(
|
|
"agents",
|
|
sa.Column("id", sa.Integer(), primary_key=True),
|
|
sa.Column("tenant_id", sa.Integer(), sa.ForeignKey("tenants.id"), nullable=False),
|
|
sa.Column("name", sa.String(128), nullable=False),
|
|
sa.Column("token_hash", sa.String(64), nullable=False, unique=True),
|
|
)
|
|
op.create_index("ix_agents_tenant_id", "agents", ["tenant_id"])
|
|
op.create_table(
|
|
"classification_cache",
|
|
sa.Column("fingerprint", sa.String(64), primary_key=True),
|
|
sa.Column("tenant_id", sa.Integer(), sa.ForeignKey("tenants.id"), nullable=False),
|
|
sa.Column("result", sa.JSON(), nullable=False),
|
|
)
|
|
op.create_index("ix_classification_cache_tenant_id", "classification_cache", ["tenant_id"])
|
|
op.create_table(
|
|
"classification_requests",
|
|
sa.Column("id", sa.Integer(), primary_key=True),
|
|
sa.Column("tenant_id", sa.Integer(), sa.ForeignKey("tenants.id"), nullable=False),
|
|
sa.Column("agent_id", sa.Integer(), sa.ForeignKey("agents.id"), nullable=False),
|
|
sa.Column("fingerprint", sa.String(64), nullable=False, unique=True),
|
|
sa.Column("payload", sa.JSON(), nullable=False),
|
|
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
|
|
)
|
|
op.create_index("ix_classification_requests_tenant_id", "classification_requests", ["tenant_id"])
|
|
op.create_index("ix_classification_requests_agent_id", "classification_requests", ["agent_id"])
|
|
op.create_table(
|
|
"classification_results",
|
|
sa.Column("id", sa.Integer(), primary_key=True),
|
|
sa.Column("request_id", sa.Integer(), sa.ForeignKey("classification_requests.id"), nullable=False, unique=True),
|
|
sa.Column("result", sa.JSON(), nullable=False),
|
|
)
|
|
|
|
|
|
def downgrade():
|
|
for table in ("classification_results", "classification_requests", "classification_cache", "agents", "ncm_entries", "datasets", "tenants"):
|
|
op.drop_table(table)
|