201802012053_ec39ababe957_add_ondelete_cascades.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. """Add ondelete cascades
  2. Revision ID: ec39ababe957
  3. Revises: 7c3fcf8a3335
  4. Create Date: 2018-02-01 20:53:08.146277
  5. """
  6. from alembic import op
  7. import sqlalchemy as sa
  8. # revision identifiers, used by Alembic.
  9. revision = 'ec39ababe957'
  10. down_revision = '7c3fcf8a3335'
  11. branch_labels = ()
  12. depends_on = None
  13. def upgrade():
  14. # ### commands auto generated by Alembic - please adjust! ###
  15. with op.batch_alter_table('conversations', schema=None) as batch_op:
  16. batch_op.drop_constraint(None, type_='foreignkey')
  17. batch_op.create_foreign_key("fk_conversations_user_id", 'users', ['user_id'], ['id'], ondelete='CASCADE')
  18. with op.batch_alter_table('forumgroups', schema=None) as batch_op:
  19. batch_op.drop_constraint('fk_forum_id', type_='foreignkey')
  20. batch_op.drop_constraint(None, type_='foreignkey')
  21. batch_op.create_foreign_key(None, 'groups', ['group_id'], ['id'], ondelete='CASCADE')
  22. batch_op.create_foreign_key('fk_fg_forum_id', 'forums', ['forum_id'], ['id'], ondelete='CASCADE', use_alter=True)
  23. with op.batch_alter_table('forums', schema=None) as batch_op:
  24. batch_op.drop_constraint(None, type_='foreignkey')
  25. batch_op.drop_constraint(None, type_='foreignkey')
  26. batch_op.create_foreign_key(None, 'categories', ['category_id'], ['id'], ondelete='CASCADE')
  27. batch_op.create_foreign_key(None, 'users', ['last_post_user_id'], ['id'], ondelete='SET NULL')
  28. with op.batch_alter_table('forumsread', schema=None) as batch_op:
  29. batch_op.drop_constraint(None, type_='foreignkey')
  30. batch_op.drop_constraint('fk_fr_forum_id', type_='foreignkey')
  31. batch_op.create_foreign_key('fk_fr_forum_id', 'forums', ['forum_id'], ['id'], ondelete='CASCADE', use_alter=True)
  32. batch_op.create_foreign_key(None, 'users', ['user_id'], ['id'], ondelete='CASCADE')
  33. with op.batch_alter_table('groups_users', schema=None) as batch_op:
  34. batch_op.drop_constraint(None, type_='foreignkey')
  35. batch_op.drop_constraint(None, type_='foreignkey')
  36. batch_op.create_foreign_key(None, 'users', ['user_id'], ['id'], ondelete='CASCADE')
  37. batch_op.create_foreign_key(None, 'groups', ['group_id'], ['id'], ondelete='CASCADE')
  38. with op.batch_alter_table('messages', schema=None) as batch_op:
  39. batch_op.drop_constraint(None, type_='foreignkey')
  40. batch_op.drop_constraint(None, type_='foreignkey')
  41. batch_op.create_foreign_key(None, 'users', ['user_id'], ['id'], ondelete='CASCADE')
  42. batch_op.create_foreign_key(None, 'conversations', ['conversation_id'], ['id'], ondelete='CASCADE')
  43. with op.batch_alter_table('moderators', schema=None) as batch_op:
  44. batch_op.drop_constraint(None, type_='foreignkey')
  45. batch_op.drop_constraint('fk_forum_id', type_='foreignkey')
  46. batch_op.create_foreign_key('fk_mods_forum_id', 'forums', ['forum_id'], ['id'], ondelete='CASCADE', use_alter=True)
  47. batch_op.create_foreign_key(None, 'users', ['user_id'], ['id'], ondelete='CASCADE')
  48. with op.batch_alter_table('plugin_store', schema=None) as batch_op:
  49. batch_op.drop_constraint(None, type_='foreignkey')
  50. batch_op.create_foreign_key(None, 'plugin_registry', ['plugin_id'], ['id'], ondelete='CASCADE')
  51. with op.batch_alter_table('posts', schema=None) as batch_op:
  52. batch_op.alter_column('hidden',
  53. existing_type=sa.BOOLEAN(),
  54. nullable=True)
  55. with op.batch_alter_table('settings', schema=None) as batch_op:
  56. batch_op.drop_constraint('fk_settingsgroup', type_='foreignkey')
  57. batch_op.create_foreign_key('fk_settingsgroup', 'settingsgroup', ['settingsgroup'], ['key'], ondelete='CASCADE', use_alter=True)
  58. with op.batch_alter_table('topics', schema=None) as batch_op:
  59. batch_op.alter_column('hidden',
  60. existing_type=sa.BOOLEAN(),
  61. nullable=True)
  62. batch_op.drop_constraint('fk_topic_forum_id', type_='foreignkey')
  63. batch_op.create_foreign_key('fk_topic_forum_id', 'forums', ['forum_id'], ['id'], ondelete='CASCADE', use_alter=True)
  64. with op.batch_alter_table('topicsread', schema=None) as batch_op:
  65. batch_op.drop_constraint(None, type_='foreignkey')
  66. batch_op.drop_constraint('fk_tr_forum_id', type_='foreignkey')
  67. batch_op.drop_constraint('fk_tr_topic_id', type_='foreignkey')
  68. batch_op.create_foreign_key('fk_tr_topic_id', 'topics', ['topic_id'], ['id'], ondelete='CASCADE', use_alter=True)
  69. batch_op.create_foreign_key('fk_tr_forum_id', 'forums', ['forum_id'], ['id'], ondelete='CASCADE', use_alter=True)
  70. batch_op.create_foreign_key(None, 'users', ['user_id'], ['id'], ondelete='CASCADE')
  71. with op.batch_alter_table('topictracker', schema=None) as batch_op:
  72. batch_op.drop_constraint('fk_tracker_topic_id', type_='foreignkey')
  73. batch_op.drop_constraint(None, type_='foreignkey')
  74. batch_op.create_foreign_key(None, 'users', ['user_id'], ['id'], ondelete='CASCADE')
  75. batch_op.create_foreign_key('fk_tracker_topic_id', 'topics', ['topic_id'], ['id'], ondelete='CASCADE', use_alter=True)
  76. # ### end Alembic commands ###
  77. def downgrade():
  78. # ### commands auto generated by Alembic - please adjust! ###
  79. with op.batch_alter_table('topictracker', schema=None) as batch_op:
  80. batch_op.drop_constraint('fk_tracker_topic_id', type_='foreignkey')
  81. batch_op.drop_constraint(None, type_='foreignkey')
  82. batch_op.create_foreign_key(None, 'users', ['user_id'], ['id'])
  83. batch_op.create_foreign_key('fk_tracker_topic_id', 'topics', ['topic_id'], ['id'])
  84. with op.batch_alter_table('topicsread', schema=None) as batch_op:
  85. batch_op.drop_constraint(None, type_='foreignkey')
  86. batch_op.drop_constraint('fk_tr_forum_id', type_='foreignkey')
  87. batch_op.drop_constraint('fk_tr_topic_id', type_='foreignkey')
  88. batch_op.create_foreign_key('fk_tr_topic_id', 'topics', ['topic_id'], ['id'])
  89. batch_op.create_foreign_key('fk_tr_forum_id', 'forums', ['forum_id'], ['id'])
  90. batch_op.create_foreign_key(None, 'users', ['user_id'], ['id'])
  91. with op.batch_alter_table('topics', schema=None) as batch_op:
  92. batch_op.drop_constraint('fk_topic_forum_id', type_='foreignkey')
  93. batch_op.create_foreign_key('fk_topic_forum_id', 'forums', ['forum_id'], ['id'])
  94. batch_op.alter_column('hidden',
  95. existing_type=sa.BOOLEAN(),
  96. nullable=False)
  97. with op.batch_alter_table('settings', schema=None) as batch_op:
  98. batch_op.drop_constraint('fk_settingsgroup', type_='foreignkey')
  99. batch_op.create_foreign_key('fk_settingsgroup', 'settingsgroup', ['settingsgroup'], ['key'])
  100. with op.batch_alter_table('posts', schema=None) as batch_op:
  101. batch_op.alter_column('hidden',
  102. existing_type=sa.BOOLEAN(),
  103. nullable=False)
  104. with op.batch_alter_table('plugin_store', schema=None) as batch_op:
  105. batch_op.drop_constraint(None, type_='foreignkey')
  106. batch_op.create_foreign_key(None, 'plugin_registry', ['plugin_id'], ['id'])
  107. with op.batch_alter_table('moderators', schema=None) as batch_op:
  108. batch_op.drop_constraint(None, type_='foreignkey')
  109. batch_op.drop_constraint('fk_mods_forum_id', type_='foreignkey')
  110. batch_op.create_foreign_key('fk_forum_id', 'forums', ['forum_id'], ['id'])
  111. batch_op.create_foreign_key(None, 'users', ['user_id'], ['id'])
  112. with op.batch_alter_table('messages', schema=None) as batch_op:
  113. batch_op.drop_constraint(None, type_='foreignkey')
  114. batch_op.drop_constraint(None, type_='foreignkey')
  115. batch_op.create_foreign_key(None, 'users', ['user_id'], ['id'])
  116. batch_op.create_foreign_key(None, 'conversations', ['conversation_id'], ['id'])
  117. with op.batch_alter_table('groups_users', schema=None) as batch_op:
  118. batch_op.drop_constraint(None, type_='foreignkey')
  119. batch_op.drop_constraint(None, type_='foreignkey')
  120. batch_op.create_foreign_key(None, 'users', ['user_id'], ['id'])
  121. batch_op.create_foreign_key(None, 'groups', ['group_id'], ['id'])
  122. with op.batch_alter_table('forumsread', schema=None) as batch_op:
  123. batch_op.drop_constraint(None, type_='foreignkey')
  124. batch_op.drop_constraint('fk_fr_forum_id', type_='foreignkey')
  125. batch_op.create_foreign_key('fk_fr_forum_id', 'forums', ['forum_id'], ['id'])
  126. batch_op.create_foreign_key(None, 'users', ['user_id'], ['id'])
  127. with op.batch_alter_table('forums', schema=None) as batch_op:
  128. batch_op.drop_constraint(None, type_='foreignkey')
  129. batch_op.drop_constraint(None, type_='foreignkey')
  130. batch_op.create_foreign_key(None, 'categories', ['category_id'], ['id'])
  131. batch_op.create_foreign_key(None, 'users', ['last_post_user_id'], ['id'])
  132. with op.batch_alter_table('forumgroups', schema=None) as batch_op:
  133. batch_op.drop_constraint('fk_fg_forum_id', type_='foreignkey')
  134. batch_op.drop_constraint(None, type_='foreignkey')
  135. batch_op.create_foreign_key(None, 'groups', ['group_id'], ['id'])
  136. batch_op.create_foreign_key('fk_forum_id', 'forums', ['forum_id'], ['id'])
  137. with op.batch_alter_table('conversations', schema=None) as batch_op:
  138. batch_op.drop_constraint(None, type_='foreignkey')
  139. batch_op.create_foreign_key(None, 'users', ['user_id'], ['id'])
  140. # ### end Alembic commands ###