diff --git a/.rubocop_todo/gitlab/doc_url.yml b/.rubocop_todo/gitlab/doc_url.yml index 13537a142a16db4696f027f9df8be8e53047b779..ba9d066f94cd7d7056f86419ceadad733545dfb0 100644 --- a/.rubocop_todo/gitlab/doc_url.yml +++ b/.rubocop_todo/gitlab/doc_url.yml @@ -31,7 +31,7 @@ Gitlab/DocUrl: - 'lib/gitlab/ci/config/entry/processable.rb' - 'lib/gitlab/config_checker/external_database_checker.rb' - 'lib/gitlab/config_checker/puma_rugged_checker.rb' - - 'lib/gitlab/database.rb' + - 'lib/gitlab/database_warnings.rb' - 'lib/gitlab/database/migration_helpers/automatic_lock_writes_on_tables.rb' - 'lib/gitlab/database/migration_helpers/v2.rb' - 'lib/gitlab/database/migrations/batched_background_migration_helpers.rb' diff --git a/.rubocop_todo/naming/heredoc_delimiter_naming.yml b/.rubocop_todo/naming/heredoc_delimiter_naming.yml index 68129f910d141584d2874192de4206a4522f2de0..fa60792bbdc151729fb2fa0ce86a0b08b5087323 100644 --- a/.rubocop_todo/naming/heredoc_delimiter_naming.yml +++ b/.rubocop_todo/naming/heredoc_delimiter_naming.yml @@ -30,7 +30,6 @@ Naming/HeredocDelimiterNaming: - 'lib/feature/shared.rb' - 'lib/gitlab/cache/import/caching.rb' - 'lib/gitlab/conflict/file_collection.rb' - - 'lib/gitlab/database.rb' - 'lib/gitlab/database/migration_helpers.rb' - 'lib/gitlab/database/migration_helpers/v2.rb' - 'lib/gitlab/exclusive_lease.rb' diff --git a/config/initializers/1_database_single_connection.rb b/config/initializers/1_database_single_connection.rb index cba203ab1cca99572be162d1fd63ca94664b89cf..d71c82c2ea7a834a23d04dbeaf461261a5eb8e3d 100644 --- a/config/initializers/1_database_single_connection.rb +++ b/config/initializers/1_database_single_connection.rb @@ -1,3 +1,3 @@ # frozen_string_literal: true -Gitlab::Database.check_single_connection_and_print_warning +Gitlab::DatabaseWarnings.check_single_connection_and_print_warning diff --git a/config/initializers/1_postgresql_only.rb b/config/initializers/1_postgresql_only.rb index 3be55255dddb2298f87024ee824d437c5e581dc2..f0bdbbd1884b7d67ead78500273807f6a27c2166 100644 --- a/config/initializers/1_postgresql_only.rb +++ b/config/initializers/1_postgresql_only.rb @@ -3,4 +3,4 @@ raise "PostgreSQL is the only supported database from GitLab 12.1" unless ApplicationRecord.database.postgresql? -Gitlab::Database.check_postgres_version_and_print_warning +Gitlab::DatabaseWarnings.check_postgres_version_and_print_warning diff --git a/ee/lib/ee/gitlab/database.rb b/ee/lib/ee/gitlab/database.rb index 1346b678fdcfc9b2b7be4c311d78fe4790d880f0..c420f153694b04a7910dbf0bb2799ce6bab2d0b8 100644 --- a/ee/lib/ee/gitlab/database.rb +++ b/ee/lib/ee/gitlab/database.rb @@ -8,13 +8,6 @@ module Database class_methods do extend ::Gitlab::Utils::Override - override :check_postgres_version_and_print_warning - def check_postgres_version_and_print_warning - super - rescue ::Geo::TrackingBase::SecondaryNotConfigured - # ignore - happens when Rake tasks yet have to create a database, e.g. for testing - end - override :all_database_connection_files def all_database_connection_files super + Dir.glob(Rails.root.join("ee/db/database_connections/*.yaml")) diff --git a/ee/lib/ee/gitlab/database_warnings.rb b/ee/lib/ee/gitlab/database_warnings.rb new file mode 100644 index 0000000000000000000000000000000000000000..79180b9375b63436d43766f4eef09e790df682c1 --- /dev/null +++ b/ee/lib/ee/gitlab/database_warnings.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +module EE + module Gitlab + module DatabaseWarnings + extend ActiveSupport::Concern + + class_methods do + extend ::Gitlab::Utils::Override + + override :check_postgres_version_and_print_warning + def check_postgres_version_and_print_warning + super + rescue ::Geo::TrackingBase::SecondaryNotConfigured + # ignore - happens when Rake tasks yet have to create a database, e.g. for testing + end + end + end + end +end diff --git a/lib/gitlab/database.rb b/lib/gitlab/database.rb index 54eb0c0e0aab17a4450d25f4e9a0b32387553c5f..6222155d812f78eb0879e040926d0ec7f059299b 100644 --- a/lib/gitlab/database.rb +++ b/lib/gitlab/database.rb @@ -185,59 +185,6 @@ def self.check_for_non_superuser end # rubocop: enable CodeReuse/ActiveRecord - def self.check_postgres_version_and_print_warning - return if Gitlab::Runtime.rails_runner? - - database_base_models.each do |name, model| - database = Gitlab::Database::Reflection.new(model) - - next if database.postgresql_minimum_supported_version? - - Kernel.warn ERB.new(Rainbow.new.wrap(<<~EOS).red).result - - ██†██†█████†██████†███†██â€â–ˆâ–ˆâ€â–ˆâ–ˆâ–ˆâ€ ██†██████†- ██†██â€â–ˆâ–ˆâ€â€â€â–ˆâ–ˆâ€â–ˆâ–ˆâ€â€â€â–ˆâ–ˆâ€â–ˆâ–ˆâ–ˆâ–ˆâ€ ██â€â–ˆâ–ˆâ€â–ˆâ–ˆâ–ˆâ–ˆâ€ ██â€â–ˆâ–ˆâ€â€â€â€â€â€ - ██†█†██â€â–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ€â–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ€â€â–ˆâ–ˆâ€â–ˆâ–ˆâ€ ██â€â–ˆâ–ˆâ€â–ˆâ–ˆâ€â–ˆâ–ˆâ€ ██â€â–ˆâ–ˆâ€ ███†- ██â€â–ˆâ–ˆâ–ˆâ€â–ˆâ–ˆâ€â–ˆâ–ˆâ€â€â€â–ˆâ–ˆâ€â–ˆâ–ˆâ€â€â€â–ˆâ–ˆâ€â–ˆâ–ˆâ€â€â–ˆâ–ˆâ€â–ˆâ–ˆâ€â–ˆâ–ˆâ€â–ˆâ–ˆâ€â€â–ˆâ–ˆâ€â–ˆâ–ˆâ€â–ˆâ–ˆâ€ ██†- â€â–ˆâ–ˆâ–ˆâ€â–ˆâ–ˆâ–ˆâ€â€â–ˆâ–ˆâ€ ██â€â–ˆâ–ˆâ€ ██â€â–ˆâ–ˆâ€ â€â–ˆâ–ˆâ–ˆâ–ˆâ€â–ˆâ–ˆâ€â–ˆâ–ˆâ€ â€â–ˆâ–ˆâ–ˆâ–ˆâ€â€â–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ€â€ - - ****************************************************************************** - You are using PostgreSQL #{database.version} for the #{name} database, but this version of GitLab requires PostgreSQL >= <%= Gitlab::Database::MINIMUM_POSTGRES_VERSION %>. - <% if Rails.env.development? || Rails.env.test? %> - If using gitlab-development-kit, please find the relevant steps here: - https://gitlab.com/gitlab-org/gitlab-development-kit/-/blob/main/doc/howto/postgresql.md#upgrade-postgresql - <% end %> - Please upgrade your environment to a supported PostgreSQL version. See - https://docs.gitlab.com/ee/install/requirements.html#database for details. - ****************************************************************************** - EOS - rescue ActiveRecord::ActiveRecordError, PG::Error - # ignore - happens when Rake tasks yet have to create a database, e.g. for testing - end - end - - def self.check_single_connection_and_print_warning - return if Gitlab::Runtime.rails_runner? - return unless database_mode == MODE_SINGLE_DATABASE - - Kernel.warn ERB.new(Rainbow.new.wrap(<<~EOS).red).result - - ██†██†█████†██████†███†██â€â–ˆâ–ˆâ€â–ˆâ–ˆâ–ˆâ€ ██†██████†- ██†██â€â–ˆâ–ˆâ€â€â€â–ˆâ–ˆâ€â–ˆâ–ˆâ€â€â€â–ˆâ–ˆâ€â–ˆâ–ˆâ–ˆâ–ˆâ€ ██â€â–ˆâ–ˆâ€â–ˆâ–ˆâ–ˆâ–ˆâ€ ██â€â–ˆâ–ˆâ€â€â€â€â€â€ - ██†█†██â€â–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ€â–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ€â€â–ˆâ–ˆâ€â–ˆâ–ˆâ€ ██â€â–ˆâ–ˆâ€â–ˆâ–ˆâ€â–ˆâ–ˆâ€ ██â€â–ˆâ–ˆâ€ ███†- ██â€â–ˆâ–ˆâ–ˆâ€â–ˆâ–ˆâ€â–ˆâ–ˆâ€â€â€â–ˆâ–ˆâ€â–ˆâ–ˆâ€â€â€â–ˆâ–ˆâ€â–ˆâ–ˆâ€â€â–ˆâ–ˆâ€â–ˆâ–ˆâ€â–ˆâ–ˆâ€â–ˆâ–ˆâ€â€â–ˆâ–ˆâ€â–ˆâ–ˆâ€â–ˆâ–ˆâ€ ██†- â€â–ˆâ–ˆâ–ˆâ€â–ˆâ–ˆâ–ˆâ€â€â–ˆâ–ˆâ€ ██â€â–ˆâ–ˆâ€ ██â€â–ˆâ–ˆâ€ â€â–ˆâ–ˆâ–ˆâ–ˆâ€â–ˆâ–ˆâ€â–ˆâ–ˆâ€ â€â–ˆâ–ˆâ–ˆâ–ˆâ€â€â–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ€â€ - - ****************************************************************************** - Your database has a single connection, and single connections were - deprecated in GitLab 15.9 https://docs.gitlab.com/ee/update/deprecations.html#single-database-connection-is-deprecated. - - Please add a :ci section to your database, following these instructions: - https://docs.gitlab.com/ee/install/installation.html#configure-gitlab-db-settings. - ****************************************************************************** - EOS - end - def self.random "RANDOM()" end diff --git a/lib/gitlab/database_warnings.rb b/lib/gitlab/database_warnings.rb new file mode 100644 index 0000000000000000000000000000000000000000..8df60ac6404d9691d9785c6c6375e65e97a3a84f --- /dev/null +++ b/lib/gitlab/database_warnings.rb @@ -0,0 +1,60 @@ +# frozen_string_literal: true + +module Gitlab + module DatabaseWarnings + def self.check_postgres_version_and_print_warning + return if Gitlab::Runtime.rails_runner? + + Gitlab::Database.database_base_models.each do |name, model| + database = Gitlab::Database::Reflection.new(model) + + next if database.postgresql_minimum_supported_version? + + Kernel.warn ERB.new(Rainbow.new.wrap(<<~WARNING).red).result + + ██†██†█████†██████†███†██â€â–ˆâ–ˆâ€â–ˆâ–ˆâ–ˆâ€ ██†██████†+ ██†██â€â–ˆâ–ˆâ€â€â€â–ˆâ–ˆâ€â–ˆâ–ˆâ€â€â€â–ˆâ–ˆâ€â–ˆâ–ˆâ–ˆâ–ˆâ€ ██â€â–ˆâ–ˆâ€â–ˆâ–ˆâ–ˆâ–ˆâ€ ██â€â–ˆâ–ˆâ€â€â€â€â€â€ + ██†█†██â€â–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ€â–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ€â€â–ˆâ–ˆâ€â–ˆâ–ˆâ€ ██â€â–ˆâ–ˆâ€â–ˆâ–ˆâ€â–ˆâ–ˆâ€ ██â€â–ˆâ–ˆâ€ ███†+ ██â€â–ˆâ–ˆâ–ˆâ€â–ˆâ–ˆâ€â–ˆâ–ˆâ€â€â€â–ˆâ–ˆâ€â–ˆâ–ˆâ€â€â€â–ˆâ–ˆâ€â–ˆâ–ˆâ€â€â–ˆâ–ˆâ€â–ˆâ–ˆâ€â–ˆâ–ˆâ€â–ˆâ–ˆâ€â€â–ˆâ–ˆâ€â–ˆâ–ˆâ€â–ˆâ–ˆâ€ ██†+ â€â–ˆâ–ˆâ–ˆâ€â–ˆâ–ˆâ–ˆâ€â€â–ˆâ–ˆâ€ ██â€â–ˆâ–ˆâ€ ██â€â–ˆâ–ˆâ€ â€â–ˆâ–ˆâ–ˆâ–ˆâ€â–ˆâ–ˆâ€â–ˆâ–ˆâ€ â€â–ˆâ–ˆâ–ˆâ–ˆâ€â€â–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ€â€ + + ****************************************************************************** + You are using PostgreSQL #{database.version} for the #{name} database, but this version of GitLab requires PostgreSQL >= <%= Gitlab::Database::MINIMUM_POSTGRES_VERSION %>. + <% if Rails.env.development? || Rails.env.test? %> + If using gitlab-development-kit, please find the relevant steps here: + https://gitlab.com/gitlab-org/gitlab-development-kit/-/blob/main/doc/howto/postgresql.md#upgrade-postgresql + <% end %> + Please upgrade your environment to a supported PostgreSQL version. See + https://docs.gitlab.com/ee/install/requirements.html#database for details. + ****************************************************************************** + WARNING + rescue ActiveRecord::ActiveRecordError, PG::Error + # ignore - happens when Rake tasks yet have to create a database, e.g. for testing + end + end + + def self.check_single_connection_and_print_warning + return if Gitlab::Runtime.rails_runner? + return unless Gitlab::Database.database_mode == Gitlab::Database::MODE_SINGLE_DATABASE + + Kernel.warn ERB.new(Rainbow.new.wrap(<<~WARNING).red).result + + ██†██†█████†██████†███†██â€â–ˆâ–ˆâ€â–ˆâ–ˆâ–ˆâ€ ██†██████†+ ██†██â€â–ˆâ–ˆâ€â€â€â–ˆâ–ˆâ€â–ˆâ–ˆâ€â€â€â–ˆâ–ˆâ€â–ˆâ–ˆâ–ˆâ–ˆâ€ ██â€â–ˆâ–ˆâ€â–ˆâ–ˆâ–ˆâ–ˆâ€ ██â€â–ˆâ–ˆâ€â€â€â€â€â€ + ██†█†██â€â–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ€â–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ€â€â–ˆâ–ˆâ€â–ˆâ–ˆâ€ ██â€â–ˆâ–ˆâ€â–ˆâ–ˆâ€â–ˆâ–ˆâ€ ██â€â–ˆâ–ˆâ€ ███†+ ██â€â–ˆâ–ˆâ–ˆâ€â–ˆâ–ˆâ€â–ˆâ–ˆâ€â€â€â–ˆâ–ˆâ€â–ˆâ–ˆâ€â€â€â–ˆâ–ˆâ€â–ˆâ–ˆâ€â€â–ˆâ–ˆâ€â–ˆâ–ˆâ€â–ˆâ–ˆâ€â–ˆâ–ˆâ€â€â–ˆâ–ˆâ€â–ˆâ–ˆâ€â–ˆâ–ˆâ€ ██†+ â€â–ˆâ–ˆâ–ˆâ€â–ˆâ–ˆâ–ˆâ€â€â–ˆâ–ˆâ€ ██â€â–ˆâ–ˆâ€ ██â€â–ˆâ–ˆâ€ â€â–ˆâ–ˆâ–ˆâ–ˆâ€â–ˆâ–ˆâ€â–ˆâ–ˆâ€ â€â–ˆâ–ˆâ–ˆâ–ˆâ€â€â–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ€â€ + + ****************************************************************************** + Your database has a single connection, and single connections were + deprecated in GitLab 15.9 https://docs.gitlab.com/ee/update/deprecations.html#single-database-connection-is-deprecated. + + Please add a :ci section to your database, following these instructions: + https://docs.gitlab.com/ee/install/installation.html#configure-gitlab-db-settings. + ****************************************************************************** + WARNING + end + end +end + +Gitlab::DatabaseWarnings.prepend_mod_with('Gitlab::DatabaseWarnings') diff --git a/spec/lib/gitlab/database_spec.rb b/spec/lib/gitlab/database_spec.rb index 5af8f8c7bddc1aa8a6fdb9d6b04b60a113a3aef5..6dd7d29ab42262f359ab884d3de9dcb426e9d0d2 100644 --- a/spec/lib/gitlab/database_spec.rb +++ b/spec/lib/gitlab/database_spec.rb @@ -198,96 +198,6 @@ end end - describe '.check_postgres_version_and_print_warning' do - let(:reflect) { instance_spy(Gitlab::Database::Reflection) } - - subject { described_class.check_postgres_version_and_print_warning } - - before do - allow(Gitlab::Database::Reflection) - .to receive(:new) - .and_return(reflect) - end - - it 'prints a warning if not compliant with minimum postgres version' do - allow(reflect).to receive(:postgresql_minimum_supported_version?).and_return(false) - - expect(Kernel) - .to receive(:warn) - .with(/You are using PostgreSQL/) - .exactly(described_class.database_base_models.length) - .times - - subject - end - - it 'doesnt print a warning if compliant with minimum postgres version' do - allow(reflect).to receive(:postgresql_minimum_supported_version?).and_return(true) - - expect(Kernel).not_to receive(:warn).with(/You are using PostgreSQL/) - - subject - end - - it 'doesnt print a warning in Rails runner environment' do - allow(reflect).to receive(:postgresql_minimum_supported_version?).and_return(false) - allow(Gitlab::Runtime).to receive(:rails_runner?).and_return(true) - - expect(Kernel).not_to receive(:warn).with(/You are using PostgreSQL/) - - subject - end - - it 'ignores ActiveRecord errors' do - allow(reflect).to receive(:postgresql_minimum_supported_version?).and_raise(ActiveRecord::ActiveRecordError) - - expect { subject }.not_to raise_error - end - - it 'ignores Postgres errors' do - allow(reflect).to receive(:postgresql_minimum_supported_version?).and_raise(PG::Error) - - expect { subject }.not_to raise_error - end - end - - describe '.check_single_connection_and_print_warning' do - subject { described_class.check_single_connection_and_print_warning } - - it 'prints a warning if single connection' do - allow(described_class).to receive(:database_mode).and_return(::Gitlab::Database::MODE_SINGLE_DATABASE) - - expect(Kernel).to receive(:warn).with(/Your database has a single connection/) - - subject - end - - it 'does not print a warning if single ci connection' do - allow(described_class).to receive(:database_mode).and_return(::Gitlab::Database::MODE_SINGLE_DATABASE_CI_CONNECTION) - - expect(Kernel).not_to receive(:warn) - - subject - end - - it 'does not print a warning if multiple connection' do - allow(described_class).to receive(:database_mode).and_return(::Gitlab::Database::MODE_MULTIPLE_DATABASES) - - expect(Kernel).not_to receive(:warn) - - subject - end - - it 'does not print a warning in Rails runner environment' do - allow(described_class).to receive(:database_mode).and_return(::Gitlab::Database::MODE_SINGLE_DATABASE) - allow(Gitlab::Runtime).to receive(:rails_runner?).and_return(true) - - expect(Kernel).not_to receive(:warn) - - subject - end - end - describe '.db_config_for_connection' do context 'when the regular connection is used' do it 'returns db_config' do diff --git a/spec/lib/gitlab/database_warnings_spec.rb b/spec/lib/gitlab/database_warnings_spec.rb new file mode 100644 index 0000000000000000000000000000000000000000..6658190b94ca571d3e827c4dfe9daef5b6489304 --- /dev/null +++ b/spec/lib/gitlab/database_warnings_spec.rb @@ -0,0 +1,96 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::DatabaseWarnings, feature_category: :database do + describe '.check_postgres_version_and_print_warning' do + let(:reflect) { instance_spy(Gitlab::Database::Reflection) } + + subject { described_class.check_postgres_version_and_print_warning } + + before do + allow(Gitlab::Database::Reflection) + .to receive(:new) + .and_return(reflect) + end + + it 'prints a warning if not compliant with minimum postgres version' do + allow(reflect).to receive(:postgresql_minimum_supported_version?).and_return(false) + + expect(Kernel) + .to receive(:warn) + .with(/You are using PostgreSQL/) + .exactly(Gitlab::Database.database_base_models.length) + .times + + subject + end + + it 'does not print a warning if compliant with minimum postgres version' do + allow(reflect).to receive(:postgresql_minimum_supported_version?).and_return(true) + + expect(Kernel).not_to receive(:warn).with(/You are using PostgreSQL/) + + subject + end + + it 'does not print a warning in Rails runner environment' do + allow(reflect).to receive(:postgresql_minimum_supported_version?).and_return(false) + allow(Gitlab::Runtime).to receive(:rails_runner?).and_return(true) + + expect(Kernel).not_to receive(:warn).with(/You are using PostgreSQL/) + + subject + end + + it 'ignores ActiveRecord errors' do + allow(reflect).to receive(:postgresql_minimum_supported_version?).and_raise(ActiveRecord::ActiveRecordError) + + expect { subject }.not_to raise_error + end + + it 'ignores Postgres errors' do + allow(reflect).to receive(:postgresql_minimum_supported_version?).and_raise(PG::Error) + + expect { subject }.not_to raise_error + end + end + + describe '.check_single_connection_and_print_warning' do + subject { described_class.check_single_connection_and_print_warning } + + it 'prints a warning if single connection' do + allow(Gitlab::Database).to receive(:database_mode).and_return(Gitlab::Database::MODE_SINGLE_DATABASE) + + expect(Kernel).to receive(:warn).with(/Your database has a single connection/) + + subject + end + + it 'does not print a warning if single ci connection' do + allow(Gitlab::Database).to receive(:database_mode) + .and_return(Gitlab::Database::MODE_SINGLE_DATABASE_CI_CONNECTION) + + expect(Kernel).not_to receive(:warn) + + subject + end + + it 'does not print a warning if multiple connection' do + allow(Gitlab::Database).to receive(:database_mode).and_return(Gitlab::Database::MODE_MULTIPLE_DATABASES) + + expect(Kernel).not_to receive(:warn) + + subject + end + + it 'does not print a warning in Rails runner environment' do + allow(Gitlab::Database).to receive(:database_mode).and_return(Gitlab::Database::MODE_SINGLE_DATABASE) + allow(Gitlab::Runtime).to receive(:rails_runner?).and_return(true) + + expect(Kernel).not_to receive(:warn) + + subject + end + end +end