Files
full-stack-fastapi/backend/app/alembic/versions/a1b2c3d4e5f6_add_mqtt_pipeline_tables.py
2026-03-17 13:10:50 +08:00

224 lines
7.6 KiB
Python

"""add mqtt pipeline tables
Revision ID: a1b2c3d4e5f6
Revises: 0b117fab4208
Create Date: 2026-03-17 12:30:00.000000
"""
from alembic import op
import sqlalchemy as sa
import sqlmodel.sql.sqltypes
# revision identifiers, used by Alembic.
revision = "a1b2c3d4e5f6"
down_revision = "0b117fab4208"
branch_labels = None
depends_on = None
def upgrade() -> None:
op.create_table(
"terminal_message_raw",
sa.Column("id", sa.Uuid(), nullable=False),
sa.Column(
"device_id", sqlmodel.sql.sqltypes.AutoString(length=255), nullable=False
),
sa.Column("topic", sqlmodel.sql.sqltypes.AutoString(length=255), nullable=False),
sa.Column("received_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("payload_bytes", sa.LargeBinary(), nullable=False),
sa.Column("payload_size", sa.Integer(), nullable=False),
sa.Column(
"payload_sha256", sqlmodel.sql.sqltypes.AutoString(length=64), nullable=False
),
sa.Column("qos", sa.Integer(), nullable=False),
sa.Column("retain", sa.Boolean(), nullable=False),
sa.Column(
"parse_status", sqlmodel.sql.sqltypes.AutoString(length=32), nullable=False
),
sa.Column("parse_attempts", sa.Integer(), nullable=False),
sa.Column("next_retry_at", sa.DateTime(timezone=True), nullable=True),
sa.Column(
"parse_error", sqlmodel.sql.sqltypes.AutoString(length=1024), nullable=True
),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.PrimaryKeyConstraint("id"),
)
op.create_index(
op.f("ix_terminal_message_raw_device_id"),
"terminal_message_raw",
["device_id"],
unique=False,
)
op.create_index(
op.f("ix_terminal_message_raw_next_retry_at"),
"terminal_message_raw",
["next_retry_at"],
unique=False,
)
op.create_index(
op.f("ix_terminal_message_raw_parse_status"),
"terminal_message_raw",
["parse_status"],
unique=False,
)
op.create_index(
op.f("ix_terminal_message_raw_payload_sha256"),
"terminal_message_raw",
["payload_sha256"],
unique=False,
)
op.create_index(
op.f("ix_terminal_message_raw_received_at"),
"terminal_message_raw",
["received_at"],
unique=False,
)
op.create_table(
"terminal_observation_event",
sa.Column("id", sa.Uuid(), nullable=False),
sa.Column("raw_id", sa.Uuid(), nullable=False),
sa.Column(
"device_id", sqlmodel.sql.sqltypes.AutoString(length=255), nullable=False
),
sa.Column("event_time", sa.DateTime(timezone=True), nullable=False),
sa.Column(
"event_type", sqlmodel.sql.sqltypes.AutoString(length=64), nullable=False
),
sa.Column("payload_json", sa.JSON(), nullable=False),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.ForeignKeyConstraint(["raw_id"], ["terminal_message_raw.id"]),
sa.PrimaryKeyConstraint("id"),
)
op.create_index(
op.f("ix_terminal_observation_event_device_id"),
"terminal_observation_event",
["device_id"],
unique=False,
)
op.create_index(
op.f("ix_terminal_observation_event_event_time"),
"terminal_observation_event",
["event_time"],
unique=False,
)
op.create_index(
op.f("ix_terminal_observation_event_event_type"),
"terminal_observation_event",
["event_type"],
unique=False,
)
op.create_index(
op.f("ix_terminal_observation_event_raw_id"),
"terminal_observation_event",
["raw_id"],
unique=False,
)
op.create_table(
"terminal_device_state",
sa.Column(
"device_id", sqlmodel.sql.sqltypes.AutoString(length=255), nullable=False
),
sa.Column("last_event_time", sa.DateTime(timezone=True), nullable=True),
sa.Column("fix_status", sqlmodel.sql.sqltypes.AutoString(length=64), nullable=True),
sa.Column("lat", sa.Float(), nullable=True),
sa.Column("lon", sa.Float(), nullable=True),
sa.Column("alt_m", sa.Float(), nullable=True),
sa.Column("satellites_used", sa.Integer(), nullable=True),
sa.Column("satellite_signal_summary", sa.JSON(), nullable=True),
sa.Column("rtcm_type_summary", sa.JSON(), nullable=True),
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
sa.PrimaryKeyConstraint("device_id"),
)
op.create_table(
"terminal_raw_archive_manifest",
sa.Column("id", sa.Uuid(), nullable=False),
sa.Column(
"device_id", sqlmodel.sql.sqltypes.AutoString(length=255), nullable=False
),
sa.Column("range_start", sa.DateTime(timezone=True), nullable=False),
sa.Column("range_end", sa.DateTime(timezone=True), nullable=False),
sa.Column(
"file_path", sqlmodel.sql.sqltypes.AutoString(length=1024), nullable=False
),
sa.Column("file_size", sa.Integer(), nullable=False),
sa.Column("sha256", sqlmodel.sql.sqltypes.AutoString(length=64), nullable=False),
sa.Column("record_count", sa.Integer(), nullable=False),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("file_path"),
)
op.create_index(
op.f("ix_terminal_raw_archive_manifest_device_id"),
"terminal_raw_archive_manifest",
["device_id"],
unique=False,
)
op.create_index(
op.f("ix_terminal_raw_archive_manifest_range_end"),
"terminal_raw_archive_manifest",
["range_end"],
unique=False,
)
op.create_index(
op.f("ix_terminal_raw_archive_manifest_range_start"),
"terminal_raw_archive_manifest",
["range_start"],
unique=False,
)
def downgrade() -> None:
op.drop_index(
op.f("ix_terminal_raw_archive_manifest_range_start"),
table_name="terminal_raw_archive_manifest",
)
op.drop_index(
op.f("ix_terminal_raw_archive_manifest_range_end"),
table_name="terminal_raw_archive_manifest",
)
op.drop_index(
op.f("ix_terminal_raw_archive_manifest_device_id"),
table_name="terminal_raw_archive_manifest",
)
op.drop_table("terminal_raw_archive_manifest")
op.drop_table("terminal_device_state")
op.drop_index(
op.f("ix_terminal_observation_event_raw_id"),
table_name="terminal_observation_event",
)
op.drop_index(
op.f("ix_terminal_observation_event_event_type"),
table_name="terminal_observation_event",
)
op.drop_index(
op.f("ix_terminal_observation_event_event_time"),
table_name="terminal_observation_event",
)
op.drop_index(
op.f("ix_terminal_observation_event_device_id"),
table_name="terminal_observation_event",
)
op.drop_table("terminal_observation_event")
op.drop_index(
op.f("ix_terminal_message_raw_received_at"), table_name="terminal_message_raw"
)
op.drop_index(
op.f("ix_terminal_message_raw_payload_sha256"),
table_name="terminal_message_raw",
)
op.drop_index(
op.f("ix_terminal_message_raw_parse_status"), table_name="terminal_message_raw"
)
op.drop_index(
op.f("ix_terminal_message_raw_next_retry_at"), table_name="terminal_message_raw"
)
op.drop_index(
op.f("ix_terminal_message_raw_device_id"), table_name="terminal_message_raw"
)
op.drop_table("terminal_message_raw")