Commit 257459cd authored by Kamil Trzciński's avatar Kamil Trzciński Committed by Thong Kuah

Add `main_database?` helper

This ensures that we use `main:`
as an indicator for a main unsharded
database.

This adds a helper method, and additionally
updated `database.yml.postgresql` to properly
support that for `rspec`.
parent 82d57c8d
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
# PRODUCTION # PRODUCTION
# #
production: production:
main:
adapter: postgresql adapter: postgresql
encoding: unicode encoding: unicode
database: gitlabhq_production database: gitlabhq_production
...@@ -22,6 +23,7 @@ production: ...@@ -22,6 +23,7 @@ production:
# Development specific # Development specific
# #
development: development:
main:
adapter: postgresql adapter: postgresql
encoding: unicode encoding: unicode
database: gitlabhq_development database: gitlabhq_development
...@@ -35,6 +37,7 @@ development: ...@@ -35,6 +37,7 @@ development:
# Staging specific # Staging specific
# #
staging: staging:
main:
adapter: postgresql adapter: postgresql
encoding: unicode encoding: unicode
database: gitlabhq_staging database: gitlabhq_staging
...@@ -46,6 +49,7 @@ staging: ...@@ -46,6 +49,7 @@ staging:
# re-generated from your development database when you run "rake". # re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production. # Do not set this db to the same as development or production.
test: &test test: &test
main:
adapter: postgresql adapter: postgresql
encoding: unicode encoding: unicode
database: gitlabhq_test database: gitlabhq_test
......
# 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,15 @@ module Gitlab ...@@ -72,8 +72,15 @@ 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
ActiveRecord::Base.configurations.primary?(name.to_s)
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.configs_for(env_name: 'test', name: 'main').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