Commit dccdaaeb authored by Thong Kuah's avatar Thong Kuah

Merge branch 'main-database-helper' into 'master'

Properly support `main:` and provide a helper for this

See merge request gitlab-org/gitlab!65054
parents c588ebae 727ce673
...@@ -393,11 +393,10 @@ db:migrate-from-previous-major-version: ...@@ -393,11 +393,10 @@ db:migrate-from-previous-major-version:
- sed -i -e "s/gem 'mimemagic', '~> 0.3.2'/gem 'ruby-magic', '~> 0.4.0'/" Gemfile - sed -i -e "s/gem 'mimemagic', '~> 0.3.2'/gem 'ruby-magic', '~> 0.4.0'/" Gemfile
- run_timed_command "gem install bundler:1.17.3" - run_timed_command "gem install bundler:1.17.3"
- run_timed_command "bundle update google-protobuf nokogiri grpc mimemagic bootsnap" - run_timed_command "bundle update google-protobuf nokogiri grpc mimemagic bootsnap"
- run_timed_command "bundle install ${BUNDLE_INSTALL_FLAGS}" - SETUP_DB=false USE_BUNDLE_INSTALL=true bash scripts/prepare_build.sh
- cp config/gitlab.yml.example config/gitlab.yml
- run_timed_command "bundle exec rake db:drop db:create db:structure:load db:migrate db:seed_fu" - run_timed_command "bundle exec rake db:drop db:create db:structure:load db:migrate db:seed_fu"
- git checkout -f $CI_COMMIT_SHA - git checkout -f $CI_COMMIT_SHA
- run_timed_command "bundle install ${BUNDLE_INSTALL_FLAGS}" - SETUP_DB=false USE_BUNDLE_INSTALL=true bash scripts/prepare_build.sh
- run_timed_command "bundle exec rake db:migrate" - run_timed_command "bundle exec rake db:migrate"
db:rollback: db:rollback:
......
# frozen_string_literal: true # frozen_string_literal: true
Gitlab::Database::ConnectionTimer.configure do |config| Gitlab::Database::ConnectionTimer.configure do |config|
config.interval = Rails.application.config_for(:database)[:force_reconnect_interval] configuration_hash = ActiveRecord::Base.configurations.find_db_config(Rails.env).configuration_hash
config.interval = configuration_hash[:force_reconnect_interval]
end end
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.prepend(Gitlab::Database::PostgresqlAdapter::ForceDisconnectableMixin) ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.prepend(Gitlab::Database::PostgresqlAdapter::ForceDisconnectableMixin)
...@@ -18,7 +18,7 @@ module Backup ...@@ -18,7 +18,7 @@ module Backup
def initialize(progress, filename: nil) def initialize(progress, filename: nil)
@progress = progress @progress = progress
@config = YAML.load_file(File.join(Rails.root, 'config', 'database.yml'))[Rails.env] @config = ActiveRecord::Base.configurations.find_db_config(Rails.env).configuration_hash
@db_file_name = filename || File.join(Gitlab.config.backup.path, 'db', 'database.sql.gz') @db_file_name = filename || File.join(Gitlab.config.backup.path, 'db', 'database.sql.gz')
end end
...@@ -30,9 +30,9 @@ module Backup ...@@ -30,9 +30,9 @@ module Backup
compress_rd.close compress_rd.close
dump_pid = dump_pid =
case config["adapter"] case config[:adapter]
when "postgresql" then when "postgresql" then
progress.print "Dumping PostgreSQL database #{config['database']} ... " progress.print "Dumping PostgreSQL database #{database} ... "
pg_env pg_env
pgsql_args = ["--clean"] # Pass '--clean' to include 'DROP TABLE' statements in the DB dump. pgsql_args = ["--clean"] # Pass '--clean' to include 'DROP TABLE' statements in the DB dump.
pgsql_args << '--if-exists' pgsql_args << '--if-exists'
...@@ -47,7 +47,7 @@ module Backup ...@@ -47,7 +47,7 @@ module Backup
end end
end end
Process.spawn('pg_dump', *pgsql_args, config['database'], out: compress_wr) Process.spawn('pg_dump', *pgsql_args, database, out: compress_wr)
end end
compress_wr.close compress_wr.close
...@@ -68,9 +68,9 @@ module Backup ...@@ -68,9 +68,9 @@ module Backup
decompress_wr.close decompress_wr.close
status, errors = status, errors =
case config["adapter"] case config[:adapter]
when "postgresql" then when "postgresql" then
progress.print "Restoring PostgreSQL database #{config['database']} ... " progress.print "Restoring PostgreSQL database #{database} ... "
pg_env pg_env
execute_and_track_errors(pg_restore_cmd, decompress_rd) execute_and_track_errors(pg_restore_cmd, decompress_rd)
end end
...@@ -93,6 +93,10 @@ module Backup ...@@ -93,6 +93,10 @@ module Backup
protected protected
def database
@config[:database]
end
def ignore_error?(line) def ignore_error?(line)
IGNORED_ERRORS_REGEXP.match?(line) IGNORED_ERRORS_REGEXP.match?(line)
end end
...@@ -128,17 +132,17 @@ module Backup ...@@ -128,17 +132,17 @@ module Backup
def pg_env def pg_env
args = { args = {
'username' => 'PGUSER', username: 'PGUSER',
'host' => 'PGHOST', host: 'PGHOST',
'port' => 'PGPORT', port: 'PGPORT',
'password' => 'PGPASSWORD', password: 'PGPASSWORD',
# SSL # SSL
'sslmode' => 'PGSSLMODE', sslmode: 'PGSSLMODE',
'sslkey' => 'PGSSLKEY', sslkey: 'PGSSLKEY',
'sslcert' => 'PGSSLCERT', sslcert: 'PGSSLCERT',
'sslrootcert' => 'PGSSLROOTCERT', sslrootcert: 'PGSSLROOTCERT',
'sslcrl' => 'PGSSLCRL', sslcrl: 'PGSSLCRL',
'sslcompression' => 'PGSSLCOMPRESSION' sslcompression: 'PGSSLCOMPRESSION'
} }
args.each do |opt, arg| args.each do |opt, arg|
# This enables the use of different PostgreSQL settings in # This enables the use of different PostgreSQL settings in
...@@ -161,7 +165,7 @@ module Backup ...@@ -161,7 +165,7 @@ module Backup
private private
def pg_restore_cmd def pg_restore_cmd
['psql', config['database']] ['psql', database]
end end
end end
end end
...@@ -72,8 +72,19 @@ module Gitlab ...@@ -72,8 +72,19 @@ module Gitlab
Gitlab::Application.config.database_configuration[Rails.env].include?(database_name.to_s) Gitlab::Application.config.database_configuration[Rails.env].include?(database_name.to_s)
end end
def self.main_database?(name)
# The database is `main` if it is a first entry in `database.yml`
# Rails internally names them `primary` to avoid confusion
# with broad `primary` usage we use `main` instead
#
# TODO: The explicit `== 'main'` is needed in a transition period till
# the `database.yml` is not migrated into `main:` syntax
# https://gitlab.com/gitlab-org/gitlab/-/merge_requests/65243
ActiveRecord::Base.configurations.primary?(name.to_s) || name.to_s == 'main'
end
def self.ci_database?(name) def self.ci_database?(name)
name == CI_DATABASE_NAME name.to_s == CI_DATABASE_NAME
end end
def self.username def self.username
......
...@@ -14,7 +14,7 @@ RSpec.describe Gitlab::Database::PostgresqlAdapter::ForceDisconnectableMixin do ...@@ -14,7 +14,7 @@ RSpec.describe Gitlab::Database::PostgresqlAdapter::ForceDisconnectableMixin do
end end
end end
let(:config) { Rails.application.config_for(:database).merge(pool: 1) } let(:config) { ActiveRecord::Base.configurations.find_db_config(Rails.env).configuration_hash.merge(pool: 1) }
let(:pool) { model.establish_connection(config) } let(:pool) { model.establish_connection(config) }
it 'calls the force disconnect callback on checkin' do it 'calls the force disconnect callback on checkin' do
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Gitlab::Database::PostgresqlAdapter::TypeMapCache do RSpec.describe Gitlab::Database::PostgresqlAdapter::TypeMapCache do
let(:db_config) { ActiveRecord::Base.configurations.configs_for(env_name: 'test', name: 'primary').configuration_hash } let(:db_config) { ActiveRecord::Base.configurations.find_db_config(Rails.env).configuration_hash }
let(:adapter_class) { ActiveRecord::ConnectionAdapters::PostgreSQLAdapter } let(:adapter_class) { ActiveRecord::ConnectionAdapters::PostgreSQLAdapter }
before do before do
......
...@@ -37,8 +37,10 @@ RSpec.describe Gitlab::Database::WithLockRetriesOutsideTransaction do ...@@ -37,8 +37,10 @@ RSpec.describe Gitlab::Database::WithLockRetriesOutsideTransaction do
context 'when lock retry is enabled' do context 'when lock retry is enabled' do
let(:lock_fiber) do let(:lock_fiber) do
Fiber.new do Fiber.new do
configuration = ActiveRecordSecond.configurations.find_db_config(Rails.env).configuration_hash
# Initiating a second DB connection for the lock # Initiating a second DB connection for the lock
conn = ActiveRecordSecond.establish_connection(Rails.configuration.database_configuration[Rails.env]).connection conn = ActiveRecordSecond.establish_connection(configuration).connection
conn.transaction do conn.transaction do
conn.execute("LOCK TABLE #{Project.table_name} in exclusive mode") conn.execute("LOCK TABLE #{Project.table_name} in exclusive mode")
......
...@@ -37,8 +37,10 @@ RSpec.describe Gitlab::Database::WithLockRetries do ...@@ -37,8 +37,10 @@ RSpec.describe Gitlab::Database::WithLockRetries do
context 'when lock retry is enabled' do context 'when lock retry is enabled' do
let(:lock_fiber) do let(:lock_fiber) do
Fiber.new do Fiber.new do
configuration = ActiveRecordSecond.configurations.find_db_config(Rails.env).configuration_hash
# Initiating a second DB connection for the lock # Initiating a second DB connection for the lock
conn = ActiveRecordSecond.establish_connection(Rails.configuration.database_configuration[Rails.env]).connection conn = ActiveRecordSecond.establish_connection(configuration).connection
conn.transaction do conn.transaction do
conn.execute("LOCK TABLE #{Project.table_name} in exclusive mode") conn.execute("LOCK TABLE #{Project.table_name} in exclusive mode")
......
...@@ -80,6 +80,40 @@ RSpec.describe Gitlab::Database do ...@@ -80,6 +80,40 @@ RSpec.describe Gitlab::Database do
end end
end end
describe '.main_database?' do
using RSpec::Parameterized::TableSyntax
where(:database_name, :result) do
:main | true
'main' | true
:ci | false
'ci' | false
:archive | false
'archive' | false
end
with_them do
it { expect(described_class.main_database?(database_name)).to eq(result) }
end
end
describe '.ci_database?' do
using RSpec::Parameterized::TableSyntax
where(:database_name, :result) do
:main | false
'main' | false
:ci | true
'ci' | true
:archive | false
'archive' | false
end
with_them do
it { expect(described_class.ci_database?(database_name)).to eq(result) }
end
end
describe '.adapter_name' do describe '.adapter_name' do
it 'returns the name of the adapter' do it 'returns the name of the adapter' do
expect(described_class.adapter_name).to be_an_instance_of(String) expect(described_class.adapter_name).to be_an_instance_of(String)
......
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