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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me>
#
# SPDX-License-Identifier: BSD-3-Clause
"""Clarify source-document and Webmention lifecycle names.
Revision ID: 9a4e0c8211d6
Revises: 7c38bb1a935e
"""
import sqlalchemy as sa
from alembic import op
revision = "9a4e0c8211d6"
down_revision = "7c38bb1a935e"
branch_labels = None
depends_on = None
_OLD_RECEIVED_STATUS = sa.Enum(
"received",
"verifying",
"verified",
"failed",
"deleted",
name="received_webmention_status",
native_enum=False,
)
_NEW_RECEIVED_STATUS = sa.Enum(
"received",
"verifying",
"verified",
"failed",
"source_removed",
name="received_webmention_status",
native_enum=False,
)
def upgrade() -> None:
# RENAME TABLE retains existing source UUIDs and updates referencing FKs.
op.rename_table("sources", "source_documents")
with op.batch_alter_table("received_webmentions") as batch:
batch.drop_constraint("uq_webmention_url_digest", type_="unique")
batch.alter_column(
"url_digest",
new_column_name="source_target_digest",
existing_type=sa.String(64),
existing_nullable=False,
)
# The new enum string is 14 characters, not the old VARCHAR(9).
batch.alter_column(
"status",
existing_type=_OLD_RECEIVED_STATUS,
type_=_NEW_RECEIVED_STATUS,
existing_nullable=False,
)
with op.batch_alter_table("received_webmentions") as batch:
batch.create_unique_constraint(
"uq_webmention_source_target_digest", ["source_target_digest"]
)
with op.batch_alter_table("sent_webmentions") as batch:
batch.drop_index("ix_sent_webmentions_source_id")
batch.drop_constraint("uq_sent_webmention_source_target_digest", type_="unique")
batch.alter_column(
"source_id",
new_column_name="source_uuid",
existing_type=sa.Uuid(),
existing_nullable=False,
)
batch.alter_column(
"active",
new_column_name="linked",
existing_type=sa.Boolean(),
existing_nullable=False,
)
batch.alter_column(
"endpoint",
new_column_name="webmention_endpoint",
existing_type=sa.Text(),
existing_nullable=True,
)
batch.alter_column(
"response_status",
new_column_name="endpoint_response_status",
existing_type=sa.Integer(),
existing_nullable=True,
)
with op.batch_alter_table("sent_webmentions") as batch:
batch.create_unique_constraint(
"uq_sent_webmention_source_uuid_target_digest",
["source_uuid", "target_digest"],
)
op.create_index(
"ix_sent_webmentions_source_uuid", "sent_webmentions", ["source_uuid"]
)
op.execute(
"UPDATE received_webmentions SET status = 'source_removed' "
"WHERE status = 'deleted'"
)
op.execute(
"UPDATE sent_webmentions SET status = 'no_endpoint' "
"WHERE status = 'unsupported'"
)
def downgrade() -> None:
op.execute(
"UPDATE received_webmentions SET status = 'deleted' "
"WHERE status = 'source_removed'"
)
op.execute(
"UPDATE sent_webmentions SET status = 'unsupported' "
"WHERE status = 'no_endpoint'"
)
with op.batch_alter_table("sent_webmentions") as batch:
batch.drop_index("ix_sent_webmentions_source_uuid")
batch.drop_constraint(
"uq_sent_webmention_source_uuid_target_digest", type_="unique"
)
batch.alter_column(
"source_uuid",
new_column_name="source_id",
existing_type=sa.Uuid(),
existing_nullable=False,
)
batch.alter_column(
"linked",
new_column_name="active",
existing_type=sa.Boolean(),
existing_nullable=False,
)
batch.alter_column(
"webmention_endpoint",
new_column_name="endpoint",
existing_type=sa.Text(),
existing_nullable=True,
)
batch.alter_column(
"endpoint_response_status",
new_column_name="response_status",
existing_type=sa.Integer(),
existing_nullable=True,
)
with op.batch_alter_table("sent_webmentions") as batch:
batch.create_unique_constraint(
"uq_sent_webmention_source_target_digest", ["source_id", "target_digest"]
)
op.create_index("ix_sent_webmentions_source_id", "sent_webmentions", ["source_id"])
with op.batch_alter_table("received_webmentions") as batch:
batch.drop_constraint("uq_webmention_source_target_digest", type_="unique")
batch.alter_column(
"source_target_digest",
new_column_name="url_digest",
existing_type=sa.String(64),
existing_nullable=False,
)
batch.alter_column(
"status",
existing_type=_NEW_RECEIVED_STATUS,
type_=_OLD_RECEIVED_STATUS,
existing_nullable=False,
)
with op.batch_alter_table("received_webmentions") as batch:
batch.create_unique_constraint("uq_webmention_url_digest", ["url_digest"])
op.rename_table("source_documents", "sources")
|