A platform for community discussion. Free, open, simple.
DEV: Introduce `Migration::Helpers` for new-site detection (#20934)
David Taylorf3402be262b3
diff --git a/db/migrate/20210527114834_set_tagging_enabled.rb b/db/migrate/20210527114834_set_tagging_enabled.rbindex 8e2dd92db46..2f988ed184f 100644--- a/db/migrate/20210527114834_set_tagging_enabled.rb+++ b/db/migrate/20210527114834_set_tagging_enabled.rb@@ -2,15 +2,8 @@class SetTaggingEnabled < ActiveRecord::Migration[6.1]def up- result = execute <<~SQL- SELECT created_at- FROM schema_migration_details- ORDER BY created_at- LIMIT 1- SQL-# keep tagging disabled for existing sites- execute <<~SQL if result.first["created_at"].to_datetime < 1.hour.ago+ execute <<~SQL if Migration::Helpers.existing_site?INSERT INTO site_settings(name, data_type, value, created_at, updated_at)VALUES('tagging_enabled', 5, 'f', NOW(), NOW())ON CONFLICT (name) DO NOTHINGdiff --git a/db/migrate/20220130192155_set_use_email_for_username_and_name_suggestions_on_existing_sites.rb b/db/migrate/20220130192155_set_use_email_for_username_and_name_suggestions_on_existing_sites.rbindex 29f9a078124..121f31f4382 100644--- a/db/migrate/20220130192155_set_use_email_for_username_and_name_suggestions_on_existing_sites.rb+++ b/db/migrate/20220130192155_set_use_email_for_username_and_name_suggestions_on_existing_sites.rb@@ -2,15 +2,8 @@class SetUseEmailForUsernameAndNameSuggestionsOnExistingSites < ActiveRecord::Migration[6.1]def up- result = execute <<~SQL- SELECT created_at- FROM schema_migration_details- ORDER BY created_at- LIMIT 1- SQL-# make setting enabled for existing sites- execute <<~SQL if result.first["created_at"].to_datetime < 1.hour.ago+ execute <<~SQL if Migration::Helpers.existing_site?INSERT INTO site_settings(name, data_type, value, created_at, updated_at)VALUES('use_email_for_username_and_name_suggestions', 5, 't', NOW(), NOW())ON CONFLICT (name) DO NOTHINGdiff --git a/db/migrate/20221212225921_enable_sidebar_and_chat.rb b/db/migrate/20221212225921_enable_sidebar_and_chat.rbindex 83b365dd49d..32c8b5f3497 100644--- a/db/migrate/20221212225921_enable_sidebar_and_chat.rb+++ b/db/migrate/20221212225921_enable_sidebar_and_chat.rb@@ -2,15 +2,8 @@class EnableSidebarAndChat < ActiveRecord::Migration[7.0]def up- result = execute <<~SQL- SELECT created_at- FROM schema_migration_details- ORDER BY created_at- LIMIT 1- SQL-- # keep sidebar legacy and chat disabled for for existing sites- if result.first["created_at"].to_datetime < 1.hour.ago+ # keep sidebar legacy and chat disabled for existing sites+ if Migration::Helpers.existing_site?execute <<~SQLINSERT INTO site_settings(name, data_type, value, created_at, updated_at)VALUES('chat_enabled', 5, 'f', NOW(), NOW())diff --git a/lib/migration/helpers.rb b/lib/migration/helpers.rbnew file mode 100644index 00000000000..3cf60dc05fc--- /dev/null+++ b/lib/migration/helpers.rb@@ -0,0 +1,23 @@+# frozen_string_literal: true++module Migration+ module Helpers+ def self.site_created_at+ result = DB.query_single <<~SQL+ SELECT created_at+ FROM schema_migration_details+ ORDER BY created_at+ LIMIT 1+ SQL+ result.first+ end++ def self.existing_site?+ site_created_at < 1.hour.ago+ end++ def self.new_site?+ !old_site?+ end+ end+end