diff --git a/config/database.yml.decomposed-postgresql b/config/database.yml.decomposed-postgresql index 23c7f052f5a04109d12dbd024f77bc8f3ce582b5..04add959ad4d78186cbf695ccdee838fd2e0a0c2 100644 --- a/config/database.yml.decomposed-postgresql +++ b/config/database.yml.decomposed-postgresql @@ -16,6 +16,14 @@ production: username: git password: "secure password" host: localhost + geo: + adapter: postgresql + encoding: unicode + database: gitlabhq_geo_production + username: git + password: "secure password" + host: localhost + # # Development specific # @@ -38,6 +46,13 @@ development: host: localhost variables: statement_timeout: 15s + geo: + adapter: postgresql + encoding: unicode + database: gitlabhq_geo_development + username: postgres + password: "secure password" + host: localhost # Warning: The database defined as "test" will be erased and # re-generated from your development database when you run "rake". @@ -63,3 +78,10 @@ test: &test prepared_statements: false variables: statement_timeout: 15s + geo: + adapter: postgresql + encoding: unicode + database: gitlabhq_geo_test + username: postgres + password: + host: localhost diff --git a/config/database.yml.postgresql b/config/database.yml.postgresql index a4daab1fd0c7db2479b43ea5d08801153362f96a..5329a8e9fd7a05c70e442a5a842e25434c40b362 100644 --- a/config/database.yml.postgresql +++ b/config/database.yml.postgresql @@ -18,6 +18,13 @@ production: # port: 8600 # record: secondary.postgresql.service.consul # interval: 300 + geo: + adapter: postgresql + encoding: unicode + database: gitlabhq_geo_production + username: git + password: "secure password" + host: localhost # # Development specific @@ -32,6 +39,13 @@ development: host: localhost variables: statement_timeout: 15s + geo: + adapter: postgresql + encoding: unicode + database: gitlabhq_geo_development + username: postgres + password: "secure password" + host: localhost # # Staging specific @@ -44,6 +58,13 @@ staging: username: git password: "secure password" host: localhost + geo: + adapter: postgresql + encoding: unicode + database: gitlabhq_geo_staging + username: git + password: "secure password" + host: localhost # Warning: The database defined as "test" will be erased and # re-generated from your development database when you run "rake". @@ -59,3 +80,10 @@ test: &test prepared_statements: false variables: statement_timeout: 15s + geo: + adapter: postgresql + encoding: unicode + database: gitlabhq_geo_test + username: postgres + password: + host: localhost diff --git a/config/database_geo.yml.postgresql b/config/database_geo.yml.postgresql deleted file mode 100644 index 25b9c6d5eb147b42b177003d737eecb238dfc6ee..0000000000000000000000000000000000000000 --- a/config/database_geo.yml.postgresql +++ /dev/null @@ -1,43 +0,0 @@ -# -# PRODUCTION -# -production: - adapter: postgresql - encoding: unicode - database: gitlabhq_geo_production - username: git - password: "secure password" - host: localhost - -# -# Development specific -# -development: - adapter: postgresql - encoding: unicode - database: gitlabhq_geo_development - username: postgres - password: "secure password" - host: localhost - -# -# Staging specific -# -staging: - adapter: postgresql - encoding: unicode - database: gitlabhq_geo_staging - username: git - password: "secure password" - host: localhost - -# Warning: The database defined as "test" will be erased and -# re-generated from your development database when you run "rake". -# Do not set this db to the same as development or production. -test: &test - adapter: postgresql - encoding: unicode - database: gitlabhq_geo_test - username: postgres - password: - host: localhost diff --git a/config/initializers/database_config.rb b/config/initializers/database_config.rb index 050ab1d9b3ec5b53a61f39f952618a122dcd3c30..84ef0bc9f16ef9c075b5e41869f9a68ccb0a0a76 100644 --- a/config/initializers/database_config.rb +++ b/config/initializers/database_config.rb @@ -1,14 +1,6 @@ # frozen_string_literal: true Gitlab.ee do - # We need to initialize the Geo database before - # setting the Geo DB connection pool size. - if File.exist?(Rails.root.join('config/database_geo.yml')) - Rails.application.configure do - config.geo_database = config_for(:database_geo) - end - end - if Gitlab::Runtime.sidekiq? && Gitlab::Geo.geo_database_configured? # The Geo::TrackingBase model does not yet use connects_to. So, # this will not properly support geo: from config/databse.yml diff --git a/doc/development/geo.md b/doc/development/geo.md index f62b2de30db4d99eed906745a99fe5e3656fa4fa..5505d41ccc910b18789b12d0fc565897a9564679 100644 --- a/doc/development/geo.md +++ b/doc/development/geo.md @@ -188,7 +188,7 @@ needs to be applied to the tracking database on each **secondary** site. ### Configuration -The database configuration is set in [`config/database_geo.yml`](https://gitlab.com/gitlab-org/gitlab/-/blob/master/config/database_geo.yml.postgresql). +The database configuration is set in [`config/database.yml`](https://gitlab.com/gitlab-org/gitlab/-/blob/master/config/database.yml.postgresql). The directory [`ee/db/geo`](https://gitlab.com/gitlab-org/gitlab/-/tree/master/ee/db/geo) contains the schema and migrations for this database. diff --git a/ee/spec/lib/gitlab/patch/database_config_spec.rb b/ee/spec/lib/gitlab/patch/database_config_spec.rb index 34223cf07b05bd722ce2cb92b7d022ec6a1be75a..ac4045a0ae50e342c081d3941126469c5046b945 100644 --- a/ee/spec/lib/gitlab/patch/database_config_spec.rb +++ b/ee/spec/lib/gitlab/patch/database_config_spec.rb @@ -3,81 +3,6 @@ require 'spec_helper' RSpec.describe Gitlab::Patch::DatabaseConfig do - describe '#load_geo_database_yaml' do - let(:configuration) { Rails::Application::Configuration.new(Rails.root) } - - context 'when config/database_geo.yml does not exist' do - before do - allow(File).to receive(:exist?).and_call_original - allow(File).to receive(:exist?).with(Rails.root.join("config/database_geo.yml")).and_return(false) - end - - it 'returns an empty hash' do - expect(configuration.load_geo_database_yaml).to eq({}) - end - end - - context 'when config/database_geo.yml exists' do - shared_examples 'hash containing geo: connection name' do - it 'returns a hash containing geo:' do - expect(configuration.load_geo_database_yaml).to match( - "production" => { "geo" => a_hash_including("adapter") }, - "development" => { "geo" => a_hash_including("adapter" => "postgresql") }, - "test" => { "geo" => a_hash_including("adapter" => "postgresql") } - ) - end - end - - before do - allow(Pathname) - .to receive(:new) - .and_call_original - - allow(Pathname) - .to receive(:new).with(Rails.root.join('config/database_geo.yml')) - .and_return(instance_double('Pathname', read: database_geo_yml)) - end - - let(:database_geo_yml) do - <<-EOS - production: - geo: - adapter: postgresql - encoding: unicode - database: gitlabhq_geo_production - username: git - password: "secure password" - host: localhost - - development: - geo: - adapter: postgresql - encoding: unicode - database: gitlabhq_geo_development - username: postgres - password: "secure password" - host: localhost - variables: - statement_timeout: 15s - - test: &test - geo: - adapter: postgresql - encoding: unicode - database: gitlabhq_geo_test - username: postgres - password: - host: localhost - prepared_statements: false - variables: - statement_timeout: 15s - EOS - end - - include_examples 'hash containing geo: connection name' - end - end - describe '#database_configuration' do let(:configuration) { Rails::Application::Configuration.new(Rails.root) } @@ -130,270 +55,110 @@ end end - context 'when config/database_geo.yml does not exist' do - before do - allow(File).to receive(:exist?).and_call_original - allow(File).to receive(:exist?).with(Rails.root.join("config/database_geo.yml")).and_return(false) - end - - context 'and does not contain Geo settings' do - let(:database_yml) do - <<-EOS - production: - main: - adapter: postgresql - encoding: unicode - database: gitlabhq_production - username: git - password: "secure password" - host: localhost - - development: - main: - adapter: postgresql - encoding: unicode - database: gitlabhq_development - username: postgres - password: "secure password" - host: localhost - variables: - statement_timeout: 15s - - test: &test - main: - adapter: postgresql - encoding: unicode - database: gitlabhq_test - username: postgres - password: - host: localhost - prepared_statements: false - variables: - statement_timeout: 15s - EOS - end - - include_examples 'hash containing main: connection name' + context 'when config/database.yml does not contain Geo settings' do + let(:database_yml) do + <<-EOS + production: + main: + adapter: postgresql + encoding: unicode + database: gitlabhq_production + username: git + password: "secure password" + host: localhost + + development: + main: + adapter: postgresql + encoding: unicode + database: gitlabhq_development + username: postgres + password: "secure password" + host: localhost + variables: + statement_timeout: 15s + + test: &test + main: + adapter: postgresql + encoding: unicode + database: gitlabhq_test + username: postgres + password: + host: localhost + prepared_statements: false + variables: + statement_timeout: 15s + EOS end - context 'contains Geo settings' do - let(:database_yml) do - <<-EOS - production: - main: - adapter: postgresql - encoding: unicode - database: gitlabhq_production - username: git - password: "secure password" - host: localhost - geo: - adapter: postgresql - encoding: unicode - database: gitlabhq_geo_production - username: git - password: "secure password" - host: localhost - - development: - main: - adapter: postgresql - encoding: unicode - database: gitlabhq_development - username: postgres - password: "secure password" - host: localhost - variables: - statement_timeout: 15s - geo: - adapter: postgresql - encoding: unicode - database: gitlabhq_geo_development - username: postgres - password: "secure password" - host: localhost - variables: - statement_timeout: 15s - - test: &test - main: - adapter: postgresql - encoding: unicode - database: gitlabhq_test - username: postgres - password: - host: localhost - prepared_statements: false - variables: - statement_timeout: 15s - geo: - adapter: postgresql - encoding: unicode - database: gitlabhq_geo_test - username: postgres - password: - host: localhost - prepared_statements: false - variables: - statement_timeout: 15s - EOS - end - - include_examples 'hash containing both main: and geo: connection names' - end + include_examples 'hash containing main: connection name' end - context 'when config/database_geo.yml exists' do - let(:database_geo_yml) do + context 'when config/database.yml contains Geo settings' do + let(:database_yml) do <<-EOS - production: - adapter: postgresql - encoding: unicode - database: gitlabhq_geo_production - username: git - password: "secure password" - host: localhost - - development: - adapter: postgresql - encoding: unicode - database: gitlabhq_geo_development - username: postgres - password: "secure password" - host: localhost - - staging: - adapter: postgresql - encoding: unicode - database: gitlabhq_geo_staging - username: git - password: "secure password" - host: localhost - - test: &test - adapter: postgresql - encoding: unicode - database: gitlabhq_geo_test - username: postgres - password: - host: localhost + production: + main: + adapter: postgresql + encoding: unicode + database: gitlabhq_production + username: git + password: "secure password" + host: localhost + geo: + adapter: postgresql + encoding: unicode + database: gitlabhq_geo_production + username: git + password: "secure password" + host: localhost + + development: + main: + adapter: postgresql + encoding: unicode + database: gitlabhq_development + username: postgres + password: "secure password" + host: localhost + variables: + statement_timeout: 15s + geo: + adapter: postgresql + encoding: unicode + database: gitlabhq_geo_development + username: postgres + password: "secure password" + host: localhost + variables: + statement_timeout: 15s + + test: &test + main: + adapter: postgresql + encoding: unicode + database: gitlabhq_test + username: postgres + password: + host: localhost + prepared_statements: false + variables: + statement_timeout: 15s + geo: + adapter: postgresql + encoding: unicode + database: gitlabhq_geo_test + username: postgres + password: + host: localhost + prepared_statements: false + variables: + statement_timeout: 15s EOS end - before do - # The `AS::ConfigurationFile` calls `read` in `def initialize` - # thus we cannot use `allow_next_instance_of` - # rubocop:disable RSpec/AnyInstanceOf - allow_any_instance_of(ActiveSupport::ConfigurationFile) - .to receive(:read).with(Rails.root.join('config/database_geo.yml')).and_return(database_geo_yml) - # rubocop:enable RSpec/AnyInstanceOf - end - - context 'and does not contain Geo setting' do - let(:database_yml) do - <<-EOS - production: - main: - adapter: postgresql - encoding: unicode - database: gitlabhq_production - username: git - password: "secure password" - host: localhost - - development: - main: - adapter: postgresql - encoding: unicode - database: gitlabhq_development - username: postgres - password: "secure password" - host: localhost - variables: - statement_timeout: 15s - - test: &test - main: - adapter: postgresql - encoding: unicode - database: gitlabhq_test - username: postgres - password: - host: localhost - prepared_statements: false - variables: - statement_timeout: 15s - EOS - end - - include_examples 'hash containing both main: and geo: connection names' - end - - context 'contains Geo setting' do - let(:database_yml) do - <<-EOS - production: - main: - adapter: postgresql - encoding: unicode - database: gitlabhq_production - username: git - password: "secure password" - host: localhost - geo: - adapter: postgresql - encoding: unicode - database: gitlabhq_geo_production - username: git - password: "secure password" - host: localhost - - development: - main: - adapter: postgresql - encoding: unicode - database: gitlabhq_development - username: postgres - password: "secure password" - host: localhost - variables: - statement_timeout: 15s - geo: - adapter: postgresql - encoding: unicode - database: gitlabhq_geo_development - username: postgres - password: "secure password" - host: localhost - variables: - statement_timeout: 15s - - test: &test - main: - adapter: postgresql - encoding: unicode - database: gitlabhq_test - username: postgres - password: - host: localhost - prepared_statements: false - variables: - statement_timeout: 15s - geo: - adapter: postgresql - encoding: unicode - database: gitlabhq_geo_test - username: postgres - password: - host: localhost - prepared_statements: false - variables: - statement_timeout: 15s - EOS - end - - include_examples 'hash containing both main: and geo: connection names' - end + include_examples 'hash containing both main: and geo: connection names' end end end diff --git a/lib/gitlab/patch/database_config.rb b/lib/gitlab/patch/database_config.rb index c5c73d505187ba79e8a66cc2702b3b27ee3e8b22..20d8f7be8fd43ca4beaa5c4c8695126e5870957b 100644 --- a/lib/gitlab/patch/database_config.rb +++ b/lib/gitlab/patch/database_config.rb @@ -1,77 +1,15 @@ # frozen_string_literal: true -# The purpose of this code is to transform legacy `database.yml` -# into a `database.yml` containing `main:` as a name of a first database -# -# This should be removed once all places using legacy `database.yml` -# are fixed. The likely moment to remove this check is the %14.0. -# -# This converts the following syntax: -# -# production: -# adapter: postgresql -# database: gitlabhq_production -# username: git -# password: "secure password" -# host: localhost -# -# Into: -# -# production: -# main: -# adapter: postgresql -# database: gitlabhq_production -# username: git -# password: "secure password" -# host: localhost -# - +# The purpose of this code is to set the migrations path +# for the Geo tracking database. module Gitlab module Patch module DatabaseConfig extend ActiveSupport::Concern - def load_database_yaml - return super unless Gitlab.ee? - - super.deep_merge(load_geo_database_yaml) - end - - # This method is taken from Rails to load a database YAML file without - # evaluating ERB. This allows us to create the rake tasks for the Geo - # tracking database without filling in the configuration values or - # loading the environment. To be removed when we start configure Geo - # tracking database in database.yml instead of custom database_geo.yml - # - # https://github.com/rails/rails/blob/v6.1.4/railties/lib/rails/application/configuration.rb#L255 - def load_geo_database_yaml - path = Rails.root.join("config/database_geo.yml") - return {} unless File.exist?(path) - - require "rails/application/dummy_erb_compiler" - - yaml = DummyERB.new(Pathname.new(path).read).result - config = YAML.load(yaml) || {} # rubocop:disable Security/YAMLLoad - - config.to_h do |env, configs| - # This check is taken from Rails where the transformation - # of a flat database.yml is done into `primary:` - # https://github.com/rails/rails/blob/v6.1.4/activerecord/lib/active_record/database_configurations.rb#L169 - if configs.is_a?(Hash) && !configs.all? { |_, v| v.is_a?(Hash) } - configs = { "geo" => configs } - end - - [env, configs] - end - end - def database_configuration super.to_h do |env, configs| if Gitlab.ee? - if !configs.key?("geo") && File.exist?(Rails.root.join("config/database_geo.yml")) - configs["geo"] = Rails.application.config_for(:database_geo).stringify_keys - end - if configs.key?("geo") migrations_paths = Array(configs["geo"]["migrations_paths"]) migrations_paths << "ee/db/geo/migrate" if migrations_paths.empty? diff --git a/scripts/prepare_build.sh b/scripts/prepare_build.sh index 4b5286963222fac69364a790c072f8c4b0205e04..d68432d9ec0bc300bea9b19426cb9c73c525874b 100644 --- a/scripts/prepare_build.sh +++ b/scripts/prepare_build.sh @@ -17,8 +17,11 @@ else cp config/database.yml.postgresql config/database.yml fi -if [ -f config/database_geo.yml.postgresql ]; then - cp config/database_geo.yml.postgresql config/database_geo.yml +# Remove Geo database setting if `ee/` directory does not exist. When it does +# not exist, it runs the GitLab test suite "as if FOSS", meaning the jobs run +# in the context of gitlab-org/gitlab-foss where the Geo is not available. +if [ ! -d "ee/" ] ; then + sed -i '/geo:/,/^$/d' config/database.yml fi # Set user to a non-superuser to ensure we test permissions @@ -27,11 +30,6 @@ sed -i 's/username: root/username: gitlab/g' config/database.yml sed -i 's/localhost/postgres/g' config/database.yml sed -i 's/username: git/username: postgres/g' config/database.yml -if [ -f config/database_geo.yml ]; then - sed -i 's/localhost/postgres/g' config/database_geo.yml - sed -i 's/username: git/username: postgres/g' config/database_geo.yml -fi - cp config/cable.yml.example config/cable.yml sed -i 's|url:.*$|url: redis://redis:6379|g' config/cable.yml diff --git a/spec/initializers/validate_database_config_spec.rb b/spec/initializers/validate_database_config_spec.rb index 5f3f950a852cc41e7079211726cd27df100a0957..23a3d9a2950087294343e1640caf9c0f2de44202 100644 --- a/spec/initializers/validate_database_config_spec.rb +++ b/spec/initializers/validate_database_config_spec.rb @@ -14,9 +14,6 @@ end before do - allow(File).to receive(:exist?).and_call_original - allow(File).to receive(:exist?).with(Rails.root.join("config/database_geo.yml")).and_return(false) - # The `AS::ConfigurationFile` calls `read` in `def initialize` # thus we cannot use `expect_next_instance_of` # rubocop:disable RSpec/AnyInstanceOf diff --git a/spec/lib/gitlab/patch/database_config_spec.rb b/spec/lib/gitlab/patch/database_config_spec.rb index 73dc84bb2ef293f3986ae0d3ac057bc7301403ee..b06d28dbcd5691a5646a1f9cc5fe5c4cb4c852f0 100644 --- a/spec/lib/gitlab/patch/database_config_spec.rb +++ b/spec/lib/gitlab/patch/database_config_spec.rb @@ -11,9 +11,6 @@ let(:configuration) { Rails::Application::Configuration.new(Rails.root) } before do - allow(File).to receive(:exist?).and_call_original - allow(File).to receive(:exist?).with(Rails.root.join("config/database_geo.yml")).and_return(false) - # The `AS::ConfigurationFile` calls `read` in `def initialize` # thus we cannot use `expect_next_instance_of` # rubocop:disable RSpec/AnyInstanceOf