From dee10e18bbb2174bd9bc7498859b3a26f76e77a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Trzci=C5=84ski?= <ayufan@ayufan.eu> Date: Tue, 15 Mar 2022 13:40:51 +0100 Subject: [PATCH] Backport `database_tasks:` from Rails 7.x For some configurations of databases we should skip creating, dropping or running migrations. In particular this applies to Phase 4, where we should not create, drop or migrate the `ci:` if it points to the same database as `main:`. This is well supported in Rails 7. This backports exact same config to achieve exactly the same outcome. --- Rakefile | 2 + ...ecord_database_tasks_configuration_flag.rb | 37 +++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 config/initializers/01_active_record_database_tasks_configuration_flag.rb diff --git a/Rakefile b/Rakefile index eb2f158972d5..9545516d2a91 100755 --- a/Rakefile +++ b/Rakefile @@ -11,6 +11,8 @@ require File.expand_path('config/application', __dir__) relative_url_conf = File.expand_path('config/initializers/relative_url', __dir__) require relative_url_conf if File.exist?("#{relative_url_conf}.rb") +require File.expand_path('config/initializers/01_active_record_database_tasks_configuration_flag.rb', __dir__) + Gitlab::Application.load_tasks Knapsack.load_tasks if defined?(Knapsack) diff --git a/config/initializers/01_active_record_database_tasks_configuration_flag.rb b/config/initializers/01_active_record_database_tasks_configuration_flag.rb new file mode 100644 index 000000000000..37374a41a30c --- /dev/null +++ b/config/initializers/01_active_record_database_tasks_configuration_flag.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +if Rails::VERSION::MAJOR >= 7 + raise "Remove `#{__FILE__}`. This is backport of `database_tasks:` Rails 7.x feature." +end + +# This backports `database_tasks:` feature to skip running migrations for some databases +# PR: https://github.com/rails/rails/pull/42794/files + +module DatabaseTasks + module ActiveRecordDatabaseConfigurations + def configs_for(env_name: nil, name: nil, include_replicas: false) + configs = super + + unless include_replicas + if name + configs = nil unless configs&.database_tasks? + else + configs = configs.select do |db_config| + db_config.database_tasks? + end + end + end + + configs + end + end + + module ActiveRecordDatabaseConfigurationsHashConfig + def database_tasks? # :nodoc: + !replica? && !!configuration_hash.fetch(:database_tasks, true) + end + end +end + +ActiveRecord::DatabaseConfigurations.prepend(DatabaseTasks::ActiveRecordDatabaseConfigurations) +ActiveRecord::DatabaseConfigurations::HashConfig.prepend(DatabaseTasks::ActiveRecordDatabaseConfigurationsHashConfig) -- GitLab