Add index to `notifications.type` and make it non-nullable

This commit is contained in:
Karina Kwiatek 2022-07-21 21:22:00 +02:00 committed by Karina Kwiatek
parent b534dc0bb8
commit 7b831875b0
2 changed files with 6 additions and 1 deletions

View File

@ -3,6 +3,7 @@
class AddTypeToNotifications < ActiveRecord::Migration[6.1] class AddTypeToNotifications < ActiveRecord::Migration[6.1]
def up def up
add_column :notifications, :type, :string add_column :notifications, :type, :string
add_index :notifications, :type
execute "UPDATE notifications SET type = 'Notification::Commented' WHERE target_type = 'Comment'" execute "UPDATE notifications SET type = 'Notification::Commented' WHERE target_type = 'Comment'"
execute "UPDATE notifications SET type = 'Notification::QuestionAnswered' WHERE target_type = 'Answer'" execute "UPDATE notifications SET type = 'Notification::QuestionAnswered' WHERE target_type = 'Answer'"
@ -14,9 +15,12 @@ class AddTypeToNotifications < ActiveRecord::Migration[6.1]
AND target_id IN (SELECT id FROM appendables WHERE type = 'Appendable::Reaction' AND parent_type = 'Answer'); AND target_id IN (SELECT id FROM appendables WHERE type = 'Appendable::Reaction' AND parent_type = 'Answer');
SQUIRREL SQUIRREL
execute "UPDATE notifications SET type = 'Notification::CommentSmiled' WHERE type IS NULL" execute "UPDATE notifications SET type = 'Notification::CommentSmiled' WHERE type IS NULL"
change_column_null :notifications, :type, false
end end
def down def down
remove_index :notifications, :type
remove_column :notifications, :type remove_column :notifications, :type
end end
end end

View File

@ -116,9 +116,10 @@ ActiveRecord::Schema.define(version: 2022_07_20_190421) do
t.boolean "new" t.boolean "new"
t.datetime "created_at" t.datetime "created_at"
t.datetime "updated_at" t.datetime "updated_at"
t.string "type" t.string "type", null: false
t.index ["new"], name: "index_notifications_on_new" t.index ["new"], name: "index_notifications_on_new"
t.index ["recipient_id"], name: "index_notifications_on_recipient_id" t.index ["recipient_id"], name: "index_notifications_on_recipient_id"
t.index ["type"], name: "index_notifications_on_type"
end end
create_table "profiles", force: :cascade do |t| create_table "profiles", force: :cascade do |t|