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.
This commit is contained in:
Jamie Gaskins 2021-10-08 15:25:04 -04:00 committed by GitHub
parent e3abee660a
commit 32ee50f278
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 3 deletions

View file

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

View file

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

View file

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