2018-10-22 03:00:50 -04:00
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2015-10-07 06:30:10 -04:00
|
|
|
|
module Gitlab
|
|
|
|
|
module Database
|
2021-06-28 17:10:13 -04:00
|
|
|
|
CI_DATABASE_NAME = 'ci'
|
|
|
|
|
|
2021-05-07 11:10:39 -04:00
|
|
|
|
# This constant is used when renaming tables concurrently.
|
|
|
|
|
# If you plan to rename a table using the `rename_table_safely` method, add your table here one milestone before the rename.
|
|
|
|
|
# Example:
|
|
|
|
|
# TABLES_TO_BE_RENAMED = {
|
|
|
|
|
# 'old_name' => 'new_name'
|
|
|
|
|
# }.freeze
|
2021-07-07 11:07:24 -04:00
|
|
|
|
TABLES_TO_BE_RENAMED = {}.freeze
|
2021-05-07 11:10:39 -04:00
|
|
|
|
|
2020-07-14 17:09:03 -04:00
|
|
|
|
# Minimum PostgreSQL version requirement per documentation:
|
|
|
|
|
# https://docs.gitlab.com/ee/install/requirements.html#postgresql-requirements
|
2021-06-14 14:10:28 -04:00
|
|
|
|
MINIMUM_POSTGRES_VERSION = 12
|
2020-07-06 23:09:32 -04:00
|
|
|
|
|
2016-06-18 13:55:45 -04:00
|
|
|
|
# https://www.postgresql.org/docs/9.2/static/datatype-numeric.html
|
|
|
|
|
MAX_INT_VALUE = 2147483647
|
2020-08-19 08:10:17 -04:00
|
|
|
|
MIN_INT_VALUE = -2147483648
|
2019-06-13 09:12:28 -04:00
|
|
|
|
|
2017-10-30 19:21:56 -04:00
|
|
|
|
# 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-06-13 09:12:28 -04:00
|
|
|
|
# FIXME: this should just be the max value of timestampz
|
2017-10-30 19:21:56 -04:00
|
|
|
|
MAX_TIMESTAMP_VALUE = Time.at((1 << 31) - 1).freeze
|
2016-06-18 13:55:45 -04:00
|
|
|
|
|
2019-07-17 05:54:40 -04:00
|
|
|
|
# The maximum number of characters for text fields, to avoid DoS attacks via parsing huge text fields
|
2019-09-18 10:02:45 -04:00
|
|
|
|
# https://gitlab.com/gitlab-org/gitlab-foss/issues/61974
|
2019-07-17 05:54:40 -04:00
|
|
|
|
MAX_TEXT_SIZE_LIMIT = 1_000_000
|
|
|
|
|
|
2019-06-25 13:28:18 -04:00
|
|
|
|
# Minimum schema version from which migrations are supported
|
2019-06-20 02:35:56 -04:00
|
|
|
|
# Migrations before this version may have been removed
|
|
|
|
|
MIN_SCHEMA_VERSION = 20190506135400
|
2019-06-20 10:56:46 -04:00
|
|
|
|
MIN_SCHEMA_GITLAB_VERSION = '11.11.0'
|
2019-06-20 02:35:56 -04:00
|
|
|
|
|
2020-06-26 11:08:45 -04:00
|
|
|
|
# Schema we store dynamically managed partitions in (e.g. for time partitioning)
|
2020-06-24 20:09:26 -04:00
|
|
|
|
DYNAMIC_PARTITIONS_SCHEMA = :gitlab_partitions_dynamic
|
|
|
|
|
|
2020-06-26 11:08:45 -04:00
|
|
|
|
# Schema we store static partitions in (e.g. for hash partitioning)
|
|
|
|
|
STATIC_PARTITIONS_SCHEMA = :gitlab_partitions_static
|
|
|
|
|
|
2020-06-24 20:09:26 -04:00
|
|
|
|
# This is an extensive list of postgres schemas owned by GitLab
|
|
|
|
|
# It does not include the default public schema
|
2020-06-26 11:08:45 -04:00
|
|
|
|
EXTRA_SCHEMAS = [DYNAMIC_PARTITIONS_SCHEMA, STATIC_PARTITIONS_SCHEMA].freeze
|
2020-06-24 20:09:26 -04:00
|
|
|
|
|
2021-05-11 02:10:29 -04:00
|
|
|
|
DEFAULT_POOL_HEADROOM = 10
|
|
|
|
|
|
|
|
|
|
# We configure the database connection pool size automatically based on the
|
|
|
|
|
# configured concurrency. We also add some headroom, to make sure we don't run
|
|
|
|
|
# out of connections when more threads besides the 'user-facing' ones are
|
|
|
|
|
# running.
|
|
|
|
|
#
|
|
|
|
|
# Read more about this in doc/development/database/client_side_connection_pool.md
|
|
|
|
|
def self.default_pool_size
|
|
|
|
|
headroom = (ENV["DB_POOL_HEADROOM"].presence || DEFAULT_POOL_HEADROOM).to_i
|
|
|
|
|
|
|
|
|
|
Gitlab::Runtime.max_threads + headroom
|
|
|
|
|
end
|
|
|
|
|
|
2017-03-17 09:16:47 -04:00
|
|
|
|
def self.config
|
2021-06-01 20:09:56 -04:00
|
|
|
|
default_config_hash = ActiveRecord::Base.configurations.find_db_config(Rails.env)&.configuration_hash || {}
|
2021-05-11 02:10:29 -04:00
|
|
|
|
|
|
|
|
|
default_config_hash.with_indifferent_access.tap do |hash|
|
|
|
|
|
# Match config/initializers/database_config.rb
|
|
|
|
|
hash[:pool] ||= default_pool_size
|
|
|
|
|
end
|
2017-03-17 09:16:47 -04:00
|
|
|
|
end
|
|
|
|
|
|
2021-06-28 17:10:13 -04:00
|
|
|
|
def self.has_config?(database_name)
|
|
|
|
|
Gitlab::Application.config.database_configuration[Rails.env].include?(database_name.to_s)
|
|
|
|
|
end
|
|
|
|
|
|
2021-07-09 05:09:53 -04:00
|
|
|
|
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
|
|
|
|
|
|
2021-06-28 17:10:13 -04:00
|
|
|
|
def self.ci_database?(name)
|
2021-07-09 05:09:53 -04:00
|
|
|
|
name.to_s == CI_DATABASE_NAME
|
2021-06-28 17:10:13 -04:00
|
|
|
|
end
|
|
|
|
|
|
2017-08-18 08:26:23 -04:00
|
|
|
|
def self.username
|
|
|
|
|
config['username'] || ENV['USER']
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.database_name
|
|
|
|
|
config['database']
|
|
|
|
|
end
|
|
|
|
|
|
2016-02-01 21:29:37 -05:00
|
|
|
|
def self.adapter_name
|
2017-03-17 09:16:47 -04:00
|
|
|
|
config['adapter']
|
2016-02-01 21:29:37 -05:00
|
|
|
|
end
|
|
|
|
|
|
2019-03-21 16:05:01 -04:00
|
|
|
|
def self.human_adapter_name
|
2019-06-13 09:12:28 -04:00
|
|
|
|
if postgresql?
|
|
|
|
|
'PostgreSQL'
|
|
|
|
|
else
|
|
|
|
|
'Unknown'
|
|
|
|
|
end
|
2015-10-07 06:30:10 -04:00
|
|
|
|
end
|
|
|
|
|
|
2021-05-27 02:10:47 -04:00
|
|
|
|
# Disables prepared statements for the current database connection.
|
|
|
|
|
def self.disable_prepared_statements
|
|
|
|
|
ActiveRecord::Base.establish_connection(config.merge(prepared_statements: false))
|
|
|
|
|
end
|
|
|
|
|
|
2019-07-24 09:59:55 -04:00
|
|
|
|
# @deprecated
|
2015-10-07 06:30:10 -04:00
|
|
|
|
def self.postgresql?
|
2020-08-05 11:09:59 -04:00
|
|
|
|
adapter_name.casecmp('postgresql') == 0
|
2016-02-01 21:29:37 -05:00
|
|
|
|
end
|
|
|
|
|
|
2017-09-19 03:44:58 -04:00
|
|
|
|
def self.read_only?
|
|
|
|
|
false
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.read_write?
|
|
|
|
|
!self.read_only?
|
|
|
|
|
end
|
|
|
|
|
|
2018-11-26 14:14:05 -05:00
|
|
|
|
# Check whether the underlying database is in read-only mode
|
2018-07-06 13:07:49 -04:00
|
|
|
|
def self.db_read_only?
|
2019-06-13 09:12:28 -04:00
|
|
|
|
pg_is_in_recovery =
|
|
|
|
|
ActiveRecord::Base
|
|
|
|
|
.connection
|
|
|
|
|
.execute('SELECT pg_is_in_recovery()')
|
|
|
|
|
.first
|
|
|
|
|
.fetch('pg_is_in_recovery')
|
2018-11-26 14:14:05 -05:00
|
|
|
|
|
2019-06-13 09:12:28 -04:00
|
|
|
|
Gitlab::Utils.to_boolean(pg_is_in_recovery)
|
2018-07-06 13:07:49 -04:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.db_read_write?
|
|
|
|
|
!self.db_read_only?
|
|
|
|
|
end
|
|
|
|
|
|
2016-02-01 21:29:37 -05:00
|
|
|
|
def self.version
|
2018-05-17 15:20:15 -04:00
|
|
|
|
@version ||= database_version.match(/\A(?:PostgreSQL |)([^\s]+).*\z/)[1]
|
2015-10-07 06:30:10 -04:00
|
|
|
|
end
|
2015-12-09 10:31:42 -05:00
|
|
|
|
|
2019-03-14 11:25:05 -04:00
|
|
|
|
def self.postgresql_minimum_supported_version?
|
2020-07-06 23:09:32 -04:00
|
|
|
|
version.to_f >= MINIMUM_POSTGRES_VERSION
|
2018-07-24 07:23:54 -04:00
|
|
|
|
end
|
|
|
|
|
|
2020-07-06 23:09:32 -04:00
|
|
|
|
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:
|
2021-06-15 17:10:04 -04:00
|
|
|
|
https://gitlab.com/gitlab-org/gitlab-development-kit/-/blob/main/doc/howto/postgresql.md#upgrade-postgresql
|
2020-07-06 23:09:32 -04:00
|
|
|
|
<% 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
|
|
|
|
|
|
2021-04-27 17:10:09 -04:00
|
|
|
|
def self.nulls_order(field, direction = :asc, nulls_order = :nulls_last)
|
|
|
|
|
raise ArgumentError unless [:nulls_last, :nulls_first].include?(nulls_order)
|
|
|
|
|
raise ArgumentError unless [:asc, :desc].include?(direction)
|
|
|
|
|
|
|
|
|
|
case nulls_order
|
|
|
|
|
when :nulls_last then nulls_last_order(field, direction)
|
|
|
|
|
when :nulls_first then nulls_first_order(field, direction)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2016-05-13 11:26:18 -04:00
|
|
|
|
def self.nulls_last_order(field, direction = 'ASC')
|
2019-06-13 09:12:28 -04:00
|
|
|
|
Arel.sql("#{field} #{direction} NULLS LAST")
|
2016-05-13 11:26:18 -04:00
|
|
|
|
end
|
|
|
|
|
|
2017-01-30 19:26:40 -05:00
|
|
|
|
def self.nulls_first_order(field, direction = 'ASC')
|
2019-06-13 09:12:28 -04:00
|
|
|
|
Arel.sql("#{field} #{direction} NULLS FIRST")
|
2017-01-30 19:26:40 -05:00
|
|
|
|
end
|
|
|
|
|
|
2016-06-15 06:10:41 -04:00
|
|
|
|
def self.random
|
2019-06-13 09:12:28 -04:00
|
|
|
|
"RANDOM()"
|
2016-06-15 06:10:41 -04:00
|
|
|
|
end
|
|
|
|
|
|
2017-04-10 06:23:28 -04:00
|
|
|
|
def self.true_value
|
2019-06-13 09:12:28 -04:00
|
|
|
|
"'t'"
|
2015-12-09 10:31:42 -05:00
|
|
|
|
end
|
|
|
|
|
|
2017-04-10 06:23:28 -04:00
|
|
|
|
def self.false_value
|
2019-06-13 09:12:28 -04:00
|
|
|
|
"'f'"
|
2015-12-09 10:31:42 -05:00
|
|
|
|
end
|
2016-02-01 21:29:37 -05:00
|
|
|
|
|
2017-02-13 08:45:26 -05:00
|
|
|
|
def self.with_connection_pool(pool_size)
|
|
|
|
|
pool = create_connection_pool(pool_size)
|
|
|
|
|
|
2017-02-14 00:48:13 -05:00
|
|
|
|
begin
|
|
|
|
|
yield(pool)
|
|
|
|
|
ensure
|
|
|
|
|
pool.disconnect!
|
|
|
|
|
end
|
2017-02-13 08:45:26 -05:00
|
|
|
|
end
|
|
|
|
|
|
2017-10-21 18:55:59 -04:00
|
|
|
|
# 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-06-13 09:12:28 -04:00
|
|
|
|
# the inserted rows
|
2017-11-24 13:04:44 -05:00
|
|
|
|
# disable_quote - A key or an Array of keys to exclude from quoting (You
|
|
|
|
|
# become responsible for protection from SQL injection for
|
|
|
|
|
# these keys!)
|
2019-12-10 02:53:40 -05:00
|
|
|
|
# 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-06-09 07:48:25 -04:00
|
|
|
|
return if rows.empty?
|
|
|
|
|
|
|
|
|
|
keys = rows.first.keys
|
|
|
|
|
columns = keys.map { |key| connection.quote_column_name(key) }
|
|
|
|
|
|
2017-11-24 13:04:44 -05:00
|
|
|
|
disable_quote = Array(disable_quote).to_set
|
2017-06-09 07:48:25 -04:00
|
|
|
|
tuples = rows.map do |row|
|
2017-11-26 23:37:23 -05:00
|
|
|
|
keys.map do |k|
|
2017-11-24 13:04:44 -05:00
|
|
|
|
disable_quote.include?(k) ? row[k] : connection.quote(row[k])
|
|
|
|
|
end
|
2017-06-09 07:48:25 -04:00
|
|
|
|
end
|
|
|
|
|
|
2017-10-21 18:55:59 -04:00
|
|
|
|
sql = <<-EOF
|
2017-06-09 07:48:25 -04:00
|
|
|
|
INSERT INTO #{table} (#{columns.join(', ')})
|
|
|
|
|
VALUES #{tuples.map { |tuple| "(#{tuple.join(', ')})" }.join(', ')}
|
|
|
|
|
EOF
|
2017-10-21 18:55:59 -04:00
|
|
|
|
|
2020-07-08 08:09:33 -04:00
|
|
|
|
sql = "#{sql} ON CONFLICT DO NOTHING" if on_conflict == :do_nothing
|
2017-10-21 18:55:59 -04:00
|
|
|
|
|
2019-12-10 02:53:40 -05:00
|
|
|
|
sql = "#{sql} RETURNING id" if return_ids
|
|
|
|
|
|
2017-10-21 18:55:59 -04:00
|
|
|
|
result = connection.execute(sql)
|
|
|
|
|
|
|
|
|
|
if return_ids
|
|
|
|
|
result.values.map { |tuple| tuple[0].to_i }
|
|
|
|
|
else
|
|
|
|
|
[]
|
|
|
|
|
end
|
2017-06-09 07:48:25 -04:00
|
|
|
|
end
|
|
|
|
|
|
2017-10-30 19:21:56 -04:00
|
|
|
|
def self.sanitize_timestamp(timestamp)
|
|
|
|
|
MAX_TIMESTAMP_VALUE > timestamp ? timestamp : MAX_TIMESTAMP_VALUE.dup
|
|
|
|
|
end
|
|
|
|
|
|
2017-02-22 09:43:01 -05:00
|
|
|
|
# pool_size - The size of the DB pool.
|
|
|
|
|
# host - An optional host name to use instead of the default one.
|
2019-08-23 00:19:51 -04:00
|
|
|
|
def self.create_connection_pool(pool_size, host = nil, port = nil)
|
2021-05-12 23:10:19 -04:00
|
|
|
|
original_config = Gitlab::Database.config
|
2017-02-22 09:43:01 -05:00
|
|
|
|
|
2021-05-12 23:10:19 -04:00
|
|
|
|
env_config = original_config.merge(pool: pool_size)
|
2021-05-11 02:10:29 -04:00
|
|
|
|
env_config[:host] = host if host
|
|
|
|
|
env_config[:port] = port if port
|
2017-02-22 09:43:01 -05:00
|
|
|
|
|
2021-05-12 23:10:19 -04:00
|
|
|
|
ActiveRecord::ConnectionAdapters::ConnectionHandler.new.establish_connection(env_config)
|
2017-02-13 08:45:26 -05:00
|
|
|
|
end
|
|
|
|
|
|
2016-02-01 21:29:37 -05:00
|
|
|
|
def self.connection
|
|
|
|
|
ActiveRecord::Base.connection
|
|
|
|
|
end
|
2019-06-25 05:15:35 -04:00
|
|
|
|
private_class_method :connection
|
2016-02-01 21:29:37 -05:00
|
|
|
|
|
2018-03-15 15:50:31 -04:00
|
|
|
|
def self.cached_column_exists?(table_name, column_name)
|
|
|
|
|
connection.schema_cache.columns_hash(table_name).has_key?(column_name.to_s)
|
|
|
|
|
end
|
|
|
|
|
|
2018-03-15 16:12:00 -04:00
|
|
|
|
def self.cached_table_exists?(table_name)
|
2020-02-17 19:09:20 -05:00
|
|
|
|
exists? && connection.schema_cache.data_source_exists?(table_name)
|
2018-03-15 16:12:00 -04:00
|
|
|
|
end
|
|
|
|
|
|
2016-02-01 21:29:37 -05:00
|
|
|
|
def self.database_version
|
|
|
|
|
row = connection.execute("SELECT VERSION()").first
|
|
|
|
|
|
2019-06-13 09:12:28 -04:00
|
|
|
|
row['version']
|
2016-02-01 21:29:37 -05:00
|
|
|
|
end
|
2016-07-19 08:21:39 -04:00
|
|
|
|
|
2020-01-10 07:07:47 -05:00
|
|
|
|
def self.exists?
|
|
|
|
|
connection
|
|
|
|
|
|
|
|
|
|
true
|
2021-04-26 08:09:44 -04:00
|
|
|
|
rescue StandardError
|
2020-01-10 07:07:47 -05:00
|
|
|
|
false
|
|
|
|
|
end
|
|
|
|
|
|
2020-10-19 08:09:20 -04:00
|
|
|
|
def self.system_id
|
|
|
|
|
row = connection.execute('SELECT system_identifier FROM pg_control_system()').first
|
|
|
|
|
|
|
|
|
|
row['system_identifier']
|
|
|
|
|
end
|
|
|
|
|
|
2021-03-15 14:09:05 -04:00
|
|
|
|
# @param [ActiveRecord::Connection] ar_connection
|
|
|
|
|
# @return [String]
|
2020-10-05 14:08:51 -04:00
|
|
|
|
def self.get_write_location(ar_connection)
|
2021-06-14 11:09:48 -04:00
|
|
|
|
use_new_load_balancer_query = Gitlab::Utils.to_boolean(ENV['USE_NEW_LOAD_BALANCER_QUERY'], default: true)
|
2021-03-15 14:09:05 -04:00
|
|
|
|
|
|
|
|
|
sql = if use_new_load_balancer_query
|
|
|
|
|
<<~NEWSQL
|
|
|
|
|
SELECT CASE
|
|
|
|
|
WHEN pg_is_in_recovery() = true AND EXISTS (SELECT 1 FROM pg_stat_get_wal_senders())
|
|
|
|
|
THEN pg_last_wal_replay_lsn()::text
|
|
|
|
|
WHEN pg_is_in_recovery() = false
|
|
|
|
|
THEN pg_current_wal_insert_lsn()::text
|
|
|
|
|
ELSE NULL
|
|
|
|
|
END AS location;
|
|
|
|
|
NEWSQL
|
|
|
|
|
else
|
|
|
|
|
<<~SQL
|
|
|
|
|
SELECT pg_current_wal_insert_lsn()::text AS location
|
|
|
|
|
SQL
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
row = ar_connection.select_all(sql).first
|
2020-10-05 14:08:51 -04:00
|
|
|
|
row['location'] if row
|
|
|
|
|
end
|
|
|
|
|
|
2016-07-19 08:21:39 -04:00
|
|
|
|
private_class_method :database_version
|
2018-09-11 21:41:14 -04:00
|
|
|
|
|
|
|
|
|
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-06-25 05:15:35 -04:00
|
|
|
|
|
2021-06-21 20:07:53 -04:00
|
|
|
|
def self.dbname(ar_connection)
|
|
|
|
|
if ar_connection.respond_to?(:pool) &&
|
|
|
|
|
ar_connection.pool.respond_to?(:db_config) &&
|
|
|
|
|
ar_connection.pool.db_config.respond_to?(:database)
|
|
|
|
|
return ar_connection.pool.db_config.database
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
'unknown'
|
|
|
|
|
end
|
|
|
|
|
|
2019-06-25 05:15:35 -04:00
|
|
|
|
# inside_transaction? will return true if the caller is running within a transaction. Handles special cases
|
2019-04-26 11:49:19 -04:00
|
|
|
|
# when running inside a test environment, where tests may be wrapped in transactions
|
2019-06-25 05:15:35 -04:00
|
|
|
|
def self.inside_transaction?
|
2019-04-26 11:49:19 -04:00
|
|
|
|
if Rails.env.test?
|
|
|
|
|
ActiveRecord::Base.connection.open_transactions > open_transactions_baseline
|
|
|
|
|
else
|
|
|
|
|
ActiveRecord::Base.connection.open_transactions > 0
|
|
|
|
|
end
|
2019-06-25 05:15:35 -04:00
|
|
|
|
end
|
|
|
|
|
|
2019-04-26 11:49:19 -04:00
|
|
|
|
# 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
|
2019-06-25 05:15:35 -04:00
|
|
|
|
|
2019-04-26 11:49:19 -04:00
|
|
|
|
def self.open_transactions_baseline
|
|
|
|
|
@open_transactions_baseline ||= 0
|
2019-06-25 05:15:35 -04:00
|
|
|
|
end
|
|
|
|
|
private_class_method :open_transactions_baseline
|
2019-06-21 05:18:19 -04:00
|
|
|
|
|
|
|
|
|
# Monkeypatch rails with upgraded database observability
|
|
|
|
|
def self.install_monkey_patches
|
|
|
|
|
ActiveRecord::Base.prepend(ActiveRecordBaseTransactionMetrics)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# MonkeyPatch for ActiveRecord::Base for adding observability
|
|
|
|
|
module ActiveRecordBaseTransactionMetrics
|
2021-03-10 10:09:11 -05:00
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
|
|
class_methods do
|
|
|
|
|
# A monkeypatch over ActiveRecord::Base.transaction.
|
|
|
|
|
# It provides observability into transactional methods.
|
|
|
|
|
def transaction(**options, &block)
|
|
|
|
|
ActiveSupport::Notifications.instrument('transaction.active_record', { connection: connection }) do
|
|
|
|
|
super(**options, &block)
|
|
|
|
|
end
|
|
|
|
|
end
|
2019-06-21 05:18:19 -04:00
|
|
|
|
end
|
|
|
|
|
end
|
2015-10-07 06:30:10 -04:00
|
|
|
|
end
|
|
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
|
Gitlab::Database.prepend_mod_with('Gitlab::Database')
|