Script 6d0a02dc7de4_sync_desynced_migrations_with_models_py
[hide private]
[frames] | no frames]

Source Code for Script script-6d0a02dc7de4_sync_desynced_migrations_with_models_py

 1  """ 
 2  Sync desynced migrations with models 
 3   
 4  Revision ID: 6d0a02dc7de4 
 5  Revises: d2fbc2ab630d 
 6  Create Date: 2020-04-21 14:40:31.230986 
 7  """ 
 8   
 9  from alembic import op 
10   
11   
12  revision = '6d0a02dc7de4' 
13  down_revision = 'd2fbc2ab630d' 
14   
15   
16 -def _index_missing(bind, index_name):
17 return not ( 18 bind.execute("SELECT count(*) FROM pg_indexes " 19 "WHERE indexname = '{}'".format(index_name)) 20 .first()[0] 21 )
22 23
24 -def upgrade():
25 bind = op.get_bind() 26 27 # sorry, PostgreSQL only 28 assert bind.dialect.name == 'postgresql' 29 30 # We need the `ix_` naming, otherwise `alembic revision --autogenerate` 31 # doesn't match the existing index with our model. 32 bind.execute("ALTER INDEX IF EXISTS build_copr_id RENAME TO ix_build_copr_id") 33 bind.execute("ALTER INDEX IF EXISTS build_package_idx RENAME TO ix_build_package_id") 34 bind.execute("ALTER INDEX IF EXISTS build_user_id_idx RENAME TO ix_build_user_id") 35 bind.execute("ALTER INDEX IF EXISTS build_chroot_build_id RENAME TO ix_build_chroot_build_id") 36 bind.execute("ALTER INDEX IF EXISTS copr_user_id_idx RENAME TO ix_copr_user_id") 37 bind.execute("ALTER INDEX IF EXISTS copr_chroot_copr_id RENAME TO ix_copr_chroot_copr_id") 38 bind.execute("ALTER INDEX IF EXISTS copr_permission_copr_id RENAME TO ix_copr_permission_copr_id") 39 bind.execute("ALTER INDEX IF EXISTS legal_flag_resolved_on RENAME TO ix_legal_flag_resolved_on") 40 bind.execute("ALTER INDEX IF EXISTS package_copr_id_idx RENAME TO ix_package_copr_id") 41 42 # Those are pretty good to have. 43 op.create_foreign_key(None, 'build', 'package', ['package_id'], ['id']) 44 op.create_foreign_key(None, 'copr', 'group', ['group_id'], ['id']) 45 46 # Fixing issue #1346, this is redundant. 47 if not _index_missing(bind, 'unique_name_stream_version_copr_id'): 48 op.drop_constraint('unique_name_stream_version_copr_id', 'module', 49 type_='unique') 50 51 # We lived without this because FAS/Kerberos provides unique usernames, but 52 # it's better to have it. 53 op.create_unique_constraint(None, 'user', ['username']) 54 55 # For some reaons some of those were missing in some of our Copr instances. 56 if _index_missing(bind, 'action_result_action_type'): 57 op.create_index('action_result_action_type', 'action', ['result', 'action_type'], unique=False) 58 if _index_missing(bind, 'ix_build_chroot_build_id'): 59 op.create_index(op.f('ix_build_chroot_build_id'), 'build_chroot', ['build_id'], unique=False) 60 if _index_missing(bind, 'copr_deleted_name'): 61 op.create_index('copr_deleted_name', 'copr', ['deleted', 'name'], unique=False) 62 if _index_missing(bind, 'ix_copr_chroot_copr_id'): 63 op.create_index(op.f('ix_copr_chroot_copr_id'), 'copr_chroot', ['copr_id'], unique=False) 64 if _index_missing(bind, 'ix_copr_permission_copr_id'): 65 op.create_index(op.f('ix_copr_permission_copr_id'), 'copr_permission', ['copr_id'], unique=False) 66 if _index_missing(bind, 'ix_legal_flag_resolved_on'): 67 op.create_index(op.f('ix_legal_flag_resolved_on'), 'legal_flag', ['resolved_on'], unique=False)
68
69 -def downgrade():
70 """ no way back, sorry """
71