about summary refs log tree commit diff
path: root/db/migrate
diff options
context:
space:
mode:
Diffstat (limited to 'db/migrate')
-rw-r--r--db/migrate/20170918125918_ids_to_bigints.rb232
1 files changed, 112 insertions, 120 deletions
diff --git a/db/migrate/20170918125918_ids_to_bigints.rb b/db/migrate/20170918125918_ids_to_bigints.rb
index 7483dd77a..c6feed8f9 100644
--- a/db/migrate/20170918125918_ids_to_bigints.rb
+++ b/db/migrate/20170918125918_ids_to_bigints.rb
@@ -1,127 +1,119 @@
+require Rails.root.join('lib', 'mastodon', 'migration_helpers')
+
 class IdsToBigints < ActiveRecord::Migration[5.1]
+  include Mastodon::MigrationHelpers
+
+  disable_ddl_transaction!
+
+  INCLUDED_COLUMNS = [
+    [:account_domain_blocks, :account_id],
+    [:account_domain_blocks, :id],
+    [:accounts, :id],
+    [:blocks, :account_id],
+    [:blocks, :id],
+    [:blocks, :target_account_id],
+    [:conversation_mutes, :account_id],
+    [:conversation_mutes, :id],
+    [:domain_blocks, :id],
+    [:favourites, :account_id],
+    [:favourites, :id],
+    [:favourites, :status_id],
+    [:follow_requests, :account_id],
+    [:follow_requests, :id],
+    [:follow_requests, :target_account_id],
+    [:follows, :account_id],
+    [:follows, :id],
+    [:follows, :target_account_id],
+    [:imports, :account_id],
+    [:imports, :id],
+    [:media_attachments, :account_id],
+    [:media_attachments, :id],
+    [:mentions, :account_id],
+    [:mentions, :id],
+    [:mutes, :account_id],
+    [:mutes, :id],
+    [:mutes, :target_account_id],
+    [:notifications, :account_id],
+    [:notifications, :from_account_id],
+    [:notifications, :id],
+    [:oauth_access_grants, :application_id],
+    [:oauth_access_grants, :id],
+    [:oauth_access_grants, :resource_owner_id],
+    [:oauth_access_tokens, :application_id],
+    [:oauth_access_tokens, :id],
+    [:oauth_access_tokens, :resource_owner_id],
+    [:oauth_applications, :id],
+    [:oauth_applications, :owner_id],
+    [:reports, :account_id],
+    [:reports, :action_taken_by_account_id],
+    [:reports, :id],
+    [:reports, :target_account_id],
+    [:session_activations, :access_token_id],
+    [:session_activations, :user_id],
+    [:session_activations, :web_push_subscription_id],
+    [:settings, :id],
+    [:settings, :thing_id],
+    [:statuses, :account_id],
+    [:statuses, :application_id],
+    [:statuses, :in_reply_to_account_id],
+    [:stream_entries, :account_id],
+    [:stream_entries, :id],
+    [:subscriptions, :account_id],
+    [:subscriptions, :id],
+    [:tags, :id],
+    [:users, :account_id],
+    [:users, :id],
+    [:web_settings, :id],
+    [:web_settings, :user_id],
+  ]
+  INCLUDED_COLUMNS << [:deprecated_preview_cards, :id] if table_exists?(:deprecated_preview_cards)
+
+  def migrate_columns(to_type)
+    # Print out a warning that this will probably take a while.
+    say ''
+    say 'WARNING: This migration may take a *long* time for large instances'
+    say 'It will *not* lock tables for any significant time, but it may run'
+    say 'for a very long time. We will pause for 10 seconds to allow you to'
+    say 'interrupt this migration if you are not ready.'
+    say ''
+    say 'This migration has some sections that can be safely interrupted'
+    say 'and restarted later, and will tell you when those are occurring.'
+    say ''
+    say 'For more information, see https://github.com/tootsuite/mastodon/pull/5088'
+
+    10.downto(1) do |i|
+      say "Continuing in #{i} second#{i == 1 ? '' : 's'}...", true
+      sleep 1
+    end
+
+    tables = INCLUDED_COLUMNS.map(&:first).uniq
+    table_sizes = {}
+
+    # Sort tables by their size
+    tables.each do |table|
+      table_sizes[table] = estimate_rows_in_table(table)
+    end
+
+    ordered_columns = INCLUDED_COLUMNS.sort_by do |col_parts|
+      [-table_sizes[col_parts.first], col_parts.last]
+    end
+
+    ordered_columns.each do |column_parts|
+      table, column = column_parts
+
+      # Skip this if we're resuming and already did this one.
+      next if column_for(table, column).sql_type == to_type.to_s
+
+      change_column_type_concurrently table, column, to_type
+      cleanup_concurrent_column_type_change table, column
+    end
+  end
+
   def up
