| |
@@ -0,0 +1,102 @@
|
| |
+ """Add DB naming conventions
|
| |
+
|
| |
+ Revision ID: bc58ec7c7d31
|
| |
+ Revises: d7b6117a119a
|
| |
+ Create Date: 2021-07-22 18:21:27.437519
|
| |
+
|
| |
+ """
|
| |
+ from alembic import op
|
| |
+
|
| |
+ # revision identifiers, used by Alembic.
|
| |
+ revision = 'bc58ec7c7d31'
|
| |
+ down_revision = 'd7b6117a119a'
|
| |
+
|
| |
+ # constraint renames {table_name: [(from, to)]}
|
| |
+ renames = {
|
| |
+ 'bug': [
|
| |
+ ('bug_pkey', 'pk_bug'),
|
| |
+ ('bug_milestone_id_fkey', 'fk_bug_milestone_id_milestone')
|
| |
+ ],
|
| |
+ 'build': [
|
| |
+ ('build_pkey', 'pk_build'),
|
| |
+ ],
|
| |
+ 'criterion': [
|
| |
+ ('criterion_pkey', 'pk_criterion'),
|
| |
+ ('criterion_milestone_id_fkey', 'fk_criterion_milestone_id_milestone'),
|
| |
+ ],
|
| |
+ 'milestone': [
|
| |
+ ('milestone_pkey', 'pk_milestone'),
|
| |
+ ('milestone_release_id_fkey', 'fk_milestone_release_id_release'),
|
| |
+ ('milestone_succeeds_id_fkey', 'fk_milestone_succeeds_id_milestone'),
|
| |
+ ],
|
| |
+ 'release': [
|
| |
+ ('release_pkey', 'pk_release'),
|
| |
+ ],
|
| |
+ 'update': [
|
| |
+ ('update_pkey', 'pk_update'),
|
| |
+ ('update_release_id_fkey', 'fk_update_release_id_release'),
|
| |
+ ],
|
| |
+ 'update_fixes': [
|
| |
+ ('update_fixes_bug_id_fkey', 'fk_update_fixes_bug_id_bug'),
|
| |
+ ('update_fixes_update_id_fkey', 'fk_update_fixes_update_id_update'),
|
| |
+ ],
|
| |
+ 'update_milestones': [
|
| |
+ ('update_milestones_milestone_id_fkey', 'fk_update_milestones_milestone_id_milestone'),
|
| |
+ ('update_milestones_update_id_fkey', 'fk_update_milestones_update_id_update'),
|
| |
+ ],
|
| |
+ 'user_info': [
|
| |
+ ('user_info_pkey', 'pk_user_info'),
|
| |
+ ],
|
| |
+ }
|
| |
+
|
| |
+
|
| |
+ def upgrade():
|
| |
+ # rename all constraints which are not autodetected by Alembic
|
| |
+ for table, renamelist in renames.items():
|
| |
+ for from_, to in renamelist:
|
| |
+ op.execute(f'ALTER TABLE "{table}" RENAME CONSTRAINT {from_} TO {to}')
|
| |
+
|
| |
+ # ### commands auto generated by Alembic - please adjust! ###
|
| |
+ op.drop_constraint('build_nvr_key', 'build', type_='unique')
|
| |
+ op.drop_constraint('build_pkg_ver_rel', 'build', type_='unique')
|
| |
+ op.create_unique_constraint(op.f('uq_build_nvr'), 'build', ['nvr'])
|
| |
+ op.create_unique_constraint('uq_build_pkg_ver_rel', 'build', ['pkg_name', 'version', 'release'])
|
| |
+ op.drop_constraint('milestone_blocker_tracker_key', 'milestone', type_='unique')
|
| |
+ op.drop_constraint('milestone_fe_tracker_key', 'milestone', type_='unique')
|
| |
+ op.drop_constraint('milestone_name_key', 'milestone', type_='unique')
|
| |
+ op.create_unique_constraint(op.f('uq_milestone_blocker_tracker'), 'milestone', ['blocker_tracker'])
|
| |
+ op.create_unique_constraint(op.f('uq_milestone_fe_tracker'), 'milestone', ['fe_tracker'])
|
| |
+ op.create_unique_constraint(op.f('uq_milestone_name'), 'milestone', ['name'])
|
| |
+ op.drop_constraint('user_info_api_key_hash_key', 'user_info', type_='unique')
|
| |
+ op.drop_constraint('user_info_bz_user_key', 'user_info', type_='unique')
|
| |
+ op.drop_constraint('user_info_fas_login_key', 'user_info', type_='unique')
|
| |
+ op.create_unique_constraint(op.f('uq_user_info_api_key_hash'), 'user_info', ['api_key_hash'])
|
| |
+ op.create_unique_constraint(op.f('uq_user_info_bz_user'), 'user_info', ['bz_user'])
|
| |
+ op.create_unique_constraint(op.f('uq_user_info_fas_login'), 'user_info', ['fas_login'])
|
| |
+ # ### end Alembic commands ###
|
| |
+
|
| |
+
|
| |
+ def downgrade():
|
| |
+ # ### commands auto generated by Alembic - please adjust! ###
|
| |
+ op.drop_constraint(op.f('uq_user_info_fas_login'), 'user_info', type_='unique')
|
| |
+ op.drop_constraint(op.f('uq_user_info_bz_user'), 'user_info', type_='unique')
|
| |
+ op.drop_constraint(op.f('uq_user_info_api_key_hash'), 'user_info', type_='unique')
|
| |
+ op.create_unique_constraint('user_info_fas_login_key', 'user_info', ['fas_login'])
|
| |
+ op.create_unique_constraint('user_info_bz_user_key', 'user_info', ['bz_user'])
|
| |
+ op.create_unique_constraint('user_info_api_key_hash_key', 'user_info', ['api_key_hash'])
|
| |
+ op.drop_constraint(op.f('uq_milestone_name'), 'milestone', type_='unique')
|
| |
+ op.drop_constraint(op.f('uq_milestone_fe_tracker'), 'milestone', type_='unique')
|
| |
+ op.drop_constraint(op.f('uq_milestone_blocker_tracker'), 'milestone', type_='unique')
|
| |
+ op.create_unique_constraint('milestone_name_key', 'milestone', ['name'])
|
| |
+ op.create_unique_constraint('milestone_fe_tracker_key', 'milestone', ['fe_tracker'])
|
| |
+ op.create_unique_constraint('milestone_blocker_tracker_key', 'milestone', ['blocker_tracker'])
|
| |
+ op.drop_constraint('uq_build_pkg_ver_rel', 'build', type_='unique')
|
| |
+ op.drop_constraint(op.f('uq_build_nvr'), 'build', type_='unique')
|
| |
+ op.create_unique_constraint('build_pkg_ver_rel', 'build', ['pkg_name', 'version', 'release'])
|
| |
+ op.create_unique_constraint('build_nvr_key', 'build', ['nvr'])
|
| |
+ # ### end Alembic commands ###
|
| |
+
|
| |
+ # rename all constraints which are not autodetected by Alembic
|
| |
+ for table, renamelist in renames.items():
|
| |
+ for from_, to in renamelist:
|
| |
+ op.execute(f'ALTER TABLE "{table}" RENAME CONSTRAINT {to} TO {from_}')
|
| |
Without naming conventions, column constraints created with e.g.
unique=True
are not named, and therefore future edits of those constraints (or e.g. database
downgrades) fail, unless you figure out the constraint name used in your
particular database and add it into each new Alembic script.
A better approach is to define a naming convention. SQLAlchemy then names all
constraints according to this convention, and Alembic scripts work
out-of-the-box even for constraint changes and downgrades. You can read more
about it at:
https://docs.sqlalchemy.org/en/14/core/constraints.html#configuring-constraint-naming-conventions
This patch adds a default naming convention (as suggested by SQLAlchemy docs),
and adds an Alembic script that performs a one-time rename of all affected
constraints. Unique constraints were detected by Alembic's autogeneration
feature, primary and foreign keys were renamed manually.