# SPDX-FileCopyrightText: 2026 Dennis Fink # # 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")