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

75 lines
1.6 KiB
Ruby
Raw Normal View History

2015-10-24 18:46:33 +05:30
module Gitlab
module Database
2016-06-22 15:30:34 +05:30
# 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
2016-04-02 18:10:28 +05:30
def self.adapter_name
connection.adapter_name
end
2015-10-24 18:46:33 +05:30
def self.mysql?
2016-06-02 11:05:42 +05:30
adapter_name.casecmp('mysql2').zero?
2015-10-24 18:46:33 +05:30
end
def self.postgresql?
2016-06-02 11:05:42 +05:30
adapter_name.casecmp('postgresql').zero?
2016-04-02 18:10:28 +05:30
end
def self.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
def self.nulls_last_order(field, direction = 'ASC')
order = "#{field} #{direction}"
if Gitlab::Database.postgresql?
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
def self.random
Gitlab::Database.postgresql? ? "RANDOM()" : "RAND()"
end
2015-12-23 02:04:40 +05:30
def true_value
2016-04-02 18:10:28 +05:30
if Gitlab::Database.postgresql?
2015-12-23 02:04:40 +05:30
"'t'"
else
1
end
end
def false_value
2016-04-02 18:10:28 +05:30
if Gitlab::Database.postgresql?
2015-12-23 02:04:40 +05:30
"'f'"
else
0
end
end
2016-04-02 18:10:28 +05:30
private
def self.connection
ActiveRecord::Base.connection
end
def self.database_version
row = connection.execute("SELECT VERSION()").first
if postgresql?
row['version']
else
row.first
end
end
2015-10-24 18:46:33 +05:30
end
end