2015-10-07 10:30:10 +00:00
|
|
|
module Gitlab
|
|
|
|
module Database
|
2016-06-18 17:55:45 +00: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-03-17 13:16:47 +00:00
|
|
|
def self.config
|
|
|
|
ActiveRecord::Base.configurations[Rails.env]
|
|
|
|
end
|
|
|
|
|
2017-08-18 12:26:23 +00:00
|
|
|
def self.username
|
|
|
|
config['username'] || ENV['USER']
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.database_name
|
|
|
|
config['database']
|
|
|
|
end
|
|
|
|
|
2016-02-02 02:29:37 +00:00
|
|
|
def self.adapter_name
|
2017-03-17 13:16:47 +00:00
|
|
|
config['adapter']
|
2016-02-02 02:29:37 +00:00
|
|
|
end
|
|
|
|
|
2015-10-07 10:30:10 +00:00
|
|
|
def self.mysql?
|
2016-04-27 22:52:32 +00:00
|
|
|
adapter_name.casecmp('mysql2').zero?
|
2015-10-07 10:30:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.postgresql?
|
2016-04-27 22:52:32 +00:00
|
|
|
adapter_name.casecmp('postgresql').zero?
|
2016-02-02 02:29:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.version
|
|
|
|
database_version.match(/\A(?:PostgreSQL |)([^\s]+).*\z/)[1]
|
2015-10-07 10:30:10 +00:00
|
|
|
end
|
2015-12-09 15:31:42 +00:00
|
|
|
|
2017-07-27 17:42:15 +00:00
|
|
|
def self.join_lateral_supported?
|
|
|
|
postgresql? && version.to_f >= 9.3
|
|
|
|
end
|
|
|
|
|
2016-05-13 15:26:18 +00:00
|
|
|
def self.nulls_last_order(field, direction = 'ASC')
|
|
|
|
order = "#{field} #{direction}"
|
|
|
|
|
2017-02-15 21:14:17 +00:00
|
|
|
if postgresql?
|
2016-05-13 15:26:18 +00: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-31 00:26:40 +00:00
|
|
|
def self.nulls_first_order(field, direction = 'ASC')
|
|
|
|
order = "#{field} #{direction}"
|
|
|
|
|
2017-02-15 21:14:17 +00:00
|
|
|
if postgresql?
|
2017-01-31 00:26:40 +00: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 10:10:41 +00:00
|
|
|
def self.random
|
2017-02-15 21:14:17 +00:00
|
|
|
postgresql? ? "RANDOM()" : "RAND()"
|
2016-06-15 10:10:41 +00:00
|
|
|
end
|
|
|
|
|
2017-04-10 10:23:28 +00:00
|
|
|
def self.true_value
|
|
|
|
if postgresql?
|
2015-12-09 15:31:42 +00:00
|
|
|
"'t'"
|
|
|
|
else
|
|
|
|
1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-10 10:23:28 +00:00
|
|
|
def self.false_value
|
|
|
|
if postgresql?
|
2015-12-09 15:31:42 +00:00
|
|
|
"'f'"
|
|
|
|
else
|
|
|
|
0
|
|
|
|
end
|
|
|
|
end
|
2016-02-02 02:29:37 +00:00
|
|
|
|
2017-02-13 13:45:26 +00:00
|
|
|
def self.with_connection_pool(pool_size)
|
|
|
|
pool = create_connection_pool(pool_size)
|
|
|
|
|
2017-02-14 05:48:13 +00:00
|
|
|
begin
|
|
|
|
yield(pool)
|
|
|
|
ensure
|
|
|
|
pool.disconnect!
|
|
|
|
end
|
2017-02-13 13:45:26 +00:00
|
|
|
end
|
|
|
|
|
2017-06-09 11:48:25 +00:00
|
|
|
def self.bulk_insert(table, rows)
|
|
|
|
return if rows.empty?
|
|
|
|
|
|
|
|
keys = rows.first.keys
|
|
|
|
columns = keys.map { |key| connection.quote_column_name(key) }
|
|
|
|
|
|
|
|
tuples = rows.map do |row|
|
|
|
|
row.values_at(*keys).map { |value| connection.quote(value) }
|
|
|
|
end
|
|
|
|
|
2017-06-26 16:21:40 +00:00
|
|
|
connection.execute <<-EOF
|
2017-06-09 11:48:25 +00:00
|
|
|
INSERT INTO #{table} (#{columns.join(', ')})
|
|
|
|
VALUES #{tuples.map { |tuple| "(#{tuple.join(', ')})" }.join(', ')}
|
|
|
|
EOF
|
|
|
|
end
|
|
|
|
|
2017-02-22 14:43:01 +00: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 13:45:26 +00: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 14:43:01 +00:00
|
|
|
|
2017-02-13 13:45:26 +00:00
|
|
|
env_config = original_config[env].merge('pool' => pool_size)
|
2017-02-22 14:43:01 +00:00
|
|
|
env_config['host'] = host if host
|
|
|
|
|
2017-02-13 13:45:26 +00: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-02 02:29:37 +00:00
|
|
|
def self.connection
|
|
|
|
ActiveRecord::Base.connection
|
|
|
|
end
|
|
|
|
|
2016-07-19 12:21:39 +00:00
|
|
|
private_class_method :connection
|
|
|
|
|
2016-02-02 02:29:37 +00:00
|
|
|
def self.database_version
|
|
|
|
row = connection.execute("SELECT VERSION()").first
|
|
|
|
|
|
|
|
if postgresql?
|
|
|
|
row['version']
|
|
|
|
else
|
|
|
|
row.first
|
|
|
|
end
|
|
|
|
end
|
2016-07-19 12:21:39 +00:00
|
|
|
|
|
|
|
private_class_method :database_version
|
2015-10-07 10:30:10 +00:00
|
|
|
end
|
|
|
|
end
|