-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set naming conventions for database constraints (#70)
- Loading branch information
Showing
3 changed files
with
142 additions
and
3 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
backend/migrations/versions/2023_12_05_0951-808f2035f714_add_naming_conventions.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
"""Add naming conventions | ||
Revision ID: 808f2035f714 | ||
Revises: 8317277d4333 | ||
Create Date: 2023-12-05 09:51:28.009193+00:00 | ||
""" | ||
from alembic import op | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "808f2035f714" | ||
down_revision = "8317277d4333" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade() -> None: | ||
op.drop_index("ix_artefact_name", table_name="artefact") | ||
op.create_index(op.f("artefact_name_ix"), "artefact", ["name"], unique=False) | ||
op.drop_index("ix_artefact_build_architecture", table_name="artefact_build") | ||
op.create_index( | ||
op.f("artefact_build_architecture_ix"), | ||
"artefact_build", | ||
["architecture"], | ||
unique=False, | ||
) | ||
op.drop_constraint("unique_environment", "environment", type_="unique") | ||
op.create_unique_constraint( | ||
op.f("environment_name_architecture_key"), | ||
"environment", | ||
["name", "architecture"], | ||
) | ||
op.drop_index("ix_family_name", table_name="family") | ||
op.create_index(op.f("family_name_ix"), "family", ["name"], unique=True) | ||
op.drop_index("ix_stage_name", table_name="stage") | ||
op.create_index(op.f("stage_name_ix"), "stage", ["name"], unique=False) | ||
|
||
|
||
def downgrade() -> None: | ||
op.drop_index(op.f("stage_name_ix"), table_name="stage") | ||
op.create_index("ix_stage_name", "stage", ["name"], unique=False) | ||
op.drop_index(op.f("family_name_ix"), table_name="family") | ||
op.create_index("ix_family_name", "family", ["name"], unique=False) | ||
op.drop_constraint( | ||
op.f("environment_name_architecture_key"), "environment", type_="unique" | ||
) | ||
op.create_unique_constraint( | ||
"unique_environment", "environment", ["name", "architecture"] | ||
) | ||
op.drop_index(op.f("artefact_build_architecture_ix"), table_name="artefact_build") | ||
op.create_index( | ||
"ix_artefact_build_architecture", | ||
"artefact_build", | ||
["architecture"], | ||
unique=False, | ||
) | ||
op.drop_index(op.f("artefact_name_ix"), table_name="artefact") | ||
op.create_index("ix_artefact_name", "artefact", ["name"], unique=False) |
69 changes: 69 additions & 0 deletions
69
...end/migrations/versions/2023_12_05_1000-f59da052cbac_rename_unconventional_index_names.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
"""Rename unconventional index names | ||
Revision ID: f59da052cbac | ||
Revises: 808f2035f714 | ||
Create Date: 2023-12-05 10:00:21.171939+00:00 | ||
""" | ||
import sqlalchemy as sa | ||
from alembic import op | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "f59da052cbac" | ||
down_revision = "808f2035f714" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade() -> None: | ||
op.drop_index( | ||
"idx_artefact_id_architecture_null_revision", | ||
table_name="artefact_build", | ||
postgresql_where="(revision IS NULL)", | ||
) | ||
op.drop_index( | ||
"idx_artefact_id_architecture_revision", | ||
table_name="artefact_build", | ||
postgresql_where="(revision IS NOT NULL)", | ||
) | ||
op.create_index( | ||
op.f("artefact_build_artefact_id_architecture_ix"), | ||
"artefact_build", | ||
["artefact_id", "architecture"], | ||
unique=True, | ||
postgresql_where=sa.text("revision IS NULL"), | ||
) | ||
op.create_index( | ||
op.f("artefact_build_artefact_id_architecture_revision_ix"), | ||
"artefact_build", | ||
["artefact_id", "architecture", "revision"], | ||
unique=True, | ||
postgresql_where=sa.text("revision IS NOT NULL"), | ||
) | ||
|
||
|
||
def downgrade() -> None: | ||
op.drop_index( | ||
op.f("artefact_build_artefact_id_architecture_revision_ix"), | ||
table_name="artefact_build", | ||
postgresql_where=sa.text("revision IS NOT NULL"), | ||
) | ||
op.drop_index( | ||
op.f("artefact_build_artefact_id_architecture_ix"), | ||
table_name="artefact_build", | ||
postgresql_where=sa.text("revision IS NULL"), | ||
) | ||
op.create_index( | ||
"idx_artefact_id_architecture_revision", | ||
"artefact_build", | ||
["artefact_id", "architecture", "revision"], | ||
unique=False, | ||
postgresql_where="(revision IS NOT NULL)", | ||
) | ||
op.create_index( | ||
"idx_artefact_id_architecture_null_revision", | ||
"artefact_build", | ||
["artefact_id", "architecture"], | ||
unique=False, | ||
postgresql_where="(revision IS NULL)", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters