2015-10-07 06:30:10 -04:00
|
|
|
module Gitlab
|
|
|
|
module Database
|
2016-06-18 13:55:45 -04:00
|
|
|
# The max value of INTEGER type is the same between MySQL and PostgreSQL:
|
|
|
|
# https://www.postgresql.org/docs/9.2/static/datatype-numeric.html
|
|
|
|
# http://dev.mysql.com/doc/refman/5.7/en/integer-types.html
|
|
|
|
MAX_INT_VALUE = 2147483647
|
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
|
|
|
|
MAX_TIMESTAMP_VALUE = Time.at((1 << 31) - 1).freeze
|
2016-06-18 13:55:45 -04:00
|
|
|
|
2017-03-17 09:16:47 -04:00
|
|
|
def self.config
|
|
|
|
ActiveRecord::Base.configurations[Rails.env]
|
|
|
|
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
|
|
|
|
|
2015-10-07 06:30:10 -04:00
|
|
|
def self.mysql?
|
2016-04-27 18:52:32 -04:00
|
|
|
adapter_name.casecmp('mysql2').zero?
|
2015-10-07 06:30:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.postgresql?
|
2016-04-27 18:52:32 -04:00
|
|
|
adapter_name.casecmp('postgresql').zero?
|
2016-02-01 21:29:37 -05:00
|
|
|
end
|
|
|
|
|
2017-09-19 03:44:58 -04:00
|
|
|
# Overridden in EE
|
|
|
|
def self.read_only?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.read_write?
|
|
|
|
!self.read_only?
|
|
|
|
end
|
|
|
|
|
2018-07-06 13:07:49 -04:00
|
|
|
# check whether the underlying database is in read-only mode
|
|
|
|
def self.db_read_only?
|
|
|
|
if postgresql?
|
|
|
|
ActiveRecord::Base.connection.execute('SELECT pg_is_in_recovery()')
|
|
|
|
.first
|
|
|
|
.fetch('pg_is_in_recovery') == 't'
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
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
|
|
|
|
2018-07-24 07:23:54 -04:00
|
|
|
def self.postgresql_9_or_less?
|
|
|
|
postgresql? && version.to_f < 10
|
|
|
|
end
|
|
|
|
|
2017-07-27 13:42:15 -04:00
|
|
|
def self.join_lateral_supported?
|
|
|
|
postgresql? && version.to_f >= 9.3
|
|
|
|
end
|
|
|
|
|
2017-12-05 11:43:47 -05:00
|
|
|
def self.replication_slots_supported?
|
|
|
|
postgresql? && version.to_f >= 9.4
|
|
|
|
end
|
|
|
|
|
2018-07-24 07:23:54 -04:00
|
|
|
def self.pg_stat_wal_receiver_supported?
|
|
|
|
postgresql? && version.to_f >= 9.6
|
|
|
|
end
|
|
|
|
|
|
|
|
# map some of the function names that changed between PostgreSQL 9 and 10
|
|
|
|
# https://wiki.postgresql.org/wiki/New_in_postgres_10
|
|
|
|
def self.pg_wal_lsn_diff
|
|
|
|
Gitlab::Database.postgresql_9_or_less? ? 'pg_xlog_location_diff' : 'pg_wal_lsn_diff'
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.pg_current_wal_insert_lsn
|
|
|
|
Gitlab::Database.postgresql_9_or_less? ? 'pg_current_xlog_insert_location' : 'pg_current_wal_insert_lsn'
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.pg_last_wal_receive_lsn
|
|
|
|
Gitlab::Database.postgresql_9_or_less? ? 'pg_last_xlog_receive_location' : 'pg_last_wal_receive_lsn'
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.pg_last_wal_replay_lsn
|
|
|
|
Gitlab::Database.postgresql_9_or_less? ? 'pg_last_xlog_replay_location' : 'pg_last_wal_replay_lsn'
|
|
|
|
end
|
|
|
|
|
2016-05-13 11:26:18 -04:00
|
|
|
def self.nulls_last_order(field, direction = 'ASC')
|
|
|
|
order = "#{field} #{direction}"
|
|
|
|
|
2017-02-15 16:14:17 -05:00
|
|
|
if postgresql?
|
2016-05-13 11:26:18 -04:00
|
|
|
order << ' NULLS LAST'
|
|
|
|
else
|
|
|
|
# `field IS NULL` will be `0` for non-NULL columns and `1` for NULL
|
|
|
|
# columns. In the (default) ascending order, `0` comes first.
|
|
|
|
order.prepend("#{field} IS NULL, ") if direction == 'ASC'
|
|
|
|
end
|
|
|
|
|
|
|
|
order
|
|
|
|
end
|
|
|
|
|
2017-01-30 19:26:40 -05:00
|
|
|
def self.nulls_first_order(field, direction = 'ASC')
|
|
|
|
order = "#{field} #{direction}"
|
|
|
|
|
2017-02-15 16:14:17 -05:00
|
|
|
if postgresql?
|
2017-01-30 19:26:40 -05:00
|
|
|
order << ' NULLS FIRST'
|
|
|
|
else
|
|
|
|
# `field IS NULL` will be `0` for non-NULL columns and `1` for NULL
|
|
|
|
# columns. In the (default) ascending order, `0` comes first.
|
|
|
|
order.prepend("#{field} IS NULL, ") if direction == 'DESC'
|
|
|
|
end
|
|
|
|
|
|
|
|
order
|
|
|
|
end
|
|
|
|
|
2016-06-15 06:10:41 -04:00
|
|
|
def self.random
|
2017-02-15 16:14:17 -05:00
|
|
|
postgresql? ? "RANDOM()" : "RAND()"
|
2016-06-15 06:10:41 -04:00
|
|
|
end
|
|
|
|
|
2017-04-10 06:23:28 -04:00
|
|
|
def self.true_value
|
|
|
|
if postgresql?
|
2015-12-09 10:31:42 -05:00
|
|
|
"'t'"
|
|
|
|
else
|
|
|
|
1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-10 06:23:28 -04:00
|
|
|
def self.false_value
|
|
|
|
if postgresql?
|
2015-12-09 10:31:42 -05:00
|
|
|
"'f'"
|
|
|
|
else
|
|
|
|
0
|
|
|
|
end
|
|
|
|
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
|
|
|
|
# the inserted rows, this only works on PostgreSQL.
|
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!)
|
|
|
|
def self.bulk_insert(table, rows, return_ids: false, disable_quote: [])
|
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-10-21 18:55:59 -04:00
|
|
|
return_ids = false if mysql?
|
2017-06-09 07:48:25 -04:00
|
|
|
|
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
|
|
|
|
|
|
|
if return_ids
|
|
|
|
sql << 'RETURNING id'
|
|
|
|
end
|
|
|
|
|
|
|
|
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.
|
|
|
|
def self.create_connection_pool(pool_size, host = nil)
|
2017-02-13 08:45:26 -05:00
|
|
|
# See activerecord-4.2.7.1/lib/active_record/connection_adapters/connection_specification.rb
|
|
|
|
env = Rails.env
|
|
|
|
original_config = ActiveRecord::Base.configurations
|
2017-02-22 09:43:01 -05:00
|
|
|
|
2017-02-13 08:45:26 -05:00
|
|
|
env_config = original_config[env].merge('pool' => pool_size)
|
2017-02-22 09:43:01 -05:00
|
|
|
env_config['host'] = host if host
|
|
|
|
|
2017-02-13 08:45:26 -05:00
|
|
|
config = original_config.merge(env => env_config)
|
|
|
|
|
|
|
|
spec =
|
|
|
|
ActiveRecord::
|
|
|
|
ConnectionAdapters::
|
|
|
|
ConnectionSpecification::Resolver.new(config).spec(env.to_sym)
|
|
|
|
|
|
|
|
ActiveRecord::ConnectionAdapters::ConnectionPool.new(spec)
|
|
|
|
end
|
|
|
|
|
2016-02-01 21:29:37 -05:00
|
|
|
def self.connection
|
|
|
|
ActiveRecord::Base.connection
|
|
|
|
end
|
|
|
|
|
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)
|
2018-06-05 05:29:33 -04:00
|
|
|
if Gitlab.rails5?
|
|
|
|
connection.schema_cache.data_source_exists?(table_name)
|
|
|
|
else
|
|
|
|
connection.schema_cache.table_exists?(table_name)
|
|
|
|
end
|
2018-03-15 16:12:00 -04:00
|
|
|
end
|
|
|
|
|
2016-07-19 08:21:39 -04:00
|
|
|
private_class_method :connection
|
|
|
|
|
2016-02-01 21:29:37 -05:00
|
|
|
def self.database_version
|
|
|
|
row = connection.execute("SELECT VERSION()").first
|
|
|
|
|
|
|
|
if postgresql?
|
|
|
|
row['version']
|
|
|
|
else
|
|
|
|
row.first
|
|
|
|
end
|
|
|
|
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
|
2015-10-07 06:30:10 -04:00
|
|
|
end
|
|
|
|
end
|