46 lines
2.3 KiB
Python
46 lines
2.3 KiB
Python
"""Add canonical aliases and external-observation review state."""
|
|
from alembic import op
|
|
import sqlalchemy as sa
|
|
|
|
revision = "0009"
|
|
down_revision = "0008"
|
|
branch_labels = None
|
|
depends_on = None
|
|
|
|
|
|
def upgrade() -> None:
|
|
op.add_column("external_observation", sa.Column("fish_id", sa.Uuid(), sa.ForeignKey("fish.id")))
|
|
op.add_column("external_observation", sa.Column("waterbody_id", sa.Uuid(), sa.ForeignKey("waterbody.id")))
|
|
op.add_column("external_observation", sa.Column("catch_report_id", sa.Uuid(), sa.ForeignKey("catch_report.id")))
|
|
op.add_column("external_observation", sa.Column("review_note", sa.Text()))
|
|
op.add_column("external_observation", sa.Column("reviewed_at", sa.DateTime(timezone=True)))
|
|
op.create_unique_constraint("uq_external_observation_catch_report_id", "external_observation", ["catch_report_id"])
|
|
op.create_table(
|
|
"external_entity_alias",
|
|
sa.Column("id", sa.Uuid(), primary_key=True),
|
|
sa.Column("source_system", sa.String(50), sa.ForeignKey("data_source.key"), nullable=False),
|
|
sa.Column("entity_type", sa.String(20), nullable=False),
|
|
sa.Column("external_id", sa.String(200), nullable=False),
|
|
sa.Column("external_name", sa.String(200), nullable=False),
|
|
sa.Column("fish_id", sa.Uuid(), sa.ForeignKey("fish.id")),
|
|
sa.Column("waterbody_id", sa.Uuid(), sa.ForeignKey("waterbody.id")),
|
|
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
|
|
sa.CheckConstraint(
|
|
"(entity_type = 'fish' AND fish_id IS NOT NULL AND waterbody_id IS NULL) OR "
|
|
"(entity_type = 'waterbody' AND waterbody_id IS NOT NULL AND fish_id IS NULL)",
|
|
name="ck_external_entity_alias_target",
|
|
),
|
|
sa.UniqueConstraint("source_system", "entity_type", "external_id"),
|
|
)
|
|
op.create_index("ix_external_entity_alias_source_system", "external_entity_alias", ["source_system"])
|
|
|
|
|
|
def downgrade() -> None:
|
|
op.drop_table("external_entity_alias")
|
|
op.drop_constraint("uq_external_observation_catch_report_id", "external_observation", type_="unique")
|
|
op.drop_column("external_observation", "reviewed_at")
|
|
op.drop_column("external_observation", "review_note")
|
|
op.drop_column("external_observation", "catch_report_id")
|
|
op.drop_column("external_observation", "waterbody_id")
|
|
op.drop_column("external_observation", "fish_id")
|