| 1 | """Add notification tables |
| 2 | |
| 3 | Revision ID: 458e2a7b6b11 |
| 4 | Revises: 03508fb56a48 |
| 5 | Create Date: 2026-04-29 10:40:19.212448 |
| 6 | |
| 7 | """ |
| 8 | from typing import Sequence |
| 9 | from typing import Union |
| 10 | |
| 11 | import sqlalchemy as sa |
| 12 | |
| 13 | from alembic import op |
| 14 | |
| 15 | # revision identifiers, used by Alembic. |
| 16 | revision: str = "458e2a7b6b11" |
| 17 | down_revision: Union[str, None] = "03508fb56a48" |
| 18 | branch_labels: Union[str, Sequence[str], None] = None |
| 19 | depends_on: Union[str, Sequence[str], None] = None |
| 20 | |
| 21 | |
| 22 | def upgrade() -> None: |
| 23 | # ### commands auto generated by Alembic - please adjust! ### |
| 24 | op.create_table( |
| 25 | "customer_notification_route", |
| 26 | sa.Column("destination", sa.Text(), nullable=True), |
| 27 | sa.Column("format_template", sa.Text(), nullable=True), |
| 28 | sa.Column("id", sa.Integer(), nullable=False), |
| 29 | sa.Column("customer_code", sa.String(length=64), nullable=False), |
| 30 | sa.Column("name", sa.String(length=128), nullable=False), |
| 31 | sa.Column("trigger", sa.String(length=64), nullable=False), |
| 32 | sa.Column("channel", sa.String(length=32), nullable=False), |
| 33 | sa.Column("min_severity", sa.String(length=20), nullable=False), |
| 34 | sa.Column("enabled", sa.Boolean(), nullable=False), |
| 35 | sa.Column("last_dispatched_at", sa.DateTime(), nullable=True), |
| 36 | sa.Column("dispatch_count", sa.Integer(), nullable=False), |
| 37 | sa.Column("created_by", sa.String(length=128), nullable=True), |
| 38 | sa.Column("created_at", sa.DateTime(), nullable=False), |
| 39 | sa.Column("updated_at", sa.DateTime(), nullable=True), |
| 40 | sa.ForeignKeyConstraint( |
| 41 | ["customer_code"], |
| 42 | ["customers.customer_code"], |
| 43 | ), |
| 44 | sa.PrimaryKeyConstraint("id"), |
| 45 | ) |
| 46 | op.create_index(op.f("ix_customer_notification_route_created_at"), "customer_notification_route", ["created_at"], unique=False) |
| 47 | op.create_index(op.f("ix_customer_notification_route_customer_code"), "customer_notification_route", ["customer_code"], unique=False) |
| 48 | op.create_index(op.f("ix_customer_notification_route_trigger"), "customer_notification_route", ["trigger"], unique=False) |
| 49 | op.create_table( |
| 50 | "notification_dispatch_log", |
| 51 | sa.Column("error_message", sa.Text(), nullable=True), |
| 52 | sa.Column("payload_preview", sa.Text(), nullable=True), |
| 53 | sa.Column("id", sa.Integer(), nullable=False), |
| 54 | sa.Column("customer_code", sa.String(length=64), nullable=False), |
| 55 | sa.Column("alert_id", sa.Integer(), nullable=False), |
| 56 | sa.Column("route_id", sa.Integer(), nullable=False), |
| 57 | sa.Column("trigger", sa.String(length=64), nullable=False), |
| 58 | sa.Column("dispatched_at", sa.DateTime(), nullable=False), |
| 59 | sa.Column("status", sa.String(length=16), nullable=False), |
| 60 | sa.Column("latency_ms", sa.Integer(), nullable=True), |
| 61 | sa.ForeignKeyConstraint( |
| 62 | ["route_id"], |
| 63 | ["customer_notification_route.id"], |
| 64 | ), |
| 65 | sa.PrimaryKeyConstraint("id"), |
| 66 | sa.UniqueConstraint("customer_code", "alert_id", "route_id", "trigger", name="uq_notif_dispatch_idem"), |
| 67 | ) |
| 68 | op.create_index(op.f("ix_notification_dispatch_log_alert_id"), "notification_dispatch_log", ["alert_id"], unique=False) |
| 69 | op.create_index(op.f("ix_notification_dispatch_log_customer_code"), "notification_dispatch_log", ["customer_code"], unique=False) |
| 70 | op.create_index(op.f("ix_notification_dispatch_log_dispatched_at"), "notification_dispatch_log", ["dispatched_at"], unique=False) |
| 71 | op.create_index(op.f("ix_notification_dispatch_log_route_id"), "notification_dispatch_log", ["route_id"], unique=False) |
| 72 | op.create_index(op.f("ix_notification_dispatch_log_status"), "notification_dispatch_log", ["status"], unique=False) |
| 73 | # ### end Alembic commands ### |
| 74 | |
| 75 | |
| 76 | def downgrade() -> None: |
| 77 | # ### commands auto generated by Alembic - please adjust! ### |
| 78 | op.drop_index(op.f("ix_notification_dispatch_log_status"), table_name="notification_dispatch_log") |
| 79 | op.drop_index(op.f("ix_notification_dispatch_log_route_id"), table_name="notification_dispatch_log") |
| 80 | op.drop_index(op.f("ix_notification_dispatch_log_dispatched_at"), table_name="notification_dispatch_log") |
| 81 | op.drop_index(op.f("ix_notification_dispatch_log_customer_code"), table_name="notification_dispatch_log") |
| 82 | op.drop_index(op.f("ix_notification_dispatch_log_alert_id"), table_name="notification_dispatch_log") |
| 83 | op.drop_table("notification_dispatch_log") |
| 84 | op.drop_index(op.f("ix_customer_notification_route_trigger"), table_name="customer_notification_route") |
| 85 | op.drop_index(op.f("ix_customer_notification_route_customer_code"), table_name="customer_notification_route") |
| 86 | op.drop_index(op.f("ix_customer_notification_route_created_at"), table_name="customer_notification_route") |
| 87 | op.drop_table("customer_notification_route") |
| 88 | # ### end Alembic commands ### |