From e29afa200aaf76acf912b4ab663b2072c157b169 Mon Sep 17 00:00:00 2001 From: David Taylor Date: Wed, 17 Jun 2020 15:57:32 +0100 Subject: [PATCH] FIX: Cleanup migrations with timestamps in the future A future-dated migration was accidently introduced by me in 45c399f0. This was removed in b9762afc, but other migrations had already been generated based on its incorrect date. This commit removes the offending data in the schema_migrations table, and corrects the version in the published_pages migration. This commit also adds a check to db:migrate which raises an error when invalid migration timestamps are used. --- ...300_add_public_field_to_published_pages.rb | 25 +++++++++++++++++++ ...701_add_public_field_to_published_pages.rb | 7 ------ lib/tasks/db.rake | 6 +++++ 3 files changed, 31 insertions(+), 7 deletions(-) create mode 100644 db/migrate/20200617144300_add_public_field_to_published_pages.rb delete mode 100644 db/migrate/20201006172701_add_public_field_to_published_pages.rb diff --git a/db/migrate/20200617144300_add_public_field_to_published_pages.rb b/db/migrate/20200617144300_add_public_field_to_published_pages.rb new file mode 100644 index 00000000000..2bdfbfc50e3 --- /dev/null +++ b/db/migrate/20200617144300_add_public_field_to_published_pages.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +class AddPublicFieldToPublishedPages < ActiveRecord::Migration[6.0] + def up + # Delete the record of https://github.com/discourse/discourse/commit/b9762afc106ee9b18d1ac33ca3cac281083e428e + execute <<~SQL + DELETE FROM schema_migrations WHERE version='20201006172700' + SQL + + # Delete the reference to the incorrectly versioned version of this migration + execute <<~SQL + DELETE FROM schema_migrations WHERE version='20201006172701' + SQL + + # Using IF NOT EXISTS because the version number of this migration was changed + # Therefore some sites may have already added the column + execute <<~SQL + ALTER TABLE "published_pages" ADD COLUMN IF NOT EXISTS "public" boolean DEFAULT FALSE NOT NULL + SQL + end + + def down + raise ActiveRecord::IrreversibleMigration + end +end diff --git a/db/migrate/20201006172701_add_public_field_to_published_pages.rb b/db/migrate/20201006172701_add_public_field_to_published_pages.rb deleted file mode 100644 index d02edd2bd9d..00000000000 --- a/db/migrate/20201006172701_add_public_field_to_published_pages.rb +++ /dev/null @@ -1,7 +0,0 @@ -# frozen_string_literal: true - -class AddPublicFieldToPublishedPages < ActiveRecord::Migration[6.0] - def change - add_column :published_pages, :public, :boolean, null: false, default: false - end -end diff --git a/lib/tasks/db.rake b/lib/tasks/db.rake index fdd463b3d17..c837a4525b7 100644 --- a/lib/tasks/db.rake +++ b/lib/tasks/db.rake @@ -187,6 +187,12 @@ end # we need to run seed_fu every time we run rake db:migrate task 'db:migrate' => ['load_config', 'environment', 'set_locale'] do |_, args| + migrations = ActiveRecord::Base.connection.migration_context.migrations + now_timestamp = Time.now.utc.strftime('%Y%m%d%H%M%S').to_i + epoch_timestamp = Time.at(0).utc.strftime('%Y%m%d%H%M%S').to_i + + raise "Migration #{migrations.last.version} is timestamped in the future" if migrations.last.version > now_timestamp + raise "Migration #{migrations.first.version} is timestamped before the epoch" if migrations.first.version < epoch_timestamp ActiveRecord::Tasks::DatabaseTasks.migrate