Skip to content
代码片段 群组 项目
提交 b627dc24 编辑于 作者: Felipe Artur's avatar Felipe Artur
浏览文件

Merge branch 'extract-database-warnings' into 'master'

Extract database warnings to a separate module

See merge request https://gitlab.com/gitlab-org/gitlab/-/merge_requests/131760



Merged-by: default avatarFelipe Artur <fcardozo@gitlab.com>
Approved-by: default avatarFelipe Artur <fcardozo@gitlab.com>
Approved-by: default avatarOmar Qunsul <oqunsul@gitlab.com>
Approved-by: default avatarSiddharth Dungarwal <sdungarwal@gitlab.com>
Co-authored-by: default avatarArturo Herrero <arturo.herrero@gmail.com>
No related branches found
No related tags found
无相关合并请求
......@@ -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'
......
......@@ -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'
......
# frozen_string_literal: true
Gitlab::Database.check_single_connection_and_print_warning
Gitlab::DatabaseWarnings.check_single_connection_and_print_warning
......@@ -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
......@@ -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"))
......
# 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
......@@ -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
......
# 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')
......@@ -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
......
# 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
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册