From 5f143c4308a90c4ccb61e3c4df1d15b15011543a Mon Sep 17 00:00:00 2001 From: Julianna Tetreault <32834804+juliannatetreault@users.noreply.github.com> Date: Mon, 13 Jul 2020 09:36:19 -0600 Subject: [PATCH] Add Polymorphism to Broadcasts (Part 1) (#9231) [deploy] * Add polymorphism to Broadcasts table - Adds a broadcastable_id column to Broadcasts - Adds a broadcastable_type column to Broadcasts * Create Announcements and Welcome Notifications tables - Adds an Announcements table - Adds a Welcome Notifications table - Adds timestamps to both tables - Adds banner_style column to Announcements table * Adds Announcement and Welcome Notification models - Adds an Announcement model in prep for polymorphism - Adds a WelcomeNotification model in prep for polymorphism * Add data_update script to backfill type_of column for Broadcasts * Add unique index on broadcastable_type and broadcastable_id on the Broadcasts table * Add update! to the data_update script to backfill broadcastable_type --- app/models/announcement.rb | 2 ++ app/models/welcome_notification.rb | 2 ++ ...00707170245_add_polymorphism_to_broadcasts.rb | 6 ++++++ .../20200707173316_create_announcements.rb | 8 ++++++++ ...0200707173524_create_welcome_notifications.rb | 7 +++++++ .../20200710174257_add_index_to_broadcasts.rb | 7 +++++++ db/schema.rb | 16 +++++++++++++++- ...backfill_broadcastable_type_for_broadcasts.rb | 9 +++++++++ 8 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 app/models/announcement.rb create mode 100644 app/models/welcome_notification.rb create mode 100644 db/migrate/20200707170245_add_polymorphism_to_broadcasts.rb create mode 100644 db/migrate/20200707173316_create_announcements.rb create mode 100644 db/migrate/20200707173524_create_welcome_notifications.rb create mode 100644 db/migrate/20200710174257_add_index_to_broadcasts.rb create mode 100644 lib/data_update_scripts/20200708163323_backfill_broadcastable_type_for_broadcasts.rb diff --git a/app/models/announcement.rb b/app/models/announcement.rb new file mode 100644 index 000000000..aa98a0e50 --- /dev/null +++ b/app/models/announcement.rb @@ -0,0 +1,2 @@ +class Announcement < ApplicationRecord +end diff --git a/app/models/welcome_notification.rb b/app/models/welcome_notification.rb new file mode 100644 index 000000000..6f9a2e7a8 --- /dev/null +++ b/app/models/welcome_notification.rb @@ -0,0 +1,2 @@ +class WelcomeNotification < ApplicationRecord +end diff --git a/db/migrate/20200707170245_add_polymorphism_to_broadcasts.rb b/db/migrate/20200707170245_add_polymorphism_to_broadcasts.rb new file mode 100644 index 000000000..598d405eb --- /dev/null +++ b/db/migrate/20200707170245_add_polymorphism_to_broadcasts.rb @@ -0,0 +1,6 @@ +class AddPolymorphismToBroadcasts < ActiveRecord::Migration[6.0] + def change + add_column :broadcasts, :broadcastable_id, :integer + add_column :broadcasts, :broadcastable_type, :string + end +end diff --git a/db/migrate/20200707173316_create_announcements.rb b/db/migrate/20200707173316_create_announcements.rb new file mode 100644 index 000000000..79f58b964 --- /dev/null +++ b/db/migrate/20200707173316_create_announcements.rb @@ -0,0 +1,8 @@ +class CreateAnnouncements < ActiveRecord::Migration[6.0] + def change + create_table :announcements do |t| + t.string :banner_style + t.timestamps + end + end +end diff --git a/db/migrate/20200707173524_create_welcome_notifications.rb b/db/migrate/20200707173524_create_welcome_notifications.rb new file mode 100644 index 000000000..6c54b0aa9 --- /dev/null +++ b/db/migrate/20200707173524_create_welcome_notifications.rb @@ -0,0 +1,7 @@ +class CreateWelcomeNotifications < ActiveRecord::Migration[6.0] + def change + create_table :welcome_notifications do |t| + t.timestamps + end + end +end diff --git a/db/migrate/20200710174257_add_index_to_broadcasts.rb b/db/migrate/20200710174257_add_index_to_broadcasts.rb new file mode 100644 index 000000000..dd5b87206 --- /dev/null +++ b/db/migrate/20200710174257_add_index_to_broadcasts.rb @@ -0,0 +1,7 @@ +class AddIndexToBroadcasts < ActiveRecord::Migration[6.0] + disable_ddl_transaction! + + def change + add_index :broadcasts, %i[broadcastable_type broadcastable_id], unique: true, algorithm: :concurrently + end +end diff --git a/db/schema.rb b/db/schema.rb index 3aec8f8a7..4b95f6408 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2020_07_06_184804) do +ActiveRecord::Schema.define(version: 2020_07_10_174257) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -58,6 +58,12 @@ ActiveRecord::Schema.define(version: 2020_07_06_184804) do t.index ["visit_token"], name: "index_ahoy_visits_on_visit_token", unique: true end + create_table "announcements", force: :cascade do |t| + t.string "banner_style" + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + end + create_table "api_secrets", force: :cascade do |t| t.datetime "created_at", null: false t.string "description", null: false @@ -293,11 +299,14 @@ ActiveRecord::Schema.define(version: 2020_07_06_184804) do t.datetime "active_status_updated_at" t.string "banner_style" t.text "body_markdown" + t.integer "broadcastable_id" + t.string "broadcastable_type" t.datetime "created_at" t.text "processed_html" t.string "title" t.string "type_of" t.datetime "updated_at" + t.index ["broadcastable_type", "broadcastable_id"], name: "index_broadcasts_on_broadcastable_type_and_broadcastable_id", unique: true t.index ["title", "type_of"], name: "index_broadcasts_on_title_and_type_of", unique: true end @@ -1326,6 +1335,11 @@ ActiveRecord::Schema.define(version: 2020_07_06_184804) do t.index ["user_id"], name: "index_webhook_endpoints_on_user_id" end + create_table "welcome_notifications", force: :cascade do |t| + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + end + add_foreign_key "api_secrets", "users", on_delete: :cascade add_foreign_key "audit_logs", "users" add_foreign_key "badge_achievements", "badges" diff --git a/lib/data_update_scripts/20200708163323_backfill_broadcastable_type_for_broadcasts.rb b/lib/data_update_scripts/20200708163323_backfill_broadcastable_type_for_broadcasts.rb new file mode 100644 index 000000000..f85ad1963 --- /dev/null +++ b/lib/data_update_scripts/20200708163323_backfill_broadcastable_type_for_broadcasts.rb @@ -0,0 +1,9 @@ +module DataUpdateScripts + class BackfillBroadcastableTypeForBroadcasts + def run + Broadcast.where(broadcastable_type: nil).each do |cast| + cast.update!(broadcastable_type: broadcast.type_of) + end + end + end +end