Use migration classes in migrations where current definition conflicts with older (#26390)
parent
ba9ea873c8
commit
5ebcd23979
|
@ -1,12 +1,19 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class AddShortcodeToMediaAttachments < ActiveRecord::Migration[5.0]
|
||||
class MigrationMediaAttachment < ApplicationRecord
|
||||
self.table_name = :media_attachments
|
||||
scope :local, -> { where(remote_url: '') }
|
||||
end
|
||||
|
||||
def up
|
||||
add_column :media_attachments, :shortcode, :string, null: true, default: nil
|
||||
add_index :media_attachments, :shortcode, unique: true
|
||||
|
||||
MigrationMediaAttachment.reset_column_information
|
||||
|
||||
# Migrate old links
|
||||
MediaAttachment.local.update_all('shortcode = id')
|
||||
MigrationMediaAttachment.local.update_all('shortcode = id')
|
||||
end
|
||||
|
||||
def down
|
||||
|
|
|
@ -1,11 +1,24 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class AddTypeToMediaAttachments < ActiveRecord::Migration[5.0]
|
||||
class MigrationMediaAttachment < ApplicationRecord
|
||||
self.table_name = :media_attachments
|
||||
enum type: [:image, :gifv, :video]
|
||||
IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif'].freeze
|
||||
VIDEO_MIME_TYPES = ['video/webm', 'video/mp4'].freeze
|
||||
end
|
||||
|
||||
def up
|
||||
add_column :media_attachments, :type, :integer, default: 0, null: false
|
||||
|
||||
MediaAttachment.where(file_content_type: MediaAttachment::IMAGE_MIME_TYPES).update_all(type: MediaAttachment.types[:image])
|
||||
MediaAttachment.where(file_content_type: MediaAttachment::VIDEO_MIME_TYPES).update_all(type: MediaAttachment.types[:video])
|
||||
MigrationMediaAttachment.reset_column_information
|
||||
|
||||
MigrationMediaAttachment
|
||||
.where(file_content_type: MigrationMediaAttachment::IMAGE_MIME_TYPES)
|
||||
.update_all(type: MigrationMediaAttachment.types[:image])
|
||||
MigrationMediaAttachment
|
||||
.where(file_content_type: MigrationMediaAttachment::VIDEO_MIME_TYPES)
|
||||
.update_all(type: MigrationMediaAttachment.types[:video])
|
||||
end
|
||||
|
||||
def down
|
||||
|
|
|
@ -7,9 +7,29 @@ class MigrateAccountConversations < ActiveRecord::Migration[5.2]
|
|||
|
||||
disable_ddl_transaction!
|
||||
|
||||
class Mention < ApplicationRecord
|
||||
belongs_to :account, inverse_of: :mentions
|
||||
belongs_to :status, -> { unscope(where: :deleted_at) }
|
||||
class MigrationAccount < ApplicationRecord
|
||||
self.table_name = :accounts
|
||||
has_many :mentions, inverse_of: :account, dependent: :destroy, class_name: 'MigrationMention', foreign_key: :account_id
|
||||
end
|
||||
|
||||
class MigrationConversation < ApplicationRecord
|
||||
self.table_name = :conversations
|
||||
end
|
||||
|
||||
class MigrationStatus < ApplicationRecord
|
||||
self.table_name = :statuses
|
||||
belongs_to :account, class_name: 'MigrationAccount'
|
||||
has_many :mentions, dependent: :destroy, inverse_of: :status, class_name: 'MigrationMention', foreign_key: :status_id
|
||||
scope :local, -> { where(local: true).or(where(uri: nil)) }
|
||||
enum visibility: { public: 0, unlisted: 1, private: 2, direct: 3, limited: 4 }, _suffix: :visibility
|
||||
has_many :active_mentions, -> { active }, class_name: 'MigrationMention', inverse_of: :status, foreign_key: :status_id
|
||||
end
|
||||
|
||||
class MigrationMention < ApplicationRecord
|
||||
self.table_name = :mentions
|
||||
belongs_to :account, inverse_of: :mentions, class_name: 'MigrationAccount'
|
||||
belongs_to :status, -> { unscope(where: :deleted_at) }, class_name: 'MigrationStatus'
|
||||
scope :active, -> { where(silent: false) }
|
||||
|
||||
delegate(
|
||||
:username,
|
||||
|
@ -19,22 +39,24 @@ class MigrateAccountConversations < ActiveRecord::Migration[5.2]
|
|||
)
|
||||
end
|
||||
|
||||
class Notification < ApplicationRecord
|
||||
belongs_to :account, optional: true
|
||||
class MigrationNotification < ApplicationRecord
|
||||
self.table_name = :notifications
|
||||
belongs_to :account, optional: true, class_name: 'MigrationAccount'
|
||||
belongs_to :activity, polymorphic: true, optional: true
|
||||
|
||||
belongs_to :status, foreign_key: 'activity_id', optional: true
|
||||
belongs_to :mention, foreign_key: 'activity_id', optional: true
|
||||
belongs_to :status, foreign_key: 'activity_id', optional: true, class_name: 'MigrationStatus'
|
||||
belongs_to :mention, foreign_key: 'activity_id', optional: true, class_name: 'MigrationMention'
|
||||
|
||||
def target_status
|
||||
mention&.status
|
||||
end
|
||||
end
|
||||
|
||||
class AccountConversation < ApplicationRecord
|
||||
belongs_to :account
|
||||
belongs_to :conversation
|
||||
belongs_to :last_status, -> { unscope(where: :deleted_at) }, class_name: 'Status'
|
||||
class MigrationAccountConversation < ApplicationRecord
|
||||
self.table_name = :account_conversations
|
||||
belongs_to :account, class_name: 'MigrationAccount'
|
||||
belongs_to :conversation, class_name: 'MigrationConversation'
|
||||
belongs_to :last_status, -> { unscope(where: :deleted_at) }, class_name: 'MigrationStatus'
|
||||
|
||||
before_validation :set_last_status
|
||||
|
||||
|
@ -74,7 +96,7 @@ class MigrateAccountConversations < ActiveRecord::Migration[5.2]
|
|||
last_time = Time.zone.now
|
||||
|
||||
local_direct_statuses.includes(:account, mentions: :account).find_each do |status|
|
||||
AccountConversation.add_status(status.account, status)
|
||||
MigrationAccountConversation.add_status(status.account, status)
|
||||
migrated += 1
|
||||
|
||||
if Time.zone.now - last_time > 1
|
||||
|
@ -84,7 +106,7 @@ class MigrateAccountConversations < ActiveRecord::Migration[5.2]
|
|||
end
|
||||
|
||||
notifications_about_direct_statuses.includes(:account, mention: { status: [:account, mentions: :account] }).find_each do |notification|
|
||||
AccountConversation.add_status(notification.account, notification.target_status)
|
||||
MigrationAccountConversation.add_status(notification.account, notification.target_status)
|
||||
migrated += 1
|
||||
|
||||
if Time.zone.now - last_time > 1
|
||||
|
@ -103,10 +125,10 @@ class MigrateAccountConversations < ActiveRecord::Migration[5.2]
|
|||
end
|
||||
|
||||
def local_direct_statuses
|
||||
Status.unscoped.local.where(visibility: :direct)
|
||||
MigrationStatus.unscoped.local.where(visibility: :direct)
|
||||
end
|
||||
|
||||
def notifications_about_direct_statuses
|
||||
Notification.joins('INNER JOIN mentions ON mentions.id = notifications.activity_id INNER JOIN statuses ON statuses.id = mentions.status_id').where(activity_type: 'Mention', statuses: { visibility: :direct })
|
||||
MigrationNotification.joins('INNER JOIN mentions ON mentions.id = notifications.activity_id INNER JOIN statuses ON statuses.id = mentions.status_id').where(activity_type: 'Mention', statuses: { visibility: :direct })
|
||||
end
|
||||
end
|
||||
|
|
|
@ -3,6 +3,10 @@
|
|||
class CopyAccountStats < ActiveRecord::Migration[5.2]
|
||||
disable_ddl_transaction!
|
||||
|
||||
class MigrationAccount < ApplicationRecord
|
||||
self.table_name = :accounts
|
||||
end
|
||||
|
||||
def up
|
||||
safety_assured do
|
||||
if supports_upsert?
|
||||
|
@ -27,7 +31,7 @@ class CopyAccountStats < ActiveRecord::Migration[5.2]
|
|||
def up_fast
|
||||
say 'Upsert is available, importing counters using the fast method'
|
||||
|
||||
Account.unscoped.select('id').find_in_batches(batch_size: 5_000) do |accounts|
|
||||
MigrationAccount.unscoped.select('id').find_in_batches(batch_size: 5_000) do |accounts|
|
||||
execute <<-SQL.squish
|
||||
INSERT INTO account_stats (account_id, statuses_count, following_count, followers_count, created_at, updated_at)
|
||||
SELECT id, statuses_count, following_count, followers_count, created_at, updated_at
|
||||
|
@ -44,7 +48,7 @@ class CopyAccountStats < ActiveRecord::Migration[5.2]
|
|||
|
||||
# We cannot use bulk INSERT or overarching transactions here because of possible
|
||||
# uniqueness violations that we need to skip over
|
||||
Account.unscoped.select('id, statuses_count, following_count, followers_count, created_at, updated_at').find_each do |account|
|
||||
MigrationAccount.unscoped.select('id, statuses_count, following_count, followers_count, created_at, updated_at').find_each do |account|
|
||||
params = [account.id, account[:statuses_count], account[:following_count], account[:followers_count], account.created_at, account.updated_at]
|
||||
exec_insert('INSERT INTO account_stats (account_id, statuses_count, following_count, followers_count, created_at, updated_at) VALUES ($1, $2, $3, $4, $5, $6)', nil, params)
|
||||
rescue ActiveRecord::RecordNotUnique
|
||||
|
|
|
@ -3,8 +3,15 @@
|
|||
class FillAccountSuspensionOrigin < ActiveRecord::Migration[5.2]
|
||||
disable_ddl_transaction!
|
||||
|
||||
class MigrationAccount < ApplicationRecord
|
||||
self.table_name = :accounts
|
||||
scope :suspended, -> { where.not(suspended_at: nil) }
|
||||
enum suspension_origin: { local: 0, remote: 1 }, _prefix: true
|
||||
end
|
||||
|
||||
def up
|
||||
Account.suspended.where(suspension_origin: nil).in_batches.update_all(suspension_origin: :local)
|
||||
MigrationAccount.reset_column_information
|
||||
MigrationAccount.suspended.where(suspension_origin: nil).in_batches.update_all(suspension_origin: :local)
|
||||
end
|
||||
|
||||
def down; end
|
||||
|
|
Loading…
Reference in New Issue