From 7b831875b00f631a771afa7c0179bcb99c2d7d04 Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Thu, 21 Jul 2022 21:22:00 +0200 Subject: [PATCH] Add index to `notifications.type` and make it non-nullable --- db/migrate/20220720190421_add_type_to_notifications.rb | 4 ++++ db/schema.rb | 3 ++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/db/migrate/20220720190421_add_type_to_notifications.rb b/db/migrate/20220720190421_add_type_to_notifications.rb index 408bc266..d65513af 100644 --- a/db/migrate/20220720190421_add_type_to_notifications.rb +++ b/db/migrate/20220720190421_add_type_to_notifications.rb @@ -3,6 +3,7 @@ class AddTypeToNotifications < ActiveRecord::Migration[6.1] def up 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::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'); SQUIRREL execute "UPDATE notifications SET type = 'Notification::CommentSmiled' WHERE type IS NULL" + + change_column_null :notifications, :type, false end def down + remove_index :notifications, :type remove_column :notifications, :type end end diff --git a/db/schema.rb b/db/schema.rb index d5d91c56..140139e5 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -116,9 +116,10 @@ ActiveRecord::Schema.define(version: 2022_07_20_190421) do t.boolean "new" t.datetime "created_at" t.datetime "updated_at" - t.string "type" + t.string "type", null: false t.index ["new"], name: "index_notifications_on_new" t.index ["recipient_id"], name: "index_notifications_on_recipient_id" + t.index ["type"], name: "index_notifications_on_type" end create_table "profiles", force: :cascade do |t|