201802021027_af3f5579c84d_add_cascades.py 14 KB

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