Read about our upcoming Code of Conduct on this issue

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

Yorick Peterse's avatar
Yorick Peterse committed
12
13
14
15
    def self.config
      ActiveRecord::Base.configurations[Rails.env]
    end

16
17
18
19
20
21
22
23
    def self.username
      config['username'] || ENV['USER']
    end

    def self.database_name
      config['database']
    end

24
    def self.adapter_name
Yorick Peterse's avatar
Yorick Peterse committed
25
      config['adapter']
26
27
    end

28
    def self.mysql?
29
      adapter_name.casecmp('mysql2').zero?
30
31
32
    end

    def self.postgresql?
33
      adapter_name.casecmp('postgresql').zero?
34
35
    end

Toon Claes's avatar
Toon Claes committed
36
37
38
39
40
41
42
43
44
    # Overridden in EE
    def self.read_only?
      false
    end

    def self.read_write?
      !self.read_only?
    end

45
    def self.version
46
      @version ||= database_version.match(/\A(?:PostgreSQL |)([^\s]+).*\z/)[1]
47
    end
48

49
50
51
52
    def self.join_lateral_supported?
      postgresql? && version.to_f >= 9.3
    end

53
54
55
56
    def self.replication_slots_supported?
      postgresql? && version.to_f >= 9.4
    end

57
58
59
    def self.nulls_last_order(field, direction = 'ASC')
      order = "#{field} #{direction}"

60
      if postgresql?
61
62
63
64
65
66
67
68
69
70
        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

71
72
73
    def self.nulls_first_order(field, direction = 'ASC')
      order = "#{field} #{direction}"

74
      if postgresql?
75
76
77
78
79
80
81
82
83
84
        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

85
    def self.random
86
      postgresql? ? "RANDOM()" : "RAND()"
87
88
    end

89
90
    def self.true_value
      if postgresql?
91
92
93
94
95
96
        "'t'"
      else
        1
      end
    end

97
98
    def self.false_value
      if postgresql?
99
100
101
102
103
        "'f'"
      else
        0
      end
    end
104

105
106
107
    def self.with_connection_pool(pool_size)
      pool = create_connection_pool(pool_size)

108
109
110
111
112
      begin
        yield(pool)
      ensure
        pool.disconnect!
      end
113
114
    end

115
116
117
118
119
120
121
122
    # 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.
123
124
125
126
    # 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: [])
127
128
129
130
      return if rows.empty?

      keys = rows.first.keys
      columns = keys.map { |key| connection.quote_column_name(key) }
131
      return_ids = false if mysql?
132

133
      disable_quote = Array(disable_quote).to_set
134
      tuples = rows.map do |row|
135
        keys.map do |k|
136
137
          disable_quote.include?(k) ? row[k] : connection.quote(row[k])
        end
138
139
      end

140
      sql = <<-EOF
141
142
143
        INSERT INTO #{table} (#{columns.join(', ')})
        VALUES #{tuples.map { |tuple| "(#{tuple.join(', ')})" }.join(', ')}
      EOF
144
145
146
147
148
149
150
151
152
153
154
155

      if return_ids
        sql << 'RETURNING id'
      end

      result = connection.execute(sql)

      if return_ids
        result.values.map { |tuple| tuple[0].to_i }
      else
        []
      end
156
157
    end

158
159
160
161
    def self.sanitize_timestamp(timestamp)
      MAX_TIMESTAMP_VALUE > timestamp ? timestamp : MAX_TIMESTAMP_VALUE.dup
    end

162
163
164
    # 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)
165
166
167
      # See activerecord-4.2.7.1/lib/active_record/connection_adapters/connection_specification.rb
      env = Rails.env
      original_config = ActiveRecord::Base.configurations
168

169
      env_config = original_config[env].merge('pool' => pool_size)
170
171
      env_config['host'] = host if host

172
173
174
175
176
177
178
179
180
181
      config = original_config.merge(env => env_config)

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

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

182
183
184
185
    def self.connection
      ActiveRecord::Base.connection
    end

186
187
188
189
    def self.cached_column_exists?(table_name, column_name)
      connection.schema_cache.columns_hash(table_name).has_key?(column_name.to_s)
    end

190
    def self.cached_table_exists?(table_name)
191
192
193
194
195
      if Gitlab.rails5?
        connection.schema_cache.data_source_exists?(table_name)
      else
        connection.schema_cache.table_exists?(table_name)
      end
196
197
    end

198
199
    private_class_method :connection

200
201
202
203
204
205
206
207
208
    def self.database_version
      row = connection.execute("SELECT VERSION()").first

      if postgresql?
        row['version']
      else
        row.first
      end
    end
209
210

    private_class_method :database_version
211
212
  end
end