20170918125918_ids_to_bigints.rb 3.5 KB

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