Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing constraint on annotation_metadata.annotation_id #9257

Merged
merged 1 commit into from
Jan 20, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions h/migrations/versions/78d3d6fe1d42_metadata_unique_constraint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""Add missing unique constraint on annotation_medatada."""

import sqlalchemy as sa
from alembic import op

revision = "78d3d6fe1d42"
down_revision = "a122e276f8d1"


def upgrade():
conn = op.get_bind()

# CONCURRENTLY can't be used inside a transaction. Finish the current one.
op.execute("COMMIT;")

# First create the index concurrently to avoid locking the table
conn.execute(
sa.text(
"""
CREATE UNIQUE INDEX CONCURRENTLY ix__annotation_metadata__annotation_id ON annotation_metadata (annotation_id);
"""
)
)
# Once the index is created, use it to create the unique constraint
conn.execute(
sa.text(
"""
ALTER TABLE annotation_metadata ADD CONSTRAINT uq__annotation_metadata__annotation_id UNIQUE USING INDEX ix__annotation_metadata__annotation_id
"""
)
)


def downgrade():
op.drop_constraint(
op.f("uq__annotation_metadata__annotation_id"),
"annotation_metadata",
type_="unique",
)
Loading