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:
parent
5affc7c08c
commit
5f143c4308
8 changed files with 56 additions and 1 deletions
2
app/models/announcement.rb
Normal file
2
app/models/announcement.rb
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
class Announcement < ApplicationRecord
|
||||
end
|
||||
2
app/models/welcome_notification.rb
Normal file
2
app/models/welcome_notification.rb
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
class WelcomeNotification < ApplicationRecord
|
||||
end
|
||||
|
|
@ -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
|
||||
8
db/migrate/20200707173316_create_announcements.rb
Normal file
8
db/migrate/20200707173316_create_announcements.rb
Normal 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
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
class CreateWelcomeNotifications < ActiveRecord::Migration[6.0]
|
||||
def change
|
||||
create_table :welcome_notifications do |t|
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
end
|
||||
7
db/migrate/20200710174257_add_index_to_broadcasts.rb
Normal file
7
db/migrate/20200710174257_add_index_to_broadcasts.rb
Normal 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
|
||||
16
db/schema.rb
16
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"
|
||||
|
|
|
|||
|
|
@ -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
|
||||
Loading…
Add table
Reference in a new issue