debian-mirror-gitlab/lib/gitlab/database.rb

344 lines
12 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2015-10-24 18:46:33 +05:30
module Gitlab
module Database
2020-07-28 23:09:34 +05:30
# Minimum PostgreSQL version requirement per documentation:
# https://docs.gitlab.com/ee/install/requirements.html#postgresql-requirements
MINIMUM_POSTGRES_VERSION = 11
2016-06-22 15:30:34 +05:30
# https://www.postgresql.org/docs/9.2/static/datatype-numeric.html
MAX_INT_VALUE = 2147483647
2020-10-24 23:57:45 +05:30
MIN_INT_VALUE = -2147483648
2019-10-12 21:52:04 +05:30
2018-03-17 18:26:18 +05:30
# The max value between MySQL's TIMESTAMP and PostgreSQL's timestampz:
# https://www.postgresql.org/docs/9.1/static/datatype-datetime.html
# https://dev.mysql.com/doc/refman/5.7/en/datetime.html
2019-10-12 21:52:04 +05:30
# FIXME: this should just be the max value of timestampz
2018-03-17 18:26:18 +05:30
MAX_TIMESTAMP_VALUE = Time.at((1 << 31) - 1).freeze
2016-06-22 15:30:34 +05:30
2019-09-04 21:01:54 +05:30
# The maximum number of characters for text fields, to avoid DoS attacks via parsing huge text fields
2019-12-04 20:38:33 +05:30
# https://gitlab.com/gitlab-org/gitlab-foss/issues/61974
2019-09-04 21:01:54 +05:30
MAX_TEXT_SIZE_LIMIT = 1_000_000
2019-09-30 21:07:59 +05:30
# Minimum schema version from which migrations are supported
# Migrations before this version may have been removed
MIN_SCHEMA_VERSION = 20190506135400
MIN_SCHEMA_GITLAB_VERSION = '11.11.0'
2020-07-28 23:09:34 +05:30
# Schema we store dynamically managed partitions in (e.g. for time partitioning)
DYNAMIC_PARTITIONS_SCHEMA = :gitlab_partitions_dynamic
# Schema we store static partitions in (e.g. for hash partitioning)
STATIC_PARTITIONS_SCHEMA = :gitlab_partitions_static
# This is an extensive list of postgres schemas owned by GitLab
# It does not include the default public schema
EXTRA_SCHEMAS = [DYNAMIC_PARTITIONS_SCHEMA, STATIC_PARTITIONS_SCHEMA].freeze
2017-08-17 22:00:37 +05:30
def self.config
ActiveRecord::Base.configurations[Rails.env]
end
2018-03-17 18:26:18 +05:30
def self.username
config['username'] || ENV['USER']
end
def self.database_name
config['database']
end
2016-04-02 18:10:28 +05:30
def self.adapter_name
2017-08-17 22:00:37 +05:30
config['adapter']
2016-04-02 18:10:28 +05:30
end
2019-07-07 11:18:12 +05:30
def self.human_adapter_name
2019-10-12 21:52:04 +05:30
if postgresql?
'PostgreSQL'
else
'Unknown'
end
2015-10-24 18:46:33 +05:30
end
2019-10-12 21:52:04 +05:30
# @deprecated
2015-10-24 18:46:33 +05:30
def self.postgresql?
2020-10-24 23:57:45 +05:30
adapter_name.casecmp('postgresql') == 0
2016-04-02 18:10:28 +05:30
end
2018-03-17 18:26:18 +05:30
def self.read_only?
false
end
def self.read_write?
!self.read_only?
end
2019-02-15 15:39:39 +05:30
# Check whether the underlying database is in read-only mode
2018-11-08 19:23:39 +05:30
def self.db_read_only?
2019-10-12 21:52:04 +05:30
pg_is_in_recovery =
ActiveRecord::Base
.connection
.execute('SELECT pg_is_in_recovery()')
.first
.fetch('pg_is_in_recovery')
2019-02-15 15:39:39 +05:30
2019-10-12 21:52:04 +05:30
Gitlab::Utils.to_boolean(pg_is_in_recovery)
2018-11-08 19:23:39 +05:30
end
def self.db_read_write?
!self.db_read_only?
end
2016-04-02 18:10:28 +05:30
def self.version
2018-10-15 14:42:47 +05:30
@version ||= database_version.match(/\A(?:PostgreSQL |)([^\s]+).*\z/)[1]
2015-10-24 18:46:33 +05:30
end
2015-12-23 02:04:40 +05:30
2020-07-28 23:09:34 +05:30
def self.postgresql_minimum_supported_version?
version.to_f >= MINIMUM_POSTGRES_VERSION
2018-03-17 18:26:18 +05:30
end
2020-07-28 23:09:34 +05:30
def self.check_postgres_version_and_print_warning
return if Gitlab::Database.postgresql_minimum_supported_version?
return if Gitlab::Runtime.rails_runner?
Kernel.warn ERB.new(Rainbow.new.wrap(<<~EOS).red).result
******************************************************************************
You are using PostgreSQL <%= Gitlab::Database.version %>, but PostgreSQL >= <%= Gitlab::Database::MINIMUM_POSTGRES_VERSION %>
is required for this version of GitLab.
<% 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/master/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
2020-01-01 13:55:28 +05:30
end
def self.nulls_last_order(field, direction = 'ASC')
2019-10-12 21:52:04 +05:30
Arel.sql("#{field} #{direction} NULLS LAST")
end
2017-08-17 22:00:37 +05:30
def self.nulls_first_order(field, direction = 'ASC')
2019-10-12 21:52:04 +05:30
Arel.sql("#{field} #{direction} NULLS FIRST")
2017-08-17 22:00:37 +05:30
end
def self.random
2019-10-12 21:52:04 +05:30
"RANDOM()"
end
2017-08-17 22:00:37 +05:30
def self.true_value
2019-10-12 21:52:04 +05:30
"'t'"
2015-12-23 02:04:40 +05:30
end
2017-08-17 22:00:37 +05:30
def self.false_value
2019-10-12 21:52:04 +05:30
"'f'"
2015-12-23 02:04:40 +05:30
end
2016-04-02 18:10:28 +05:30
2017-08-17 22:00:37 +05:30
def self.with_connection_pool(pool_size)
pool = create_connection_pool(pool_size)
begin
yield(pool)
ensure
pool.disconnect!
end
end
2018-03-17 18:26:18 +05:30
# Bulk inserts a number of rows into a table, optionally returning their
# IDs.
#
# table - The name of the table to insert the rows into.
# rows - An Array of Hash instances, each mapping the columns to their
# values.
# return_ids - When set to true the return value will be an Array of IDs of
2019-10-12 21:52:04 +05:30
# the inserted rows
2018-03-17 18:26:18 +05:30
# disable_quote - A key or an Array of keys to exclude from quoting (You
# become responsible for protection from SQL injection for
# these keys!)
2020-01-01 13:55:28 +05:30
# on_conflict - Defines an upsert. Values can be: :disabled (default) or
# :do_nothing
def self.bulk_insert(table, rows, return_ids: false, disable_quote: [], on_conflict: nil)
2017-09-10 17:25:29 +05:30
return if rows.empty?
keys = rows.first.keys
columns = keys.map { |key| connection.quote_column_name(key) }
2018-03-17 18:26:18 +05:30
disable_quote = Array(disable_quote).to_set
2017-09-10 17:25:29 +05:30
tuples = rows.map do |row|
2018-03-17 18:26:18 +05:30
keys.map do |k|
disable_quote.include?(k) ? row[k] : connection.quote(row[k])
end
2017-09-10 17:25:29 +05:30
end
2018-03-17 18:26:18 +05:30
sql = <<-EOF
2017-09-10 17:25:29 +05:30
INSERT INTO #{table} (#{columns.join(', ')})
VALUES #{tuples.map { |tuple| "(#{tuple.join(', ')})" }.join(', ')}
EOF
2018-03-17 18:26:18 +05:30
2020-07-28 23:09:34 +05:30
sql = "#{sql} ON CONFLICT DO NOTHING" if on_conflict == :do_nothing
2018-03-17 18:26:18 +05:30
2020-01-01 13:55:28 +05:30
sql = "#{sql} RETURNING id" if return_ids
2018-03-17 18:26:18 +05:30
result = connection.execute(sql)
if return_ids
result.values.map { |tuple| tuple[0].to_i }
else
[]
end
end
def self.sanitize_timestamp(timestamp)
MAX_TIMESTAMP_VALUE > timestamp ? timestamp : MAX_TIMESTAMP_VALUE.dup
2017-09-10 17:25:29 +05:30
end
2017-08-17 22:00:37 +05:30
# pool_size - The size of the DB pool.
# host - An optional host name to use instead of the default one.
2019-12-04 20:38:33 +05:30
def self.create_connection_pool(pool_size, host = nil, port = nil)
2017-08-17 22:00:37 +05:30
env = Rails.env
2020-03-13 15:44:24 +05:30
original_config = ActiveRecord::Base.configurations.to_h
2017-08-17 22:00:37 +05:30
env_config = original_config[env].merge('pool' => pool_size)
env_config['host'] = host if host
2019-12-04 20:38:33 +05:30
env_config['port'] = port if port
2017-08-17 22:00:37 +05:30
2020-03-13 15:44:24 +05:30
config = ActiveRecord::DatabaseConfigurations.new(
original_config.merge(env => env_config)
)
2017-08-17 22:00:37 +05:30
spec =
ActiveRecord::
ConnectionAdapters::
ConnectionSpecification::Resolver.new(config).spec(env.to_sym)
ActiveRecord::ConnectionAdapters::ConnectionPool.new(spec)
end
2016-04-02 18:10:28 +05:30
def self.connection
ActiveRecord::Base.connection
end
2019-09-30 21:07:59 +05:30
private_class_method :connection
2016-04-02 18:10:28 +05:30
2018-03-27 19:54:05 +05:30
def self.cached_column_exists?(table_name, column_name)
connection.schema_cache.columns_hash(table_name).has_key?(column_name.to_s)
end
def self.cached_table_exists?(table_name)
2020-03-13 15:44:24 +05:30
exists? && connection.schema_cache.data_source_exists?(table_name)
2018-03-27 19:54:05 +05:30
end
2016-04-02 18:10:28 +05:30
def self.database_version
row = connection.execute("SELECT VERSION()").first
2019-10-12 21:52:04 +05:30
row['version']
2016-04-02 18:10:28 +05:30
end
2016-09-13 17:45:13 +05:30
2020-03-13 15:44:24 +05:30
def self.exists?
connection
true
rescue
false
end
2021-01-03 14:25:43 +05:30
def self.system_id
row = connection.execute('SELECT system_identifier FROM pg_control_system()').first
row['system_identifier']
end
def self.get_write_location(ar_connection)
row = ar_connection
.select_all("SELECT pg_current_wal_insert_lsn()::text AS location")
.first
row['location'] if row
end
2016-09-13 17:45:13 +05:30
private_class_method :database_version
2018-12-05 23:21:45 +05:30
def self.add_post_migrate_path_to_rails(force: false)
return if ENV['SKIP_POST_DEPLOYMENT_MIGRATIONS'] && !force
Rails.application.config.paths['db'].each do |db_path|
path = Rails.root.join(db_path, 'post_migrate').to_s
unless Rails.application.config.paths['db/migrate'].include? path
Rails.application.config.paths['db/migrate'] << path
# Rails memoizes migrations at certain points where it won't read the above
# path just yet. As such we must also update the following list of paths.
ActiveRecord::Migrator.migrations_paths << path
end
end
end
2019-09-30 21:07:59 +05:30
# inside_transaction? will return true if the caller is running within a transaction. Handles special cases
# when running inside a test environment, where tests may be wrapped in transactions
def self.inside_transaction?
if Rails.env.test?
ActiveRecord::Base.connection.open_transactions > open_transactions_baseline
else
ActiveRecord::Base.connection.open_transactions > 0
end
end
# These methods that access @open_transactions_baseline are not thread-safe.
# These are fine though because we only call these in RSpec's main thread. If we decide to run
# specs multi-threaded, we would need to use something like ThreadGroup to keep track of this value
def self.set_open_transactions_baseline
@open_transactions_baseline = ActiveRecord::Base.connection.open_transactions
end
def self.reset_open_transactions_baseline
@open_transactions_baseline = 0
end
def self.open_transactions_baseline
@open_transactions_baseline ||= 0
end
private_class_method :open_transactions_baseline
# Monkeypatch rails with upgraded database observability
def self.install_monkey_patches
ActiveRecord::Base.prepend(ActiveRecordBaseTransactionMetrics)
end
# observe_transaction_duration is called from ActiveRecordBaseTransactionMetrics.transaction and used to
# record transaction durations.
def self.observe_transaction_duration(duration_seconds)
2020-10-24 23:57:45 +05:30
if current_transaction = ::Gitlab::Metrics::Transaction.current
current_transaction.observe(:gitlab_database_transaction_seconds, duration_seconds) do
docstring "Time spent in database transactions, in seconds"
end
end
2019-09-30 21:07:59 +05:30
rescue Prometheus::Client::LabelSetValidator::LabelSetError => err
# Ensure that errors in recording these metrics don't affect the operation of the application
2020-11-24 15:15:51 +05:30
Gitlab::AppLogger.error("Unable to observe database transaction duration: #{err}")
2019-09-30 21:07:59 +05:30
end
# MonkeyPatch for ActiveRecord::Base for adding observability
module ActiveRecordBaseTransactionMetrics
# A monkeypatch over ActiveRecord::Base.transaction.
# It provides observability into transactional methods.
def transaction(options = {}, &block)
start_time = Gitlab::Metrics::System.monotonic_time
super(options, &block)
ensure
Gitlab::Database.observe_transaction_duration(Gitlab::Metrics::System.monotonic_time - start_time)
end
end
2015-10-24 18:46:33 +05:30
end
end
2019-12-04 20:38:33 +05:30
Gitlab::Database.prepend_if_ee('EE::Gitlab::Database')