Commit dee10e18 authored by Kamil Trzciński's avatar Kamil Trzciński

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.
parent c33c5d65
......@@ -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)
# 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)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment