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
This commit is contained in:
Julianna Tetreault 2020-07-13 09:36:19 -06:00 committed by GitHub
parent 5affc7c08c
commit 5f143c4308
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 56 additions and 1 deletions

View file

@ -0,0 +1,2 @@
class Announcement < ApplicationRecord
end

View file

@ -0,0 +1,2 @@
class WelcomeNotification < ApplicationRecord
end

View file

@ -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

View file

@ -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

View file

@ -0,0 +1,7 @@
class CreateWelcomeNotifications < ActiveRecord::Migration[6.0]
def change
create_table :welcome_notifications do |t|
t.timestamps
end
end
end

View file

@ -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

View file

@ -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"

View file

@ -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