aboutsummaryrefslogtreecommitdiff
path: root/webmentions_ssg/migrations/versions/c8fa49e1a203_sent_webmention_status_history.py
blob: 454367dfd53ea21f078259c21ce5525566eff247 (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 outgoing Webmention delivery history.

Revision ID: c8fa49e1a203
Revises: 2b4c6e8a0d12
"""

import sqlalchemy as sa
from alembic import op

revision = "c8fa49e1a203"
down_revision = "2b4c6e8a0d12"
branch_labels = None
depends_on = None


def upgrade():
    op.create_table(
        "sent_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(
                "sent",
                "no_endpoint",
                "failed",
                name="sent_webmention_status",
                native_enum=False,
            ),
            nullable=True,
        ),
        sa.Column("action", sa.String(length=80), nullable=False),
        sa.Column("revision", sa.Integer(), nullable=False),
        sa.Column("attempt_count", sa.Integer(), nullable=False),
        sa.Column("failure_reason", sa.Text(), nullable=True),
        sa.Column("endpoint_response_status", sa.Integer(), nullable=True),
        sa.Column("occurred_at", sa.DateTime(timezone=True), nullable=False),
        sa.ForeignKeyConstraint(
            ["webmention_uuid"], ["sent_webmentions.uuid"], ondelete="CASCADE"
        ),
    )
    op.create_index(
        "ix_sent_webmention_status_events_webmention_id",
        "sent_webmention_status_events",
        ["webmention_uuid", "id"],
    )
    # A pre-upgrade delivery has only its current result available.
    op.execute(
        sa.text(
            """INSERT INTO sent_webmention_status_events
               (webmention_uuid, status, action, revision, attempt_count,
                failure_reason, endpoint_response_status, occurred_at)
               SELECT uuid, status, 'Existing status at upgrade',
                      desired_revision, attempt_count, failure_reason,
                      endpoint_response_status,
                      COALESCE(last_attempted_at, last_sent_at, CURRENT_TIMESTAMP)
               FROM sent_webmentions"""
        )
    )


def downgrade():
    op.drop_index(
        "ix_sent_webmention_status_events_webmention_id",
        table_name="sent_webmention_status_events",
    )
    op.drop_table("sent_webmention_status_events")