BigW Consortium Gitlab

database.rb 3.46 KB
Newer Older
1 2
module Gitlab
  module Database
3 4 5 6 7
    # 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

8 9 10 11
    def self.config
      ActiveRecord::Base.configurations[Rails.env]
    end

12
    def self.adapter_name
13
      config['adapter']
14 15
    end

16
    def self.mysql?
17
      adapter_name.casecmp('mysql2').zero?
18 19 20
    end

    def self.postgresql?
21
      adapter_name.casecmp('postgresql').zero?
22 23 24 25
    end

    def self.version
      database_version.match(/\A(?:PostgreSQL |)([^\s]+).*\z/)[1]
26
    end
27

28 29 30 31
    def self.join_lateral_supported?
      postgresql? && version.to_f >= 9.3
    end

32 33 34
    def self.nulls_last_order(field, direction = 'ASC')
      order = "#{field} #{direction}"

35
      if postgresql?
36 37 38 39 40 41 42 43 44 45
        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

46 47 48
    def self.nulls_first_order(field, direction = 'ASC')
      order = "#{field} #{direction}"

49
      if postgresql?
50 51 52 53 54 55 56 57 58 59
        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

60
    def self.random
61
      postgresql? ? "RANDOM()" : "RAND()"
62 63
    end

64 65
    def self.true_value
      if postgresql?
66 67 68 69 70 71
        "'t'"
      else
        1
      end
    end

72 73
    def self.false_value
      if postgresql?
74 75 76 77 78
        "'f'"
      else
        0
      end
    end
79

80 81 82
    def self.with_connection_pool(pool_size)
      pool = create_connection_pool(pool_size)

83 84 85 86 87
      begin
        yield(pool)
      ensure
        pool.disconnect!
      end
88 89
    end

90 91 92 93 94 95 96 97 98 99
    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

100
      connection.execute <<-EOF
101 102 103 104 105
        INSERT INTO #{table} (#{columns.join(', ')})
        VALUES #{tuples.map { |tuple| "(#{tuple.join(', ')})" }.join(', ')}
      EOF
    end

106 107 108
    # 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)
109 110 111
      # See activerecord-4.2.7.1/lib/active_record/connection_adapters/connection_specification.rb
      env = Rails.env
      original_config = ActiveRecord::Base.configurations
112

113
      env_config = original_config[env].merge('pool' => pool_size)
114 115
      env_config['host'] = host if host

116 117 118 119 120 121 122 123 124 125
      config = original_config.merge(env => env_config)

      spec =
        ActiveRecord::
          ConnectionAdapters::
          ConnectionSpecification::Resolver.new(config).spec(env.to_sym)

      ActiveRecord::ConnectionAdapters::ConnectionPool.new(spec)
    end

126 127 128 129
    def self.connection
      ActiveRecord::Base.connection
    end

130 131
    private_class_method :connection

132 133 134 135 136 137 138 139 140
    def self.database_version
      row = connection.execute("SELECT VERSION()").first

      if postgresql?
        row['version']
      else
        row.first
      end
    end
141 142

    private_class_method :database_version
143 144
  end
end