20170918125918_ids_to_bigints.rb 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. require Rails.root.join('lib', 'mastodon', 'migration_helpers')
  2. class IdsToBigints < ActiveRecord::Migration[5.1]
  3. include Mastodon::MigrationHelpers
  4. disable_ddl_transaction!
  5. INCLUDED_COLUMNS = [
  6. [:account_domain_blocks, :account_id],
  7. [:account_domain_blocks, :id],
  8. [:accounts, :id],
  9. [:blocks, :account_id],
  10. [:blocks, :id],
  11. [:blocks, :target_account_id],
  12. [:conversation_mutes, :account_id],
  13. [:conversation_mutes, :id],
  14. [:domain_blocks, :id],
  15. [:favourites, :account_id],
  16. [:favourites, :id],
  17. [:favourites, :status_id],
  18. [:follow_requests, :account_id],
  19. [:follow_requests, :id],
  20. [:follow_requests, :target_account_id],
  21. [:follows, :account_id],
  22. [:follows, :id],
  23. [:follows, :target_account_id],
  24. [:imports, :account_id],
  25. [:imports, :id],
  26. [:media_attachments, :account_id],
  27. [:media_attachments, :id],
  28. [:mentions, :account_id],
  29. [:mentions, :id],
  30. [:mutes, :account_id],
  31. [:mutes, :id],
  32. [:mutes, :target_account_id],
  33. [:notifications, :account_id],
  34. [:notifications, :from_account_id],
  35. [:notifications, :id],
  36. [:oauth_access_grants, :application_id],
  37. [:oauth_access_grants, :id],
  38. [:oauth_access_grants, :resource_owner_id],
  39. [:oauth_access_tokens, :application_id],
  40. [:oauth_access_tokens, :id],
  41. [:oauth_access_tokens, :resource_owner_id],
  42. [:oauth_applications, :id],
  43. [:oauth_applications, :owner_id],
  44. [:reports, :account_id],
  45. [:reports, :action_taken_by_account_id],
  46. [:reports, :id],
  47. [:reports, :target_account_id],
  48. [:session_activations, :access_token_id],
  49. [:session_activations, :user_id],
  50. [:session_activations, :web_push_subscription_id],
  51. [:settings, :id],
  52. [:settings, :thing_id],
  53. [:statuses, :account_id],
  54. [:statuses, :application_id],
  55. [:statuses, :in_reply_to_account_id],
  56. [:stream_entries, :account_id],
  57. [:stream_entries, :id],
  58. [:subscriptions, :account_id],
  59. [:subscriptions, :id],
  60. [:tags, :id],
  61. [:users, :account_id],
  62. [:users, :id],
  63. [:web_settings, :id],
  64. [:web_settings, :user_id],
  65. ]
  66. INCLUDED_COLUMNS << [:deprecated_preview_cards, :id] if table_exists?(:deprecated_preview_cards)
  67. def migrate_columns(to_type)
  68. # Print out a warning that this will probably take a while.
  69. say ''
  70. say 'WARNING: This migration may take a *long* time for large instances'
  71. say 'It will *not* lock tables for any significant time, but it may run'
  72. say 'for a very long time. We will pause for 10 seconds to allow you to'
  73. say 'interrupt this migration if you are not ready.'
  74. say ''
  75. say 'This migration has some sections that can be safely interrupted'
  76. say 'and restarted later, and will tell you when those are occurring.'
  77. say ''
  78. say 'For more information, see https://github.com/tootsuite/mastodon/pull/5088'
  79. 10.downto(1) do |i|
  80. say "Continuing in #{i} second#{i == 1 ? '' : 's'}...", true
  81. sleep 1
  82. end
  83. tables = INCLUDED_COLUMNS.map(&:first).uniq
  84. table_sizes = {}
  85. # Sort tables by their size
  86. tables.each do |table|
  87. table_sizes[table] = estimate_rows_in_table(table)
  88. end
  89. ordered_columns = INCLUDED_COLUMNS.sort_by do |col_parts|
  90. [-table_sizes[col_parts.first], col_parts.last]
  91. end
  92. ordered_columns.each do |column_parts|
  93. table, column = column_parts
  94. # Skip this if we're resuming and already did this one.
  95. next if column_for(table, column).sql_type == to_type.to_s
  96. change_column_type_concurrently table, column, to_type
  97. cleanup_concurrent_column_type_change table, column
  98. end
  99. end
  100. def up
  101. migrate_columns(:bigint)
  102. end
  103. def down
  104. migrate_columns(:integer)
  105. end
  106. end