|
| 1 | +"""add calibration table; media.active_calibration_id |
| 2 | +
|
| 3 | +Revision ID: f8e9a1b2c3d4 |
| 4 | +Revises: e2f3a4b5c6d7 |
| 5 | +Create Date: 2026-03-23 |
| 6 | +
|
| 7 | +""" |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +from typing import Sequence, Union |
| 11 | + |
| 12 | +import sqlalchemy as sa |
| 13 | +from alembic import op |
| 14 | + |
| 15 | +revision: str = "f8e9a1b2c3d4" |
| 16 | +down_revision: Union[str, None] = "e2f3a4b5c6d7" |
| 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 | + op.create_table( |
| 23 | + "calibration", |
| 24 | + sa.Column("id", sa.Uuid(), nullable=False), |
| 25 | + sa.Column("source_media_id", sa.Uuid(), nullable=False), |
| 26 | + sa.Column("frame_index", sa.Integer(), nullable=True), |
| 27 | + sa.Column("time_seconds", sa.Float(), nullable=True), |
| 28 | + sa.Column("corners_json", sa.Text(), nullable=False), |
| 29 | + sa.Column("reference_edge_index", sa.Integer(), nullable=False), |
| 30 | + sa.Column("reference_length_mm", sa.Float(), nullable=False), |
| 31 | + sa.Column("ref_width_px", sa.Integer(), nullable=True), |
| 32 | + sa.Column("ref_height_px", sa.Integer(), nullable=True), |
| 33 | + sa.Column("mm_per_px", sa.Float(), nullable=False), |
| 34 | + sa.Column("label", sa.String(length=512), nullable=True), |
| 35 | + sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("(CURRENT_TIMESTAMP)"), nullable=False), |
| 36 | + sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.text("(CURRENT_TIMESTAMP)"), nullable=False), |
| 37 | + sa.ForeignKeyConstraint( |
| 38 | + ["source_media_id"], |
| 39 | + ["media.id"], |
| 40 | + name=op.f("fk_calibration_source_media_id_media"), |
| 41 | + ondelete="CASCADE", |
| 42 | + ), |
| 43 | + sa.PrimaryKeyConstraint("id", name=op.f("pk_calibration")), |
| 44 | + ) |
| 45 | + op.create_index(op.f("ix_calibration_source_media_id"), "calibration", ["source_media_id"], unique=False) |
| 46 | + |
| 47 | + # SQLite cannot ALTER TABLE ADD CONSTRAINT; keep column + FK + index in one batch_alter_table. |
| 48 | + with op.batch_alter_table("media") as batch: |
| 49 | + batch.add_column( |
| 50 | + sa.Column( |
| 51 | + "active_calibration_id", |
| 52 | + sa.Uuid(), |
| 53 | + nullable=True, |
| 54 | + ) |
| 55 | + ) |
| 56 | + batch.create_foreign_key( |
| 57 | + op.f("fk_media_active_calibration_id_calibration"), |
| 58 | + "calibration", |
| 59 | + ["active_calibration_id"], |
| 60 | + ["id"], |
| 61 | + ondelete="SET NULL", |
| 62 | + ) |
| 63 | + batch.create_index( |
| 64 | + op.f("ix_media_active_calibration_id"), |
| 65 | + ["active_calibration_id"], |
| 66 | + unique=False, |
| 67 | + ) |
| 68 | + |
| 69 | + |
| 70 | +def downgrade() -> None: |
| 71 | + with op.batch_alter_table("media") as batch: |
| 72 | + batch.drop_constraint(op.f("fk_media_active_calibration_id_calibration"), type_="foreignkey") |
| 73 | + batch.drop_index(op.f("ix_media_active_calibration_id")) |
| 74 | + batch.drop_column("active_calibration_id") |
| 75 | + |
| 76 | + op.drop_index(op.f("ix_calibration_source_media_id"), table_name="calibration") |
| 77 | + op.drop_table("calibration") |
0 commit comments