|
| 1 | +# SPDX-FileCopyrightText: 2024-2025 MTS PJSC |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +"""Make unique names constraints case-insensitive |
| 4 | +
|
| 5 | +Revision ID: 17481d3b2466 |
| 6 | +Revises: 52fb9d8765fd |
| 7 | +Create Date: 2025-09-16 11:18:01.308085 |
| 8 | +
|
| 9 | +""" |
| 10 | + |
| 11 | +import sqlalchemy as sa |
| 12 | +from alembic import op |
| 13 | + |
| 14 | +# revision identifiers, used by Alembic. |
| 15 | +revision = "17481d3b2466" |
| 16 | +down_revision = "52fb9d8765fd" |
| 17 | +branch_labels = None |
| 18 | +depends_on = None |
| 19 | + |
| 20 | + |
| 21 | +def upgrade() -> None: |
| 22 | + op.execute(sa.text("UPDATE location SET type = lower(type), name = lower(name)")) |
| 23 | + op.execute(sa.text("UPDATE address SET url = lower(url)")) |
| 24 | + op.execute(sa.text("UPDATE job_type SET type = upper(type)")) |
| 25 | + |
| 26 | + op.create_index( |
| 27 | + "ix__dataset__location_id__name_lower", |
| 28 | + "dataset", |
| 29 | + ["location_id", sa.literal_column("lower(name)")], |
| 30 | + unique=True, |
| 31 | + ) |
| 32 | + op.drop_constraint(op.f("uq__dataset__location_id_name"), "dataset", type_="unique") |
| 33 | + op.drop_index(op.f("ix__dataset__location_id"), table_name="dataset") |
| 34 | + op.drop_index(op.f("ix__dataset__name"), table_name="dataset") |
| 35 | + |
| 36 | + op.create_index( |
| 37 | + "ix__job__location_id_name_lower", |
| 38 | + "job", |
| 39 | + ["location_id", sa.literal_column("lower(name)")], |
| 40 | + unique=True, |
| 41 | + ) |
| 42 | + op.drop_constraint(op.f("uq__job__location_id_name"), "job", type_="unique") |
| 43 | + op.drop_index(op.f("ix__job__location_id"), table_name="job") |
| 44 | + op.drop_index(op.f("ix__job__name"), table_name="job") |
| 45 | + |
| 46 | + op.create_index("ix__tag__name_lower", "tag", [sa.literal_column("lower(name)")], unique=True) |
| 47 | + op.drop_constraint(op.f("uq__tag__name"), "tag", type_="unique") |
| 48 | + |
| 49 | + op.create_index( |
| 50 | + "ix__tag_value__tag_id_value_lower", |
| 51 | + "tag_value", |
| 52 | + ["tag_id", sa.literal_column("lower(value)")], |
| 53 | + unique=True, |
| 54 | + ) |
| 55 | + op.drop_constraint(op.f("uq__tag_value__tag_id_value"), "tag_value", type_="unique") |
| 56 | + op.drop_index(op.f("ix__tag_value__tag_id"), table_name="tag_value") |
| 57 | + |
| 58 | + op.create_index("ix__user__name_lower", "user", [sa.literal_column("lower(name)")], unique=True) |
| 59 | + op.drop_index(op.f("ix__user__name"), table_name="user") |
| 60 | + |
| 61 | + |
| 62 | +def downgrade() -> None: |
| 63 | + op.create_index(op.f("ix__user__name"), "user", ["name"], unique=True) |
| 64 | + op.drop_index("ix__user__name_lower", table_name="user") |
| 65 | + |
| 66 | + op.create_unique_constraint( |
| 67 | + op.f("uq__tag_value__tag_id_value"), |
| 68 | + "tag_value", |
| 69 | + ["tag_id", "value"], |
| 70 | + postgresql_nulls_not_distinct=False, |
| 71 | + ) |
| 72 | + op.create_index(op.f("ix__tag_value__tag_id"), "tag_value", ["tag_id"], unique=False) |
| 73 | + op.drop_index("ix__tag_value__tag_id_value_lower", table_name="tag_value") |
| 74 | + |
| 75 | + op.create_unique_constraint(op.f("uq__tag__name"), "tag", ["name"]) |
| 76 | + op.drop_index("ix__tag__name_lower", table_name="tag") |
| 77 | + |
| 78 | + op.create_unique_constraint(op.f("uq__job_type__type"), "job_type", ["type"]) |
| 79 | + op.create_index(op.f("ix__job_type__type"), "job_type", ["type"], unique=False) |
| 80 | + op.drop_index("ix__job_type__type_lower", table_name="job_type") |
| 81 | + |
| 82 | + op.create_index(op.f("ix__job__location_id"), "job", ["location_id"], unique=False) |
| 83 | + op.create_index(op.f("ix__job__name"), "job", ["name"], unique=False) |
| 84 | + op.create_unique_constraint( |
| 85 | + op.f("uq__job__location_id_name"), |
| 86 | + "job", |
| 87 | + ["location_id", "name"], |
| 88 | + ) |
| 89 | + op.drop_index("ix__job__location_id_name_lower", table_name="job") |
| 90 | + |
| 91 | + op.create_unique_constraint( |
| 92 | + op.f("uq__dataset__location_id_name"), |
| 93 | + "dataset", |
| 94 | + ["location_id", "name"], |
| 95 | + ) |
| 96 | + op.create_index(op.f("ix__dataset__name"), "dataset", ["name"], unique=False) |
| 97 | + op.create_index(op.f("ix__dataset__location_id"), "dataset", ["location_id"], unique=False) |
| 98 | + op.drop_index("ix__dataset__location_id__name_lower", table_name="dataset") |
0 commit comments