Store message receivers in a new table instead of in a serializied value

This commit is contained in:
Patrick Gansterer 2019-10-29 01:19:05 +01:00
parent 1cb7f888b7
commit f8148e7d30
7 changed files with 96 additions and 35 deletions

View file

@ -0,0 +1,51 @@
class CreateMessageRecipients < ActiveRecord::Migration
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, [: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

View file

@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20181201000000) do
ActiveRecord::Schema.define(version: 20181201000100) do
create_table "article_categories", force: :cascade do |t|
t.string "name", limit: 255, default: "", null: false
@ -248,12 +248,20 @@ ActiveRecord::Schema.define(version: 20181201000000) do
add_index "memberships", ["user_id", "group_id"], name: "index_memberships_on_user_id_and_group_id", unique: true, using: :btree
create_table "message_recipients", force: :cascade do |t|
t.integer "message_id", null: false
t.integer "user_id", null: false
t.integer "email_state", default: 0, null: false
t.datetime "read_at"
end
add_index "message_recipients", ["message_id"], name: "index_message_recipients_on_message_id", using: :btree
add_index "message_recipients", ["user_id", "read_at"], name: "index_message_recipients_on_user_id_and_read_at", using: :btree
create_table "messages", force: :cascade do |t|
t.integer "sender_id", limit: 4
t.text "recipients_ids", limit: 65535
t.string "subject", limit: 255, null: false
t.text "body", limit: 65535
t.integer "email_state", limit: 4, default: 0, null: false
t.boolean "private", default: false
t.datetime "created_at"
t.integer "reply_to", limit: 4