Remove add_column_with_default migration helper (#28654)

This commit is contained in:
Matt Jankowski 2024-01-10 05:35:06 -05:00 committed by GitHub
parent 36b46ea3b5
commit ea1c0feb86
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
29 changed files with 37 additions and 188 deletions

View File

@ -1,14 +1,10 @@
# frozen_string_literal: true # frozen_string_literal: true
require Rails.root.join('lib', 'mastodon', 'migration_helpers')
class AddHideNotificationsToMute < ActiveRecord::Migration[5.1] class AddHideNotificationsToMute < ActiveRecord::Migration[5.1]
include Mastodon::MigrationHelpers
disable_ddl_transaction! disable_ddl_transaction!
def up def up
add_column_with_default :mutes, :hide_notifications, :boolean, default: true, allow_null: false add_column :mutes, :hide_notifications, :boolean, default: true, null: false
end end
def down def down

View File

@ -1,14 +1,10 @@
# frozen_string_literal: true # frozen_string_literal: true
require Rails.root.join('lib', 'mastodon', 'migration_helpers')
class AddDisabledToCustomEmojis < ActiveRecord::Migration[5.2] class AddDisabledToCustomEmojis < ActiveRecord::Migration[5.2]
include Mastodon::MigrationHelpers
disable_ddl_transaction! disable_ddl_transaction!
def up def up
safety_assured { add_column_with_default :custom_emojis, :disabled, :bool, default: false } safety_assured { add_column :custom_emojis, :disabled, :bool, default: false, null: false }
end end
def down def down

View File

@ -1,16 +1,12 @@
# frozen_string_literal: true # frozen_string_literal: true
require Rails.root.join('lib', 'mastodon', 'migration_helpers')
class AddReblogsToFollows < ActiveRecord::Migration[5.2] class AddReblogsToFollows < ActiveRecord::Migration[5.2]
include Mastodon::MigrationHelpers
disable_ddl_transaction! disable_ddl_transaction!
def up def up
safety_assured do safety_assured do
add_column_with_default :follows, :show_reblogs, :boolean, default: true, allow_null: false add_column :follows, :show_reblogs, :boolean, default: true, null: false
add_column_with_default :follow_requests, :show_reblogs, :boolean, default: true, allow_null: false add_column :follow_requests, :show_reblogs, :boolean, default: true, null: false
end end
end end

View File

@ -1,14 +1,10 @@
# frozen_string_literal: true # frozen_string_literal: true
require Rails.root.join('lib', 'mastodon', 'migration_helpers')
class AddMemorialToAccounts < ActiveRecord::Migration[5.2] class AddMemorialToAccounts < ActiveRecord::Migration[5.2]
include Mastodon::MigrationHelpers
disable_ddl_transaction! disable_ddl_transaction!
def up def up
safety_assured { add_column_with_default :accounts, :memorial, :bool, default: false } safety_assured { add_column :accounts, :memorial, :bool, default: false, null: false }
end end
def down def down

View File

@ -1,14 +1,10 @@
# frozen_string_literal: true # frozen_string_literal: true
require Rails.root.join('lib', 'mastodon', 'migration_helpers')
class AddDisabledToUsers < ActiveRecord::Migration[5.2] class AddDisabledToUsers < ActiveRecord::Migration[5.2]
include Mastodon::MigrationHelpers
disable_ddl_transaction! disable_ddl_transaction!
def up def up
safety_assured { add_column_with_default :users, :disabled, :bool, default: false } safety_assured { add_column :users, :disabled, :bool, default: false, null: false }
end end
def down def down

View File

@ -1,14 +1,10 @@
# frozen_string_literal: true # frozen_string_literal: true
require Rails.root.join('lib', 'mastodon', 'migration_helpers')
class AddModeratorToAccounts < ActiveRecord::Migration[5.2] class AddModeratorToAccounts < ActiveRecord::Migration[5.2]
include Mastodon::MigrationHelpers
disable_ddl_transaction! disable_ddl_transaction!
def up def up
safety_assured { add_column_with_default :users, :moderator, :bool, default: false } safety_assured { add_column :users, :moderator, :bool, default: false, null: false }
end end
def down def down

View File

@ -1,15 +1,11 @@
# frozen_string_literal: true # frozen_string_literal: true
require Rails.root.join('lib', 'mastodon', 'migration_helpers')
class AddEmbedURLToPreviewCards < ActiveRecord::Migration[5.2] class AddEmbedURLToPreviewCards < ActiveRecord::Migration[5.2]
include Mastodon::MigrationHelpers
disable_ddl_transaction! disable_ddl_transaction!
def up def up
safety_assured do safety_assured do
add_column_with_default :preview_cards, :embed_url, :string, default: '', allow_null: false add_column :preview_cards, :embed_url, :string, default: '', null: false
end end
end end

View File

@ -1,15 +1,11 @@
# frozen_string_literal: true # frozen_string_literal: true
require Rails.root.join('lib', 'mastodon', 'migration_helpers')
class AddAutofollowToInvites < ActiveRecord::Migration[5.2] class AddAutofollowToInvites < ActiveRecord::Migration[5.2]
include Mastodon::MigrationHelpers
disable_ddl_transaction! disable_ddl_transaction!
def up def up
safety_assured do safety_assured do
add_column_with_default :invites, :autofollow, :bool, default: false, allow_null: false add_column :invites, :autofollow, :bool, default: false, null: false
end end
end end

View File

@ -1,15 +1,11 @@
# frozen_string_literal: true # frozen_string_literal: true
require Rails.root.join('lib', 'mastodon', 'migration_helpers')
class AddWholeWordToCustomFilter < ActiveRecord::Migration[5.2] class AddWholeWordToCustomFilter < ActiveRecord::Migration[5.2]
include Mastodon::MigrationHelpers
disable_ddl_transaction! disable_ddl_transaction!
def change def change
safety_assured do safety_assured do
add_column_with_default :custom_filters, :whole_word, :boolean, default: true, allow_null: false add_column :custom_filters, :whole_word, :boolean, default: true, null: false
end end
end end

View File

@ -1,19 +1,15 @@
# frozen_string_literal: true # frozen_string_literal: true
require Rails.root.join('lib', 'mastodon', 'migration_helpers')
class AddConfidentialToDoorkeeperApplication < ActiveRecord::Migration[5.2] class AddConfidentialToDoorkeeperApplication < ActiveRecord::Migration[5.2]
include Mastodon::MigrationHelpers
disable_ddl_transaction! disable_ddl_transaction!
def up def up
safety_assured do safety_assured do
add_column_with_default( add_column(
:oauth_applications, :oauth_applications,
:confidential, :confidential,
:boolean, :boolean,
allow_null: false, null: false,
default: true # maintaining backwards compatibility: require secrets default: true # maintaining backwards compatibility: require secrets
) )
end end

View File

@ -1,19 +1,15 @@
# frozen_string_literal: true # frozen_string_literal: true
require Rails.root.join('lib', 'mastodon', 'migration_helpers')
class AddSilentToMentions < ActiveRecord::Migration[5.2] class AddSilentToMentions < ActiveRecord::Migration[5.2]
include Mastodon::MigrationHelpers
disable_ddl_transaction! disable_ddl_transaction!
def up def up
safety_assured do safety_assured do
add_column_with_default( add_column(
:mentions, :mentions,
:silent, :silent,
:boolean, :boolean,
allow_null: false, null: false,
default: false default: false
) )
end end

View File

@ -1,15 +1,11 @@
# frozen_string_literal: true # frozen_string_literal: true
require Rails.root.join('lib', 'mastodon', 'migration_helpers')
class AddRejectReportsToDomainBlocks < ActiveRecord::Migration[5.2] class AddRejectReportsToDomainBlocks < ActiveRecord::Migration[5.2]
include Mastodon::MigrationHelpers
disable_ddl_transaction! disable_ddl_transaction!
def up def up
safety_assured do safety_assured do
add_column_with_default :domain_blocks, :reject_reports, :boolean, default: false, allow_null: false add_column :domain_blocks, :reject_reports, :boolean, default: false, null: false
end end
end end

View File

@ -1,19 +1,15 @@
# frozen_string_literal: true # frozen_string_literal: true
require Rails.root.join('lib', 'mastodon', 'migration_helpers')
class AddUnreadToAccountConversations < ActiveRecord::Migration[5.2] class AddUnreadToAccountConversations < ActiveRecord::Migration[5.2]
include Mastodon::MigrationHelpers
disable_ddl_transaction! disable_ddl_transaction!
def up def up
safety_assured do safety_assured do
add_column_with_default( add_column(
:account_conversations, :account_conversations,
:unread, :unread,
:boolean, :boolean,
allow_null: false, null: false,
default: false default: false
) )
end end

View File

@ -1,19 +1,15 @@
# frozen_string_literal: true # frozen_string_literal: true
require Rails.root.join('lib', 'mastodon', 'migration_helpers')
class AddShowRepliesToLists < ActiveRecord::Migration[5.2] class AddShowRepliesToLists < ActiveRecord::Migration[5.2]
include Mastodon::MigrationHelpers
disable_ddl_transaction! disable_ddl_transaction!
def up def up
safety_assured do safety_assured do
add_column_with_default( add_column(
:lists, :lists,
:replies_policy, :replies_policy,
:integer, :integer,
allow_null: false, null: false,
default: 0 default: 0
) )
end end

View File

@ -1,15 +1,11 @@
# frozen_string_literal: true # frozen_string_literal: true
require Rails.root.join('lib', 'mastodon', 'migration_helpers')
class AddOverwriteToImports < ActiveRecord::Migration[5.2] class AddOverwriteToImports < ActiveRecord::Migration[5.2]
include Mastodon::MigrationHelpers
disable_ddl_transaction! disable_ddl_transaction!
def up def up
safety_assured do safety_assured do
add_column_with_default :imports, :overwrite, :boolean, default: false, allow_null: false add_column :imports, :overwrite, :boolean, default: false, null: false
end end
end end

View File

@ -1,19 +1,15 @@
# frozen_string_literal: true # frozen_string_literal: true
require Rails.root.join('lib', 'mastodon', 'migration_helpers')
class AddLockVersionToPolls < ActiveRecord::Migration[5.2] class AddLockVersionToPolls < ActiveRecord::Migration[5.2]
include Mastodon::MigrationHelpers
disable_ddl_transaction! disable_ddl_transaction!
def up def up
safety_assured do safety_assured do
add_column_with_default( add_column(
:polls, :polls,
:lock_version, :lock_version,
:integer, :integer,
allow_null: false, null: false,
default: 0 default: 0
) )
end end

View File

@ -1,19 +1,15 @@
# frozen_string_literal: true # frozen_string_literal: true
require Rails.root.join('lib', 'mastodon', 'migration_helpers')
class AddApprovedToUsers < ActiveRecord::Migration[5.2] class AddApprovedToUsers < ActiveRecord::Migration[5.2]
include Mastodon::MigrationHelpers
disable_ddl_transaction! disable_ddl_transaction!
def up def up
safety_assured do safety_assured do
add_column_with_default( add_column(
:users, :users,
:approved, :approved,
:bool, :bool,
allow_null: false, null: false,
default: true default: true
) )
end end

View File

@ -1,14 +1,10 @@
# frozen_string_literal: true # frozen_string_literal: true
require Rails.root.join('lib', 'mastodon', 'migration_helpers')
class AddLockVersionToAccountStats < ActiveRecord::Migration[5.2] class AddLockVersionToAccountStats < ActiveRecord::Migration[5.2]
include Mastodon::MigrationHelpers
disable_ddl_transaction! disable_ddl_transaction!
def up def up
safety_assured { add_column_with_default :account_stats, :lock_version, :integer, allow_null: false, default: 0 } safety_assured { add_column :account_stats, :lock_version, :integer, null: false, default: 0 }
end end
def down def down

View File

@ -1,14 +1,10 @@
# frozen_string_literal: true # frozen_string_literal: true
require Rails.root.join('lib', 'mastodon', 'migration_helpers')
class AddTitleToAccountWarningPresets < ActiveRecord::Migration[5.2] class AddTitleToAccountWarningPresets < ActiveRecord::Migration[5.2]
include Mastodon::MigrationHelpers
disable_ddl_transaction! disable_ddl_transaction!
def up def up
safety_assured { add_column_with_default :account_warning_presets, :title, :string, default: '', allow_null: false } safety_assured { add_column :account_warning_presets, :title, :string, default: '', null: false }
end end
def down def down

View File

@ -1,16 +1,12 @@
# frozen_string_literal: true # frozen_string_literal: true
require Rails.root.join('lib', 'mastodon', 'migration_helpers')
class AddNotifyToFollows < ActiveRecord::Migration[5.2] class AddNotifyToFollows < ActiveRecord::Migration[5.2]
include Mastodon::MigrationHelpers
disable_ddl_transaction! disable_ddl_transaction!
def up def up
safety_assured do safety_assured do
add_column_with_default :follows, :notify, :boolean, default: false, allow_null: false add_column :follows, :notify, :boolean, default: false, null: false
add_column_with_default :follow_requests, :notify, :boolean, default: false, allow_null: false add_column :follow_requests, :notify, :boolean, default: false, null: false
end end
end end

View File

@ -1,14 +1,10 @@
# frozen_string_literal: true # frozen_string_literal: true
require Rails.root.join('lib', 'mastodon', 'migration_helpers')
class AddObfuscateToDomainBlocks < ActiveRecord::Migration[5.2] class AddObfuscateToDomainBlocks < ActiveRecord::Migration[5.2]
include Mastodon::MigrationHelpers
disable_ddl_transaction! disable_ddl_transaction!
def up def up
safety_assured { add_column_with_default :domain_blocks, :obfuscate, :boolean, default: false, allow_null: false } safety_assured { add_column :domain_blocks, :obfuscate, :boolean, default: false, null: false }
end end
def down def down

View File

@ -1,15 +1,11 @@
# frozen_string_literal: true # frozen_string_literal: true
require Rails.root.join('lib', 'mastodon', 'migration_helpers')
class AddCategoryToReports < ActiveRecord::Migration[6.1] class AddCategoryToReports < ActiveRecord::Migration[6.1]
include Mastodon::MigrationHelpers
disable_ddl_transaction! disable_ddl_transaction!
def up def up
safety_assured do safety_assured do
add_column_with_default :reports, :category, :int, default: 0, allow_null: false add_column :reports, :category, :int, default: 0, null: false
change_table(:reports, bulk: true) do |t| change_table(:reports, bulk: true) do |t|
t.column :action_taken_at, :datetime t.column :action_taken_at, :datetime
t.column :rule_ids, :bigint, array: true t.column :rule_ids, :bigint, array: true

View File

@ -1,15 +1,11 @@
# frozen_string_literal: true # frozen_string_literal: true
require Rails.root.join('lib', 'mastodon', 'migration_helpers')
class AddActionToCustomFilters < ActiveRecord::Migration[6.1] class AddActionToCustomFilters < ActiveRecord::Migration[6.1]
include Mastodon::MigrationHelpers
disable_ddl_transaction! disable_ddl_transaction!
def up def up
safety_assured do safety_assured do
add_column_with_default :custom_filters, :action, :integer, allow_null: false, default: 0 add_column :custom_filters, :action, :integer, null: false, default: 0
execute 'UPDATE custom_filters SET action = 1 WHERE irreversible IS TRUE' execute 'UPDATE custom_filters SET action = 1 WHERE irreversible IS TRUE'
end end
end end

View File

@ -1,14 +1,10 @@
# frozen_string_literal: true # frozen_string_literal: true
require Rails.root.join('lib', 'mastodon', 'migration_helpers')
class AddExclusiveToLists < ActiveRecord::Migration[6.1] class AddExclusiveToLists < ActiveRecord::Migration[6.1]
include Mastodon::MigrationHelpers
disable_ddl_transaction! disable_ddl_transaction!
def up def up
safety_assured { add_column_with_default :lists, :exclusive, :boolean, default: false, allow_null: false } safety_assured { add_column :lists, :exclusive, :boolean, default: false, null: false }
end end
def down def down

View File

@ -1,14 +1,10 @@
# frozen_string_literal: true # frozen_string_literal: true
require Rails.root.join('lib', 'mastodon', 'migration_helpers')
class AddImageDescriptionToPreviewCards < ActiveRecord::Migration[7.0] class AddImageDescriptionToPreviewCards < ActiveRecord::Migration[7.0]
include Mastodon::MigrationHelpers
disable_ddl_transaction! disable_ddl_transaction!
def up def up
safety_assured { add_column_with_default :preview_cards, :image_description, :string, default: '', allow_null: false } safety_assured { add_column :preview_cards, :image_description, :string, default: '', null: false }
end end
def down def down

View File

@ -1,14 +1,10 @@
# frozen_string_literal: true # frozen_string_literal: true
require Rails.root.join('lib', 'mastodon', 'migration_helpers')
class AddIndexableToAccounts < ActiveRecord::Migration[7.0] class AddIndexableToAccounts < ActiveRecord::Migration[7.0]
include Mastodon::MigrationHelpers
disable_ddl_transaction! disable_ddl_transaction!
def up def up
safety_assured { add_column_with_default :accounts, :indexable, :boolean, default: false, allow_null: false } safety_assured { add_column :accounts, :indexable, :boolean, default: false, null: false }
end end
def down def down

View File

@ -1,10 +1,6 @@
# frozen_string_literal: true # frozen_string_literal: true
require Rails.root.join('lib', 'mastodon', 'migration_helpers')
class RemoveWholeWordFromCustomFilters < ActiveRecord::Migration[6.1] class RemoveWholeWordFromCustomFilters < ActiveRecord::Migration[6.1]
include Mastodon::MigrationHelpers
disable_ddl_transaction! disable_ddl_transaction!
def up def up
@ -15,7 +11,7 @@ class RemoveWholeWordFromCustomFilters < ActiveRecord::Migration[6.1]
def down def down
safety_assured do safety_assured do
add_column_with_default :custom_filters, :whole_word, :boolean, default: true, allow_null: false add_column :custom_filters, :whole_word, :boolean, default: true, null: false
end end
end end
end end

View File

@ -1,10 +1,6 @@
# frozen_string_literal: true # frozen_string_literal: true
require Rails.root.join('lib', 'mastodon', 'migration_helpers')
class RemoveIrreversibleFromCustomFilters < ActiveRecord::Migration[6.1] class RemoveIrreversibleFromCustomFilters < ActiveRecord::Migration[6.1]
include Mastodon::MigrationHelpers
disable_ddl_transaction! disable_ddl_transaction!
def up def up
@ -15,7 +11,7 @@ class RemoveIrreversibleFromCustomFilters < ActiveRecord::Migration[6.1]
def down def down
safety_assured do safety_assured do
add_column_with_default :custom_filters, :irreversible, :boolean, allow_null: false, default: false add_column :custom_filters, :irreversible, :boolean, null: false, default: false
end end
end end
end end

View File

@ -104,18 +104,7 @@ module Mastodon
'in the body of your migration class' 'in the body of your migration class'
end end
# If default value is presented, use `add_column_with_default` method instead. add_column(table_name, column_name, :datetime_with_timezone, **options)
if options[:default]
add_column_with_default(
table_name,
column_name,
:datetime_with_timezone,
default: options[:default],
allow_null: options[:null]
)
else
add_column(table_name, column_name, :datetime_with_timezone, **options)
end
end end
end end
@ -377,34 +366,6 @@ module Mastodon
end end
end end
# Adds a column with a default value without locking an entire table.
#
# This method runs the following steps:
#
# 1. Add the column with a default value of NULL.
# 2. Change the default value of the column to the specified value.
# 3. Update all existing rows in batches.
# 4. Set a `NOT NULL` constraint on the column if desired (the default).
#
# These steps ensure a column can be added to a large and commonly used
# table without locking the entire table for the duration of the table
# modification.
#
# table - The name of the table to update.
# column - The name of the column to add.
# type - The column type (e.g. `:integer`).
# default - The default value for the column.
# limit - Sets a column limit. For example, for :integer, the default is
# 4-bytes. Set `limit: 8` to allow 8-byte integers.
# allow_null - When set to `true` the column will allow NULL values, the
# default is to not allow NULL values.
#
# This method can also take a block which is passed directly to the
# `update_column_in_batches` method.
def add_column_with_default(table, column, type, default:, limit: nil, allow_null: false, &block)
add_column(table, column, type, default: default, limit: limit, null: allow_null)
end
# Renames a column without requiring downtime. # Renames a column without requiring downtime.
# #
# Concurrent renames work by using database triggers to ensure both the # Concurrent renames work by using database triggers to ensure both the