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:
- 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 "bundle update google-protobuf nokogiri grpc mimemagic bootsnap"
- run_timed_command "bundle install ${BUNDLE_INSTALL_FLAGS}"
- cp config/gitlab.yml.example config/gitlab.yml
- SETUP_DB=false USE_BUNDLE_INSTALL=true bash scripts/prepare_build.sh
- run_timed_command "bundle exec rake db:drop db:create db:structure:load db:migrate db:seed_fu"
- 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"
db:rollback:
......
# frozen_string_literal: true
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
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.prepend(Gitlab::Database::PostgresqlAdapter::ForceDisconnectableMixin)
......@@ -18,7 +18,7 @@ module Backup
def initialize(progress, filename: nil)
@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')
end
......@@ -30,9 +30,9 @@ module Backup
compress_rd.close
dump_pid =
case config["adapter"]
case config[:adapter]
when "postgresql" then
progress.print "Dumping PostgreSQL database #{config['database']} ... "
progress.print "Dumping PostgreSQL database #{database} ... "
pg_env
pgsql_args = ["--clean"] # Pass '--clean' to include 'DROP TABLE' statements in the DB dump.
pgsql_args << '--if-exists'
......@@ -47,7 +47,7 @@ module Backup
end
end
Process.spawn('pg_dump', *pgsql_args, config['database'], out: compress_wr)
Process.spawn('pg_dump', *pgsql_args, database, out: compress_wr)
end
compress_wr.close
......@@ -68,9 +68,9 @@ module Backup
decompress_wr.close
status, errors =
case config["adapter"]
case config[:adapter]
when "postgresql" then
progress.print "Restoring PostgreSQL database #{config['database']} ... "
progress.print "Restoring PostgreSQL database #{database} ... "
pg_env
execute_and_track_errors(pg_restore_cmd, decompress_rd)
end
......@@ -93,6 +93,10 @@ module Backup
protected
def database
@config[:database]
end
def ignore_error?(line)
IGNORED_ERRORS_REGEXP.match?(line)
end
......@@ -128,17 +132,17 @@ module Backup
def pg_env
args = {
'username' => 'PGUSER',
'host' => 'PGHOST',
'port' => 'PGPORT',
'password' => 'PGPASSWORD',
username: 'PGUSER',
host: 'PGHOST',
port: 'PGPORT',
password: 'PGPASSWORD',
# SSL
'sslmode' => 'PGSSLMODE',
'sslkey' => 'PGSSLKEY',
'sslcert' => 'PGSSLCERT',
'sslrootcert' => 'PGSSLROOTCERT',
'sslcrl' => 'PGSSLCRL',
'sslcompression' => 'PGSSLCOMPRESSION'
sslmode: 'PGSSLMODE',
sslkey: 'PGSSLKEY',
sslcert: 'PGSSLCERT',
sslrootcert: 'PGSSLROOTCERT',
sslcrl: 'PGSSLCRL',
sslcompression: 'PGSSLCOMPRESSION'
}
args.each do |opt, arg|
# This enables the use of different PostgreSQL settings in
......@@ -161,7 +165,7 @@ module Backup
private
def pg_restore_cmd
['psql', config['database']]
['psql', database]
end
end
end
......@@ -72,8 +72,19 @@ module Gitlab
Gitlab::Application.config.database_configuration[Rails.env].include?(database_name.to_s)
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)
name == CI_DATABASE_NAME
name.to_s == CI_DATABASE_NAME
end
def self.username
......
......@@ -14,7 +14,7 @@ RSpec.describe Gitlab::Database::PostgresqlAdapter::ForceDisconnectableMixin do
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) }
it 'calls the force disconnect callback on checkin' do
......
......@@ -3,7 +3,7 @@
require 'spec_helper'
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 }
before do
......
......@@ -37,8 +37,10 @@ RSpec.describe Gitlab::Database::WithLockRetriesOutsideTransaction do
context 'when lock retry is enabled' do
let(:lock_fiber) do
Fiber.new do
configuration = ActiveRecordSecond.configurations.find_db_config(Rails.env).configuration_hash
# 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.execute("LOCK TABLE #{Project.table_name} in exclusive mode")
......
......@@ -37,8 +37,10 @@ RSpec.describe Gitlab::Database::WithLockRetries do
context 'when lock retry is enabled' do
let(:lock_fiber) do
Fiber.new do
configuration = ActiveRecordSecond.configurations.find_db_config(Rails.env).configuration_hash
# 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.execute("LOCK TABLE #{Project.table_name} in exclusive mode")
......
......@@ -80,6 +80,40 @@ RSpec.describe Gitlab::Database do
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
it 'returns the name of the adapter' do
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