From 70dc2429bccb0d212cec35cd59f8446e0dcaa671 Mon Sep 17 00:00:00 2001 From: rhymes Date: Mon, 1 Jun 2020 18:11:38 +0200 Subject: [PATCH] Add timestamps to broadcasts (#8181) * Add timestamps to broadcasts * Update schema version --- .../20200530084533_add_timestamps_to_broadcasts.rb | 7 +++++++ db/schema.rb | 4 +++- ...20200530085153_backfill_broadcasts_timestamps.rb | 13 +++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20200530084533_add_timestamps_to_broadcasts.rb create mode 100644 lib/data_update_scripts/20200530085153_backfill_broadcasts_timestamps.rb diff --git a/db/migrate/20200530084533_add_timestamps_to_broadcasts.rb b/db/migrate/20200530084533_add_timestamps_to_broadcasts.rb new file mode 100644 index 000000000..8e3316f04 --- /dev/null +++ b/db/migrate/20200530084533_add_timestamps_to_broadcasts.rb @@ -0,0 +1,7 @@ +class AddTimestampsToBroadcasts < ActiveRecord::Migration[5.2] + def change + # NOTE: we need to have nullable timestamps as the table is already + # populated. Will remove the NULL clause when data is safely backfilled + add_timestamps(:broadcasts, null: true) + end +end diff --git a/db/schema.rb b/db/schema.rb index 34ddbb435..571ff0ea3 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_05_26_151807) do +ActiveRecord::Schema.define(version: 2020_05_30_084533) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -268,9 +268,11 @@ ActiveRecord::Schema.define(version: 2020_05_26_151807) do create_table "broadcasts", id: :serial, force: :cascade do |t| t.boolean "active", default: false t.text "body_markdown" + t.datetime "created_at" t.text "processed_html" t.string "title" t.string "type_of" + t.datetime "updated_at" end create_table "buffer_updates", force: :cascade do |t| diff --git a/lib/data_update_scripts/20200530085153_backfill_broadcasts_timestamps.rb b/lib/data_update_scripts/20200530085153_backfill_broadcasts_timestamps.rb new file mode 100644 index 000000000..7530638fb --- /dev/null +++ b/lib/data_update_scripts/20200530085153_backfill_broadcasts_timestamps.rb @@ -0,0 +1,13 @@ +module DataUpdateScripts + class BackfillBroadcastsTimestamps + def run + # Broadcasts didn't have timestamps columns until + # 20200530084533_add_timestamps_to_broadcasts was created, thus we can + # safely assume that only those with `created_at IS NULL` need their + # timestamps overridden + Broadcast.where(created_at: nil).order(title: :asc).each do |cast| + cast.update(created_at: Time.current, updated_at: Time.current) + end + end + end +end