mirror of
https://github.com/mastodon/mastodon.git
synced 2024-11-24 13:35:07 +01:00
Use migration classes in migrations where current definition conflicts with older (#26390)
This commit is contained in:
parent
b12d75ef4f
commit
271d384fd0
@ -1,12 +1,19 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class AddShortcodeToMediaAttachments < ActiveRecord::Migration[5.0]
|
class AddShortcodeToMediaAttachments < ActiveRecord::Migration[5.0]
|
||||||
|
class MigrationMediaAttachment < ApplicationRecord
|
||||||
|
self.table_name = :media_attachments
|
||||||
|
scope :local, -> { where(remote_url: '') }
|
||||||
|
end
|
||||||
|
|
||||||
def up
|
def up
|
||||||
add_column :media_attachments, :shortcode, :string, null: true, default: nil
|
add_column :media_attachments, :shortcode, :string, null: true, default: nil
|
||||||
add_index :media_attachments, :shortcode, unique: true
|
add_index :media_attachments, :shortcode, unique: true
|
||||||
|
|
||||||
|
MigrationMediaAttachment.reset_column_information
|
||||||
|
|
||||||
# Migrate old links
|
# Migrate old links
|
||||||
MediaAttachment.local.update_all('shortcode = id')
|
MigrationMediaAttachment.local.update_all('shortcode = id')
|
||||||
end
|
end
|
||||||
|
|
||||||
def down
|
def down
|
||||||
|
@ -1,11 +1,24 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class AddTypeToMediaAttachments < ActiveRecord::Migration[5.0]
|
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
|
def up
|
||||||
add_column :media_attachments, :type, :integer, default: 0, null: false
|
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])
|
MigrationMediaAttachment.reset_column_information
|
||||||
MediaAttachment.where(file_content_type: MediaAttachment::VIDEO_MIME_TYPES).update_all(type: MediaAttachment.types[:video])
|
|
||||||
|
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
|
end
|
||||||
|
|
||||||
def down
|
def down
|
||||||
|
@ -7,9 +7,29 @@ class MigrateAccountConversations < ActiveRecord::Migration[5.2]
|
|||||||
|
|
||||||
disable_ddl_transaction!
|
disable_ddl_transaction!
|
||||||
|
|
||||||
class Mention < ApplicationRecord
|
class MigrationAccount < ApplicationRecord
|
||||||
belongs_to :account, inverse_of: :mentions
|
self.table_name = :accounts
|
||||||
belongs_to :status, -> { unscope(where: :deleted_at) }
|
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(
|
delegate(
|
||||||
:username,
|
:username,
|
||||||
@ -19,22 +39,24 @@ class MigrateAccountConversations < ActiveRecord::Migration[5.2]
|
|||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
class Notification < ApplicationRecord
|
class MigrationNotification < ApplicationRecord
|
||||||
belongs_to :account, optional: true
|
self.table_name = :notifications
|
||||||
|
belongs_to :account, optional: true, class_name: 'MigrationAccount'
|
||||||
belongs_to :activity, polymorphic: true, optional: true
|
belongs_to :activity, polymorphic: true, optional: true
|
||||||
|
|
||||||
belongs_to :status, 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
|
belongs_to :mention, foreign_key: 'activity_id', optional: true, class_name: 'MigrationMention'
|
||||||
|
|
||||||
def target_status
|
def target_status
|
||||||
mention&.status
|
mention&.status
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class AccountConversation < ApplicationRecord
|
class MigrationAccountConversation < ApplicationRecord
|
||||||
belongs_to :account
|
self.table_name = :account_conversations
|
||||||
belongs_to :conversation
|
belongs_to :account, class_name: 'MigrationAccount'
|
||||||
belongs_to :last_status, -> { unscope(where: :deleted_at) }, class_name: 'Status'
|
belongs_to :conversation, class_name: 'MigrationConversation'
|
||||||
|
belongs_to :last_status, -> { unscope(where: :deleted_at) }, class_name: 'MigrationStatus'
|
||||||
|
|
||||||
before_validation :set_last_status
|
before_validation :set_last_status
|
||||||
|
|
||||||
@ -74,7 +96,7 @@ class MigrateAccountConversations < ActiveRecord::Migration[5.2]
|
|||||||
last_time = Time.zone.now
|
last_time = Time.zone.now
|
||||||
|
|
||||||
local_direct_statuses.includes(:account, mentions: :account).find_each do |status|
|
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
|
migrated += 1
|
||||||
|
|
||||||
if Time.zone.now - last_time > 1
|
if Time.zone.now - last_time > 1
|
||||||
@ -84,7 +106,7 @@ class MigrateAccountConversations < ActiveRecord::Migration[5.2]
|
|||||||
end
|
end
|
||||||
|
|
||||||
notifications_about_direct_statuses.includes(:account, mention: { status: [:account, mentions: :account] }).find_each do |notification|
|
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
|
migrated += 1
|
||||||
|
|
||||||
if Time.zone.now - last_time > 1
|
if Time.zone.now - last_time > 1
|
||||||
@ -103,10 +125,10 @@ class MigrateAccountConversations < ActiveRecord::Migration[5.2]
|
|||||||
end
|
end
|
||||||
|
|
||||||
def local_direct_statuses
|
def local_direct_statuses
|
||||||
Status.unscoped.local.where(visibility: :direct)
|
MigrationStatus.unscoped.local.where(visibility: :direct)
|
||||||
end
|
end
|
||||||
|
|
||||||
def notifications_about_direct_statuses
|
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
|
||||||
end
|
end
|
||||||
|
@ -3,6 +3,10 @@
|
|||||||
class CopyAccountStats < ActiveRecord::Migration[5.2]
|
class CopyAccountStats < ActiveRecord::Migration[5.2]
|
||||||
disable_ddl_transaction!
|
disable_ddl_transaction!
|
||||||
|
|
||||||
|
class MigrationAccount < ApplicationRecord
|
||||||
|
self.table_name = :accounts
|
||||||
|
end
|
||||||
|
|
||||||
def up
|
def up
|
||||||
safety_assured do
|
safety_assured do
|
||||||
if supports_upsert?
|
if supports_upsert?
|
||||||
@ -27,7 +31,7 @@ class CopyAccountStats < ActiveRecord::Migration[5.2]
|
|||||||
def up_fast
|
def up_fast
|
||||||
say 'Upsert is available, importing counters using the fast method'
|
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
|
execute <<-SQL.squish
|
||||||
INSERT INTO account_stats (account_id, statuses_count, following_count, followers_count, created_at, updated_at)
|
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
|
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
|
# We cannot use bulk INSERT or overarching transactions here because of possible
|
||||||
# uniqueness violations that we need to skip over
|
# 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]
|
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)
|
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
|
rescue ActiveRecord::RecordNotUnique
|
||||||
|
@ -3,8 +3,15 @@
|
|||||||
class FillAccountSuspensionOrigin < ActiveRecord::Migration[5.2]
|
class FillAccountSuspensionOrigin < ActiveRecord::Migration[5.2]
|
||||||
disable_ddl_transaction!
|
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
|
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
|
end
|
||||||
|
|
||||||
def down; end
|
def down; end
|
||||||
|
Loading…
Reference in New Issue
Block a user