201802021027_af3f5579c84d_add_cascades.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. """Add cascades
  2. Revision ID: af3f5579c84d
  3. Revises: 7c3fcf8a3335
  4. Create Date: 2018-02-02 10:27:50.290095
  5. """
  6. import logging
  7. from alembic import op
  8. import sqlalchemy as sa
  9. logger = logging.getLogger("alembic.runtime.migration")
  10. # revision identifiers, used by Alembic.
  11. revision = 'af3f5579c84d'
  12. down_revision = '7c3fcf8a3335'
  13. branch_labels = ()
  14. depends_on = None
  15. def upgrade():
  16. # ### commands auto generated by Alembic - please adjust! ###
  17. con = op.get_bind()
  18. if con.engine.dialect.name == "sqlite":
  19. # its not possible to remove unnamed constraints...
  20. logger.warning("SQLite is only partially supported for revision {}."
  21. .format(revision))
  22. with op.batch_alter_table('conversations', schema=None) as batch_op:
  23. if con.engine.dialect.name == "mysql":
  24. # user_id
  25. batch_op.drop_constraint("conversations_ibfk_3", type_='foreignkey')
  26. elif con.engine.dialect.name == "postgresql":
  27. batch_op.drop_constraint('conversations_user_id_fkey', type_='foreignkey')
  28. batch_op.create_foreign_key(batch_op.f('fk_conversations_user_id_users'), 'users', ['user_id'], ['id'], ondelete='CASCADE')
  29. with op.batch_alter_table('forumgroups', schema=None) as batch_op:
  30. if con.engine.dialect.name == "sqlite":
  31. batch_op.drop_constraint('fk_forum_id', type_='foreignkey')
  32. elif con.engine.dialect.name == "mysql":
  33. # group_id
  34. batch_op.drop_constraint("forumgroups_ibfk_1", type_='foreignkey')
  35. elif con.engine.dialect.name == "postgresql":
  36. batch_op.drop_constraint('forumgroups_group_id_fkey', type_='foreignkey')
  37. batch_op.create_foreign_key(batch_op.f('fk_forumgroups_group_id_groups'), 'groups', ['group_id'], ['id'], ondelete='CASCADE')
  38. batch_op.create_foreign_key(batch_op.f('fk_forumgroups_forum_id_forums'), 'forums', ['forum_id'], ['id'], ondelete='CASCADE')
  39. with op.batch_alter_table('forums', schema=None) as batch_op:
  40. if con.engine.dialect.name == "mysql":
  41. # category_id
  42. batch_op.drop_constraint("forums_ibfk_1", type_='foreignkey')
  43. # last_post_user_id
  44. batch_op.drop_constraint("forums_ibfk_3", type_='foreignkey')
  45. elif con.engine.dialect.name == "postgresql":
  46. batch_op.drop_constraint('forums_category_id_fkey', type_='foreignkey')
  47. batch_op.drop_constraint('forums_last_post_user_id_fkey', type_='foreignkey')
  48. batch_op.create_foreign_key(batch_op.f('fk_forums_category_id_categories'), 'categories', ['category_id'], ['id'], ondelete='CASCADE')
  49. batch_op.create_foreign_key(batch_op.f('fk_forums_last_post_user_id_users'), 'users', ['last_post_user_id'], ['id'], ondelete='SET NULL')
  50. with op.batch_alter_table('forumsread', schema=None) as batch_op:
  51. if con.engine.dialect.name == "sqlite":
  52. batch_op.drop_constraint('fk_fr_forum_id', type_='foreignkey')
  53. elif con.engine.dialect.name == "mysql":
  54. # user_id
  55. batch_op.drop_constraint("forumsread_ibfk_1", type_='foreignkey')
  56. elif con.engine.dialect.name == "postgresql":
  57. batch_op.drop_constraint('forumsread_user_id_fkey', type_='foreignkey')
  58. batch_op.create_foreign_key(batch_op.f('fk_forumsread_forum_id_forums'), 'forums', ['forum_id'], ['id'], ondelete='CASCADE')
  59. batch_op.create_foreign_key(batch_op.f('fk_forumsread_user_id_users'), 'users', ['user_id'], ['id'], ondelete='CASCADE')
  60. with op.batch_alter_table('groups_users', schema=None) as batch_op:
  61. if con.engine.dialect.name == "mysql":
  62. # group_id
  63. batch_op.drop_constraint("groups_users_ibfk_1", type_='foreignkey')
  64. # user_id
  65. batch_op.drop_constraint("groups_users_ibfk_2", type_='foreignkey')
  66. elif con.engine.dialect.name == "postgresql":
  67. batch_op.drop_constraint('groups_users_group_id_fkey', type_='foreignkey')
  68. batch_op.drop_constraint('groups_users_user_id_fkey', type_='foreignkey')
  69. batch_op.create_foreign_key(batch_op.f('fk_groups_users_user_id_users'), 'users', ['user_id'], ['id'], ondelete='CASCADE')
  70. batch_op.create_foreign_key(batch_op.f('fk_groups_users_group_id_groups'), 'groups', ['group_id'], ['id'], ondelete='CASCADE')
  71. with op.batch_alter_table('messages', schema=None) as batch_op:
  72. if con.engine.dialect.name == "mysql":
  73. # conversation_id
  74. batch_op.drop_constraint("messages_ibfk_1", type_='foreignkey')
  75. # user_id
  76. batch_op.drop_constraint("messages_ibfk_2", type_='foreignkey')
  77. elif con.engine.dialect.name == "postgresql":
  78. batch_op.drop_constraint('messages_conversation_id_fkey', type_='foreignkey')
  79. batch_op.drop_constraint('messages_user_id_fkey', type_='foreignkey')
  80. batch_op.create_foreign_key(batch_op.f('fk_messages_conversation_id_conversations'), 'conversations', ['conversation_id'], ['id'], ondelete='CASCADE')
  81. batch_op.create_foreign_key(batch_op.f('fk_messages_user_id_users'), 'users', ['user_id'], ['id'], ondelete='CASCADE')
  82. with op.batch_alter_table('moderators', schema=None) as batch_op:
  83. if con.engine.dialect.name == "sqlite":
  84. batch_op.drop_constraint('fk_forum_id', type_='foreignkey')
  85. elif con.engine.dialect.name == "mysql":
  86. # user_id
  87. batch_op.drop_constraint("moderators_ibfk_1", type_='foreignkey')
  88. elif con.engine.dialect.name == "postgresql":
  89. batch_op.drop_constraint('moderators_user_id_fkey', type_='foreignkey')
  90. batch_op.create_foreign_key(batch_op.f('fk_moderators_forum_id_forums'), 'forums', ['forum_id'], ['id'], ondelete='CASCADE')
  91. batch_op.create_foreign_key(batch_op.f('fk_moderators_user_id_users'), 'users', ['user_id'], ['id'], ondelete='CASCADE')
  92. with op.batch_alter_table('plugin_store', schema=None) as batch_op:
  93. if con.engine.dialect.name == "mysql":
  94. # plugin_id
  95. batch_op.drop_constraint("plugin_store_ibfk_1", type_='foreignkey')
  96. elif con.engine.dialect.name == "postgresql":
  97. batch_op.drop_constraint('plugin_store_plugin_id_fkey', type_='foreignkey')
  98. batch_op.create_foreign_key(batch_op.f('fk_plugin_store_plugin_id_plugin_registry'), 'plugin_registry', ['plugin_id'], ['id'], ondelete='CASCADE')
  99. with op.batch_alter_table('posts', schema=None) as batch_op:
  100. if con.engine.dialect.name == "sqlite":
  101. batch_op.drop_constraint('fk_post_topic_id', type_='foreignkey')
  102. batch_op.create_foreign_key(batch_op.f('fk_posts_topic_id_topics'), 'topics', ['topic_id'], ['id'], ondelete='CASCADE')
  103. with op.batch_alter_table('settings', schema=None) as batch_op:
  104. if con.engine.dialect.name == "sqlite":
  105. batch_op.drop_constraint('fk_settingsgroup', type_='foreignkey')
  106. batch_op.create_foreign_key(batch_op.f('fk_settings_settingsgroup_settingsgroup'), 'settingsgroup', ['settingsgroup'], ['key'], ondelete='CASCADE')
  107. with op.batch_alter_table('topics', schema=None) as batch_op:
  108. if con.engine.dialect.name == "sqlite":
  109. batch_op.drop_constraint('fk_topic_forum_id', type_='foreignkey')
  110. batch_op.create_foreign_key(batch_op.f('fk_topics_forum_id_forums'), 'forums', ['forum_id'], ['id'], ondelete='CASCADE')
  111. with op.batch_alter_table('topicsread', schema=None) as batch_op:
  112. if con.engine.dialect.name == "sqlite":
  113. batch_op.drop_constraint('fk_tr_forum_id', type_='foreignkey')
  114. batch_op.drop_constraint('fk_tr_topic_id', type_='foreignkey')
  115. elif con.engine.dialect.name == "mysql":
  116. # user_id
  117. batch_op.drop_constraint("topicsread_ibfk_1", type_='foreignkey')
  118. elif con.engine.dialect.name == "postgresql":
  119. batch_op.drop_constraint('topicsread_user_id_fkey', type_='foreignkey')
  120. batch_op.create_foreign_key(batch_op.f('fk_topicsread_topic_id_topics'), 'topics', ['topic_id'], ['id'], ondelete='CASCADE')
  121. batch_op.create_foreign_key(batch_op.f('fk_topicsread_forum_id_forums'), 'forums', ['forum_id'], ['id'], ondelete='CASCADE')
  122. batch_op.create_foreign_key(batch_op.f('fk_topicsread_user_id_users'), 'users', ['user_id'], ['id'], ondelete='CASCADE')
  123. with op.batch_alter_table('topictracker', schema=None) as batch_op:
  124. if con.engine.dialect.name == "sqlite":
  125. batch_op.drop_constraint('fk_tracker_topic_id', type_='foreignkey')
  126. elif con.engine.dialect.name == "mysql":
  127. # user_id
  128. batch_op.drop_constraint("topictracker_ibfk_1", type_='foreignkey')
  129. elif con.engine.dialect.name == "postgresql":
  130. batch_op.drop_constraint('topictracker_user_id_fkey', type_='foreignkey')
  131. batch_op.create_foreign_key(batch_op.f('fk_topictracker_topic_id_topics'), 'topics', ['topic_id'], ['id'], ondelete='CASCADE')
  132. batch_op.create_foreign_key(batch_op.f('fk_topictracker_user_id_users'), 'users', ['user_id'], ['id'], ondelete='CASCADE')
  133. # ### end Alembic commands ###
  134. def downgrade():
  135. # ### commands auto generated by Alembic - please adjust! ###
  136. with op.batch_alter_table('topictracker', schema=None) as batch_op:
  137. batch_op.drop_constraint(batch_op.f('fk_topictracker_user_id_users'), type_='foreignkey')
  138. batch_op.drop_constraint(batch_op.f('fk_topictracker_topic_id_topics'), type_='foreignkey')
  139. batch_op.create_foreign_key('fk_topictracker_user_id_users', 'users', ['user_id'], ['id'])
  140. with op.batch_alter_table('topicsread', schema=None) as batch_op:
  141. batch_op.drop_constraint(batch_op.f('fk_topicsread_user_id_users'), type_='foreignkey')
  142. batch_op.drop_constraint(batch_op.f('fk_topicsread_forum_id_forums'), type_='foreignkey')
  143. batch_op.drop_constraint(batch_op.f('fk_topicsread_topic_id_topics'), type_='foreignkey')
  144. batch_op.create_foreign_key('fk_topicsread_user_id_users', 'users', ['user_id'], ['id'])
  145. with op.batch_alter_table('topics', schema=None) as batch_op:
  146. batch_op.drop_constraint(batch_op.f('fk_topics_forum_id_forums'), type_='foreignkey')
  147. with op.batch_alter_table('settings', schema=None) as batch_op:
  148. batch_op.drop_constraint(batch_op.f('fk_settings_settingsgroup_settingsgroup'), type_='foreignkey')
  149. with op.batch_alter_table('posts', schema=None) as batch_op:
  150. batch_op.drop_constraint(batch_op.f('fk_posts_topic_id_topics'), type_='foreignkey')
  151. with op.batch_alter_table('plugin_store', schema=None) as batch_op:
  152. batch_op.drop_constraint(batch_op.f('fk_plugin_store_plugin_id_plugin_registry'), type_='foreignkey')
  153. batch_op.create_foreign_key('fk_plugin_store_plugin_id_plugin_registry', 'plugin_registry', ['plugin_id'], ['id'])
  154. with op.batch_alter_table('moderators', schema=None) as batch_op:
  155. batch_op.drop_constraint(batch_op.f('fk_moderators_user_id_users'), type_='foreignkey')
  156. batch_op.drop_constraint(batch_op.f('fk_moderators_forum_id_forums'), type_='foreignkey')
  157. batch_op.create_foreign_key('fk_moderators_user_id_users', 'users', ['user_id'], ['id'])
  158. with op.batch_alter_table('messages', schema=None) as batch_op:
  159. batch_op.drop_constraint(batch_op.f('fk_messages_user_id_users'), type_='foreignkey')
  160. batch_op.drop_constraint(batch_op.f('fk_messages_conversation_id_conversations'), type_='foreignkey')
  161. batch_op.create_foreign_key('fk_messages_user_id_users', 'users', ['user_id'], ['id'])
  162. batch_op.create_foreign_key('fk_messages_conversation_id_conversations', 'conversations', ['conversation_id'], ['id'])
  163. with op.batch_alter_table('groups_users', schema=None) as batch_op:
  164. batch_op.drop_constraint(batch_op.f('fk_groups_users_group_id_groups'), type_='foreignkey')
  165. batch_op.drop_constraint(batch_op.f('fk_groups_users_user_id_users'), type_='foreignkey')
  166. batch_op.create_foreign_key('fk_groups_users_user_id_users', 'users', ['user_id'], ['id'])
  167. batch_op.create_foreign_key('fk_groups_users_group_id_groups', 'groups', ['group_id'], ['id'])
  168. with op.batch_alter_table('forumsread', schema=None) as batch_op:
  169. batch_op.drop_constraint(batch_op.f('fk_forumsread_user_id_users'), type_='foreignkey')
  170. batch_op.drop_constraint(batch_op.f('fk_forumsread_forum_id_forums'), type_='foreignkey')
  171. batch_op.create_foreign_key('fk_forumsread_user_id_users', 'users', ['user_id'], ['id'])
  172. with op.batch_alter_table('forums', schema=None) as batch_op:
  173. batch_op.drop_constraint(batch_op.f('fk_forums_last_post_user_id_users'), type_='foreignkey')
  174. batch_op.drop_constraint(batch_op.f('fk_forums_category_id_categories'), type_='foreignkey')
  175. batch_op.create_foreign_key('fk_forums_last_post_user_id_users', 'users', ['last_post_user_id'], ['id'])
  176. batch_op.create_foreign_key('fk_forums_category_id_categories', 'categories', ['category_id'], ['id'])
  177. with op.batch_alter_table('forumgroups', schema=None) as batch_op:
  178. batch_op.drop_constraint(batch_op.f('fk_forumgroups_forum_id_forums'), type_='foreignkey')
  179. batch_op.drop_constraint(batch_op.f('fk_forumgroups_group_id_groups'), type_='foreignkey')
  180. batch_op.create_foreign_key('fk_forumgroups_group_id_groups', 'groups', ['group_id'], ['id'])
  181. with op.batch_alter_table('conversations', schema=None) as batch_op:
  182. batch_op.drop_constraint(batch_op.f('fk_conversations_user_id_users'), type_='foreignkey')
  183. batch_op.create_foreign_key('fk_conversations_user_id_users', 'users', ['user_id'], ['id'])
  184. # ### end Alembic commands ###