201802021027_af3f5579c84d_add_cascades.py 14 KB

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