Clworld

Patch SQL for #3562 "Add all missing foreign keys"

Jun 5th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 4.95 KB | None | 0 0
  1. -- Patch SQL for migration by #3562 "Add all missing foreign keys" https://github.com/tootsuite/mastodon/commit/4c06d1cb241020f2407a6be01b584c508a06ffe9
  2. -- You may need additional SQL for your environment. This SQL works for me.
  3.  
  4. -- statuses.in_reply_to の statuses が無い
  5. START TRANSACTION;
  6. -- 件数確認 / check count
  7. SELECT COUNT(*)
  8. FROM statuses s
  9. LEFT JOIN statuses rs ON rs.id = s.in_reply_to_id
  10. WHERE s.in_reply_to_id IS NOT NULL
  11. AND   rs.id IS NULL;
  12. -- on_delete: nullify
  13. UPDATE statuses SET in_reply_to_id = NULL
  14. FROM statuses s
  15. LEFT JOIN statuses rs ON rs.id = s.in_reply_to_id
  16. WHERE statuses.id = s.id
  17. AND   s.in_reply_to_id IS NOT NULL
  18. AND   rs.id IS NULL;
  19. -- 更新後件数確認(0件のはず) / re-check count(must be zero.)
  20. SELECT COUNT(*)
  21. FROM statuses s
  22. LEFT JOIN statuses rs ON rs.id = s.in_reply_to_id
  23. WHERE s.in_reply_to_id IS NOT NULL
  24. AND   rs.id IS NULL;
  25.  
  26. COMMIT;
  27.  
  28. -- follow_requests.target_account_id の accounts が無い
  29. START TRANSACTION;
  30. -- 件数確認 / check count
  31. SELECT COUNT(*)
  32. FROM follow_requests fr
  33. LEFT JOIN accounts a ON a.id = fr.target_account_id
  34. WHERE fr.target_account_id IS NOT NULL
  35. AND   a.id IS NULL;
  36. -- on_delete: cascade
  37. DELETE FROM follow_requests WHERE id IN (
  38.   SELECT fr.id
  39.   FROM follow_requests fr
  40.   LEFT JOIN accounts a ON a.id = fr.target_account_id
  41.   WHERE fr.target_account_id IS NOT NULL
  42.   AND   a.id IS NULL
  43. );
  44. -- 更新後件数確認(0件のはず) / re-check count(must be zero.)
  45. SELECT COUNT(*)
  46. FROM follow_requests fr
  47. LEFT JOIN accounts a ON a.id = fr.target_account_id
  48. WHERE fr.target_account_id IS NOT NULL
  49. AND   a.id IS NULL;
  50.  
  51. COMMIT;
  52.  
  53. -- media_attachments.status_id の statuses が無い
  54. START TRANSACTION;
  55. -- 件数確認 / check count
  56. SELECT COUNT(*)
  57. FROM media_attachments ma
  58. LEFT JOIN statuses s ON s.id = ma.status_id
  59. WHERE ma.status_id IS NOT NULL
  60. AND   s.id IS NULL;
  61. -- on_delete: nullify
  62. UPDATE media_attachments SET status_id = NULL
  63. FROM media_attachments ma
  64. LEFT JOIN statuses s ON s.id = ma.status_id
  65. WHERE media_attachments.id = ma.id
  66. AND   ma.status_id IS NOT NULL
  67. AND   s.id IS NULL;
  68. -- 更新後件数確認(0件のはず) / re-check count(must be zero.)
  69. SELECT COUNT(*)
  70. FROM media_attachments ma
  71. LEFT JOIN statuses s ON s.id = ma.status_id
  72. WHERE ma.status_id IS NOT NULL
  73. AND   s.id IS NULL;
  74.  
  75. COMMIT;
  76.  
  77. -- mentions.status_id の statuses が無い
  78. START TRANSACTION;
  79. -- 件数確認 / check count
  80. SELECT COUNT(*)
  81. FROM mentions m
  82. LEFT JOIN statuses s ON s.id = m.status_id
  83. WHERE m.status_id IS NOT NULL
  84. AND   s.id IS NULL;
  85. -- on_delete: cascade
  86. DELETE FROM mentions WHERE id IN (
  87.   SELECT m.id
  88.   FROM mentions m
  89.   LEFT JOIN statuses s ON s.id = m.status_id
  90.   WHERE m.status_id IS NOT NULL
  91.   AND   s.id IS NULL
  92. );
  93. -- 更新後件数確認(0件のはず) / re-check count(must be zero.)
  94. SELECT COUNT(*)
  95. FROM mentions m
  96. LEFT JOIN statuses s ON s.id = m.status_id
  97. WHERE m.status_id IS NOT NULL
  98. AND   s.id IS NULL;
  99.  
  100. COMMIT;
  101.  
  102. -- preview_cards.status_id の statuses が無い
  103. START TRANSACTION;
  104. -- 件数確認 / check count
  105. SELECT COUNT(*)
  106. FROM preview_cards pc
  107. LEFT JOIN statuses s ON s.id = pc.status_id
  108. WHERE pc.status_id IS NOT NULL
  109. AND   s.id IS NULL;
  110. -- on_delete: cascade
  111. DELETE FROM preview_cards WHERE id IN (
  112.   SELECT pc.id
  113.   FROM preview_cards pc
  114.   LEFT JOIN statuses s ON s.id = pc.status_id
  115.   WHERE pc.status_id IS NOT NULL
  116.   AND   s.id IS NULL
  117. );
  118. -- 更新後件数確認(0件のはず) / re-check count(must be zero.)
  119. SELECT COUNT(*)
  120. FROM preview_cards pc
  121. LEFT JOIN statuses s ON s.id = pc.status_id
  122. WHERE pc.status_id IS NOT NULL
  123. AND   s.id IS NULL;
  124.  
  125. COMMIT;
  126.  
  127. -- web_settings.user_id の users が無い
  128. START TRANSACTION;
  129. -- 件数確認 / check count
  130. SELECT COUNT(*)
  131. FROM web_settings ws
  132. LEFT JOIN users u ON u.id = ws.user_id
  133. WHERE ws.user_id IS NOT NULL
  134. AND   u.id IS NULL;
  135. -- on_delete: cascade
  136. DELETE FROM web_settings WHERE id IN (
  137.   SELECT ws.id
  138.   FROM web_settings ws
  139.   LEFT JOIN users u ON u.id = ws.user_id
  140.   WHERE ws.user_id IS NOT NULL
  141.   AND   u.id IS NULL
  142. );
  143. -- 更新後件数確認(0件のはず) / re-check count(must be zero.)
  144. SELECT COUNT(*)
  145. FROM web_settings ws
  146. LEFT JOIN users u ON u.id = ws.user_id
  147. WHERE ws.user_id IS NOT NULL
  148. AND   u.id IS NULL;
  149.  
  150. COMMIT;
  151.  
  152. -- oauth_access_tokens.resource_owner_id の users が無い
  153. START TRANSACTION;
  154. -- 件数確認 / check count
  155. SELECT COUNT(*)
  156. FROM oauth_access_tokens o
  157. LEFT JOIN users u ON u.id = o.resource_owner_id
  158. WHERE o.resource_owner_id IS NOT NULL
  159. AND   u.id IS NULL;
  160. -- on_delete: cascade
  161. DELETE FROM oauth_access_tokens WHERE id IN (
  162.   SELECT o.id
  163.   FROM oauth_access_tokens o
  164.   LEFT JOIN users u ON u.id = o.resource_owner_id
  165.   WHERE o.resource_owner_id IS NOT NULL
  166.   AND   u.id IS NULL
  167. );
  168. -- 更新後件数確認(0件のはず) / re-check count(must be zero.)
  169. SELECT COUNT(*)
  170. FROM oauth_access_tokens o
  171. LEFT JOIN users u ON u.id = o.resource_owner_id
  172. WHERE o.resource_owner_id IS NOT NULL
  173. AND   u.id IS NULL;
  174.  
  175. COMMIT;
Advertisement
Add Comment
Please, Sign In to add comment