-    change_column :account_domain_blocks, :account_id, :bigint
-    change_column :account_domain_blocks, :id, :bigint
-    change_column :accounts, :id, :bigint
-    change_column :blocks, :account_id, :bigint
-    change_column :blocks, :id, :bigint
-    change_column :blocks, :target_account_id, :bigint
-    change_column :conversation_mutes, :account_id, :bigint
-    change_column :conversation_mutes, :id, :bigint
-    change_column :deprecated_preview_cards, :id, :bigint if table_exists?(:deprecated_preview_cards)
-    change_column :domain_blocks, :id, :bigint
-    change_column :favourites, :account_id, :bigint
-    change_column :favourites, :id, :bigint
-    change_column :favourites, :status_id, :bigint
-    change_column :follow_requests, :account_id, :bigint
-    change_column :follow_requests, :id, :bigint
-    change_column :follow_requests, :target_account_id, :bigint
-    change_column :follows, :account_id, :bigint
-    change_column :follows, :id, :bigint
-    change_column :follows, :target_account_id, :bigint
-    change_column :imports, :account_id, :bigint
-    change_column :imports, :id, :bigint
-    change_column :media_attachments, :account_id, :bigint
-    change_column :media_attachments, :id, :bigint
-    change_column :mentions, :account_id, :bigint
-    change_column :mentions, :id, :bigint
-    change_column :mutes, :account_id, :bigint
-    change_column :mutes, :id, :bigint
-    change_column :mutes, :target_account_id, :bigint
-    change_column :notifications, :account_id, :bigint
-    change_column :notifications, :from_account_id, :bigint
-    change_column :notifications, :id, :bigint
-    change_column :oauth_access_grants, :application_id, :bigint
-    change_column :oauth_access_grants, :id, :bigint
-    change_column :oauth_access_grants, :resource_owner_id, :bigint
-    change_column :oauth_access_tokens, :application_id, :bigint
-    change_column :oauth_access_tokens, :id, :bigint
-    change_column :oauth_access_tokens, :resource_owner_id, :bigint
-    change_column :oauth_applications, :id, :bigint
-    change_column :oauth_applications, :owner_id, :bigint
-    change_column :reports, :account_id, :bigint
-    change_column :reports, :action_taken_by_account_id, :bigint
-    change_column :reports, :id, :bigint
-    change_column :reports, :target_account_id, :bigint
-    change_column :session_activations, :access_token_id, :bigint
-    change_column :session_activations, :user_id, :bigint
-    change_column :session_activations, :web_push_subscription_id, :bigint
-    change_column :settings, :id, :bigint
-    change_column :settings, :thing_id, :bigint
-    change_column :statuses, :account_id, :bigint
-    change_column :statuses, :application_id, :bigint
-    change_column :statuses, :in_reply_to_account_id, :bigint
-    change_column :stream_entries, :account_id, :bigint
-    change_column :stream_entries, :id, :bigint
-    change_column :subscriptions, :account_id, :bigint
-    change_column :subscriptions, :id, :bigint
-    change_column :tags, :id, :bigint
-    change_column :users, :account_id, :bigint
-    change_column :users, :id, :bigint
-    change_column :web_settings, :id, :bigint
-    change_column :web_settings, :user_id, :bigint
+    migrate_columns(:bigint)
   end
 
   def down
-    change_column :account_domain_blocks, :account_id, :integer
-    change_column :account_domain_blocks, :id, :integer
-    change_column :accounts, :id, :integer
-    change_column :blocks, :account_id, :integer
-    change_column :blocks, :id, :integer
-    change_column :blocks, :target_account_id, :integer
-    change_column :conversation_mutes, :account_id, :integer
-    change_column :conversation_mutes, :id, :integer
-    change_column :deprecated_preview_cards, :id, :integer if table_exists?(:deprecated_preview_cards)
-    change_column :domain_blocks, :id, :integer
-    change_column :favourites, :account_id, :integer
-    change_column :favourites, :id, :integer
-    change_column :favourites, :status_id, :integer
-    change_column :follow_requests, :account_id, :integer
-    change_column :follow_requests, :id, :integer
-    change_column :follow_requests, :target_account_id, :integer
-    change_column :follows, :account_id, :integer
-    change_column :follows, :id, :integer
-    change_column :follows, :target_account_id, :integer
-    change_column :imports, :account_id, :integer
-    change_column :imports, :id, :integer
-    change_column :media_attachments, :account_id, :integer
-    change_column :media_attachments, :id, :integer
-    change_column :mentions, :account_id, :integer
-    change_column :mentions, :id, :integer
-    change_column :mutes, :account_id, :integer
-    change_column :mutes, :id, :integer
-    change_column :mutes, :target_account_id, :integer
-    change_column :notifications, :account_id, :integer
-    change_column :notifications, :from_account_id, :integer
-    change_column :notifications, :id, :integer
-    change_column :oauth_access_grants, :application_id, :integer
-    change_column :oauth_access_grants, :id, :integer
-    change_column :oauth_access_grants, :resource_owner_id, :integer
-    change_column :oauth_access_tokens, :application_id, :integer
-    change_column :oauth_access_tokens, :id, :integer
-    change_column :oauth_access_tokens, :resource_owner_id, :integer
-    change_column :oauth_applications, :id, :integer
-    change_column :oauth_applications, :owner_id, :integer
-    change_column :reports, :account_id, :integer
-    change_column :reports, :action_taken_by_account_id, :integer
-    change_column :reports, :id, :integer
-    change_column :reports, :target_account_id, :integer
-    change_column :session_activations, :access_token_id, :integer
-    change_column :session_activations, :user_id, :integer
-    change_column :session_activations, :web_push_subscription_id, :integer
-    change_column :settings, :id, :integer
-    change_column :settings, :thing_id, :integer
-    change_column :statuses, :account_id, :integer
-    change_column :statuses, :application_id, :integer
-    change_column :statuses, :in_reply_to_account_id, :integer
-    change_column :stream_entries, :account_id, :integer
-    change_column :stream_entries, :id, :integer
-    change_column :subscriptions, :account_id, :integer
-    change_column :subscriptions, :id, :integer
-    change_column :tags, :id, :integer
-    change_column :users, :account_id, :integer
-    change_column :users, :id, :integer
-    change_column :web_settings, :id, :integer
-    change_column :web_settings, :user_id, :integer
+    migrate_columns(:integer)
   end
 end