From 32ee50f278056aa15220bc82bb74aacebdf441eb Mon Sep 17 00:00:00 2001 From: Jamie Gaskins Date: Fri, 8 Oct 2021 15:25:04 -0400 Subject: [PATCH] Fix Sitemap for tags (#14989) Set a NOT NULL constraint on tags timestamps This commit also monkeypatches in a method for StrongMigrations to disable a check temporarily. This migration is safe enough for DEV (it's not ideal, but it'll only lock the table for a matter of milliseconds), so we'll be fine disabling it but we don't want to disable the check globally for all migrations in case there's a migration where this is not safe. --- config/initializers/strong_migrations.rb | 10 ++++++ ...d_not_null_constraint_to_tag_timestamps.rb | 32 +++++++++++++++++++ db/schema.rb | 6 ++-- 3 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20211008170433_add_not_null_constraint_to_tag_timestamps.rb diff --git a/config/initializers/strong_migrations.rb b/config/initializers/strong_migrations.rb index 0a8a27e72..01872026f 100644 --- a/config/initializers/strong_migrations.rb +++ b/config/initializers/strong_migrations.rb @@ -9,3 +9,13 @@ StrongMigrations.target_postgresql_version = 11 # https://github.com/ankane/strong_migrations#down-migrations--rollbacks StrongMigrations.check_down = true + +module StrongMigrations + def self.temporarily_disable_check(check) + disable_check check + + yield + ensure + enable_check check + end +end diff --git a/db/migrate/20211008170433_add_not_null_constraint_to_tag_timestamps.rb b/db/migrate/20211008170433_add_not_null_constraint_to_tag_timestamps.rb new file mode 100644 index 000000000..a9880b50c --- /dev/null +++ b/db/migrate/20211008170433_add_not_null_constraint_to_tag_timestamps.rb @@ -0,0 +1,32 @@ +class AddNotNullConstraintToTagTimestamps < ActiveRecord::Migration[6.1] + # Timestamps were added to acts_as_taggable_on at this time + # See: https://github.com/mbleigh/acts-as-taggable-on/commit/ce14b9ead3a283577a976b6b0d9bfbec278e21bb + DEFAULT_TIMESTAMP = "2019-02-06 16:43:19" + + def up + # This migration will lock the table, so StrongMigrations is right to call + # it out, but this particular table isn't so large that a table lock will + # cause downtime. The EXPLAIN ANALYZE on the query that will be used to + # check this on DEV shows "Execution Time: 34.136 ms". We can handle a 34ms + # table lock. + StrongMigrations.temporarily_disable_check :change_column_null_postgresql do + Tag + .where(created_at: nil) + .update_all(created_at: DEFAULT_TIMESTAMP) + + Tag + .where(updated_at: nil) + .update_all(updated_at: DEFAULT_TIMESTAMP) + + change_column_null :tags, :created_at, false + change_column_null :tags, :updated_at, false + end + end + + def down + StrongMigrations.temporarily_disable_check :change_column_null_postgresql do + change_column_null :tags, :created_at, true + change_column_null :tags, :updated_at, true + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 368725edb..b6c022539 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: 2021_10_07_172232) do +ActiveRecord::Schema.define(version: 2021_10_08_170433) do # These are extensions that must be enabled in order to support this database enable_extension "citext" @@ -1189,7 +1189,7 @@ ActiveRecord::Schema.define(version: 2021_10_07_172232) do t.bigint "badge_id" t.string "bg_color_hex" t.string "category", default: "uncategorized", null: false - t.datetime "created_at" + t.datetime "created_at", null: false t.integer "hotness_score", default: 0 t.string "keywords_for_search" t.bigint "mod_chat_channel_id" @@ -1206,7 +1206,7 @@ ActiveRecord::Schema.define(version: 2021_10_07_172232) do t.boolean "supported", default: false t.integer "taggings_count", default: 0 t.string "text_color_hex" - t.datetime "updated_at" + t.datetime "updated_at", null: false t.text "wiki_body_html" t.text "wiki_body_markdown" t.index ["name"], name: "index_tags_on_name", unique: true