foodsoft/db/migrate/20181201000100_create_message_recipients.foodsoft_messages.rb
Philipp Rothmann fb2b4d8a8a chore: rubocop
chore: fix api test conventions

chore: rubocop -A spec/

chore: more rubocop -A

fix failing test

rubocop fixes

removes helper methods that are in my opinion dead code

more rubocop fixes

rubocop -a --auto-gen-config
2023-06-09 17:35:05 +02:00

51 lines
1.2 KiB
Ruby

class CreateMessageRecipients < ActiveRecord::Migration[4.2]
class Message < ActiveRecord::Base
has_many :message_recipients
end
class MessageRecipient < ActiveRecord::Base
end
def up
create_table :message_recipients do |t|
t.references :message, index: true, null: false
t.references :user, null: false
t.integer :email_state, default: 0, null: false
t.datetime :read_at
end
add_index :message_recipients, %i[user_id read_at]
Message.all.each do |m|
recipients = YAML.load(m.recipients_ids).map do |r|
{
message_id: m.id,
user_id: r,
email_state: m.email_state
}
end
MessageRecipient.create! recipients
end
change_table :messages do |t|
t.remove :recipients_ids
t.remove :email_state
end
end
def down
change_table :messages do |t|
t.text :recipients_ids
t.integer :email_state, default: 0, null: false
end
messages = Message.all.includes(:message_recipients).map do |m|
recipients = m.message_recipients.map(&:user_id)
m.recipients_ids = recipients.to_yaml
m.email_state = m.email_state
m.save!
end
drop_table :message_recipients
end
end