aboutsummaryrefslogtreecommitdiff
path: root/webmentions_ssg/migrations/versions/ef8a9b6c2d41_received_status_history.py
blob: db63011915dc966fd532e52cfd9945fecd208a83 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me>
#
# SPDX-License-Identifier: BSD-3-Clause

"""Record received Webmention lifecycle transitions.

Revision ID: ef8a9b6c2d41
Revises: a4f91c2e08d7
"""

import sqlalchemy as sa
from alembic import op

revision = "ef8a9b6c2d41"
down_revision = "a4f91c2e08d7"
branch_labels = None
depends_on = None


def upgrade():
    op.create_table(
        "received_webmention_status_events",
        sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column("webmention_uuid", sa.Uuid(), nullable=False),
        sa.Column(
            "status",
            sa.Enum(
                "received",
                "verifying",
                "verified",
                "failed",
                "source_removed",
                name="received_webmention_status",
                native_enum=False,
            ),
            nullable=False,
        ),
        sa.Column("action", sa.String(length=80), nullable=False),
        sa.Column("failure_reason", sa.Text(), nullable=True),
        sa.Column("recovery_attempts", sa.Integer(), nullable=False),
        sa.Column("occurred_at", sa.DateTime(timezone=True), nullable=False),
        sa.ForeignKeyConstraint(
            ["webmention_uuid"],
            ["received_webmentions.uuid"],
            ondelete="CASCADE",
        ),
    )
    op.create_index(
        "ix_received_webmention_status_events_webmention_id",
        "received_webmention_status_events",
        ["webmention_uuid", "id"],
    )

    # Existing rows have no saved transitions; record just the last known state.
    op.execute(
        sa.text(
            """INSERT INTO received_webmention_status_events
               (webmention_uuid, status, action, failure_reason, recovery_attempts, occurred_at)
               SELECT uuid, status, 'Existing status at upgrade',
                      failure_reason, recovery_attempts, updated_at
               FROM received_webmentions"""
        )
    )


def downgrade():
    op.drop_index(
        "ix_received_webmention_status_events_webmention_id",
        table_name="received_webmention_status_events",
    )
    op.drop_table("received_webmention_status_events")