mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-05-12 01:02:39 +02:00
BE-2: Enforce unique connector names per user and search space (idempotent migration)
This commit is contained in:
parent
d75df7e5b2
commit
d979c156f8
1 changed files with 53 additions and 0 deletions
|
|
@ -0,0 +1,53 @@
|
||||||
|
"""
|
||||||
|
Add unique constraint for (search_space_id, user_id, name) on search_source_connectors.
|
||||||
|
|
||||||
|
Revision ID: 58
|
||||||
|
Revises: 57
|
||||||
|
Create Date: 2026-01-06 14:00:00.000000
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
from collections.abc import Sequence
|
||||||
|
from alembic import op
|
||||||
|
|
||||||
|
revision: str = "58"
|
||||||
|
down_revision: str | None = "57"
|
||||||
|
branch_labels: str | Sequence[str] | None = None
|
||||||
|
depends_on: str | Sequence[str] | None = None
|
||||||
|
|
||||||
|
from sqlalchemy import text
|
||||||
|
|
||||||
|
def upgrade() -> None:
|
||||||
|
connection = op.get_bind()
|
||||||
|
constraint_exists = connection.execute(
|
||||||
|
text("""
|
||||||
|
SELECT 1 FROM information_schema.table_constraints
|
||||||
|
WHERE table_name='search_source_connectors'
|
||||||
|
AND constraint_type='UNIQUE'
|
||||||
|
AND constraint_name='uq_searchspace_user_connector_name'
|
||||||
|
""")
|
||||||
|
).scalar()
|
||||||
|
if not constraint_exists:
|
||||||
|
op.create_unique_constraint(
|
||||||
|
"uq_searchspace_user_connector_name",
|
||||||
|
"search_source_connectors",
|
||||||
|
["search_space_id", "user_id", "name"]
|
||||||
|
)
|
||||||
|
|
||||||
|
def downgrade() -> None:
|
||||||
|
connection = op.get_bind()
|
||||||
|
constraint_exists = connection.execute(
|
||||||
|
text("""
|
||||||
|
SELECT 1 FROM information_schema.table_constraints
|
||||||
|
WHERE table_name='search_source_connectors'
|
||||||
|
AND constraint_type='UNIQUE'
|
||||||
|
AND constraint_name='uq_searchspace_user_connector_name'
|
||||||
|
""")
|
||||||
|
).scalar()
|
||||||
|
if constraint_exists:
|
||||||
|
op.drop_constraint(
|
||||||
|
"uq_searchspace_user_connector_name",
|
||||||
|
"search_source_connectors",
|
||||||
|
type_="unique"
|
||||||
|
)
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue