20170918125918_ids_to_bigints.rb 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # frozen_string_literal: true
  2. require_relative '../../lib/mastodon/migration_helpers'
  3. require_relative '../../lib/mastodon/migration_warning'
  4. class IdsToBigints < ActiveRecord::Migration[5.1]
  5. include Mastodon::MigrationHelpers
  6. include Mastodon::MigrationWarning
  7. TABLE_COLUMN_MAPPING = [
  8. [:account_domain_blocks, :account_id],
  9. [:account_domain_blocks, :id],
  10. [:accounts, :id],
  11. [:blocks, :account_id],
  12. [:blocks, :id],
  13. [:blocks, :target_account_id],
  14. [:conversation_mutes, :account_id],
  15. [:conversation_mutes, :id],
  16. [:domain_blocks, :id],
  17. [:favourites, :account_id],
  18. [:favourites, :id],
  19. [:favourites, :status_id],
  20. [:follow_requests, :account_id],
  21. [:follow_requests, :id],
  22. [:follow_requests, :target_account_id],
  23. [:follows, :account_id],
  24. [:follows, :id],
  25. [:follows, :target_account_id],
  26. [:imports, :account_id],
  27. [:imports, :id],
  28. [:media_attachments, :account_id],
  29. [:media_attachments, :id],
  30. [:mentions, :account_id],
  31. [:mentions, :id],
  32. [:mutes, :account_id],
  33. [:mutes, :id],
  34. [:mutes, :target_account_id],
  35. [:notifications, :account_id],
  36. [:notifications, :from_account_id],
  37. [:notifications, :id],
  38. [:oauth_access_grants, :application_id],
  39. [:oauth_access_grants, :id],
  40. [:oauth_access_grants, :resource_owner_id],
  41. [:oauth_access_tokens, :application_id],
  42. [:oauth_access_tokens, :id],
  43. [:oauth_access_tokens, :resource_owner_id],
  44. [:oauth_applications, :id],
  45. [:oauth_applications, :owner_id],
  46. [:reports, :account_id],
  47. [:reports, :action_taken_by_account_id],
  48. [:reports, :id],
  49. [:reports, :target_account_id],
  50. [:session_activations, :access_token_id],
  51. [:session_activations, :user_id],
  52. [:session_activations, :web_push_subscription_id],
  53. [:settings, :id],
  54. [:settings, :thing_id],
  55. [:statuses, :account_id],
  56. [:statuses, :application_id],
  57. [:statuses, :in_reply_to_account_id],
  58. [:stream_entries, :account_id],
  59. [:stream_entries, :id],
  60. [:subscriptions, :account_id],
  61. [:subscriptions, :id],
  62. [:tags, :id],
  63. [:users, :account_id],
  64. [:users, :id],
  65. [:web_settings, :id],
  66. [:web_settings, :user_id],
  67. ].freeze
  68. disable_ddl_transaction!
  69. def migrate_columns(to_type)
  70. display_warning
  71. table_sizes = {}
  72. # Sort tables by their size
  73. tables.each do |table|
  74. table_sizes[table] = estimate_rows_in_table(table)
  75. end
  76. ordered_columns = included_columns.sort_by do |col_parts|
  77. [-table_sizes[col_parts.first], col_parts.last]
  78. end
  79. ordered_columns.each do |column_parts|
  80. table, column = column_parts
  81. # Skip this if we're resuming and already did this one.
  82. next if column_for(table, column).sql_type == to_type.to_s
  83. change_column_type_concurrently table, column, to_type
  84. cleanup_concurrent_column_type_change table, column
  85. end
  86. end
  87. def display_warning
  88. migration_duration_warning(<<~EXPLANATION)
  89. This migration has some sections that can be safely interrupted
  90. and restarted later, and will tell you when those are occurring.
  91. For more information, see https://github.com/mastodon/mastodon/pull/5088
  92. EXPLANATION
  93. end
  94. def tables
  95. included_columns.map(&:first).uniq
  96. end
  97. def included_columns
  98. TABLE_COLUMN_MAPPING.dup.tap do |included_columns|
  99. included_columns << [:deprecated_preview_cards, :id] if table_exists?(:deprecated_preview_cards)
  100. end
  101. end
  102. def up
  103. migrate_columns(:bigint)
  104. end
  105. def down
  106. migrate_columns(:integer)
  107. end
  108. end