Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix old migration failing with new status default scope #12493

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions db/migrate/20181024224956_migrate_account_conversations.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,66 @@
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) }

delegate(
:username,
:acct,
to: :account,
prefix: true
)
end

class Notification < ApplicationRecord
belongs_to :account, optional: true
belongs_to :activity, polymorphic: true, optional: true

belongs_to :status, foreign_type: 'Status', foreign_key: 'activity_id', optional: true
belongs_to :mention, foreign_type: 'Mention', foreign_key: 'activity_id', optional: true

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'

before_validation :set_last_status

class << self
def add_status(recipient, status)
conversation = find_or_initialize_by(account: recipient, conversation_id: status.conversation_id, participant_account_ids: participants_from_status(recipient, status))

return conversation if conversation.status_ids.include?(status.id)

conversation.status_ids << status.id
conversation.unread = status.account_id != recipient.id
conversation.save
conversation
rescue ActiveRecord::StaleObjectError
retry
end

private

def participants_from_status(recipient, status)
((status.active_mentions.pluck(:account_id) + [status.account_id]).uniq - [recipient.id]).sort
end
end

private

def set_last_status
self.status_ids = status_ids.sort
self.last_status_id = status_ids.last
end
end

def up
say ''
say 'WARNING: This migration may take a *long* time for large instances'
Expand Down