Class: ActiveRecord::ConnectionAdapters::CockroachDBAdapter
Constant Summary
collapse
- ADAPTER_NAME =
"CockroachDB".freeze
- DEFAULT_PRIMARY_KEY =
"rowid"
- SPATIAL_COLUMN_OPTIONS =
{
geography: { geographic: true },
geometry: {},
geometry_collection: {},
line_string: {},
multi_line_string: {},
multi_point: {},
multi_polygon: {},
spatial: {},
st_point: {},
st_polygon: {},
}
- DEFAULT_SRID =
0
Class Method Summary
collapse
Instance Method Summary
collapse
#quote
#insert_fixtures_set, #transaction_isolation_levels
#disable_referential_integrity
#add_index, #create_schema_dumper, #create_table_definition, #default_sequence_name, #foreign_keys, #native_database_types, #new_column_from_field, #primary_key, #reset_pk_sequence!, #schema_creation, #spatial_column_info, #type_to_sql
Constructor Details
#initialize(connection, logger, conn_params, config) ⇒ CockroachDBAdapter
Returns a new instance of CockroachDBAdapter.
[View source]
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 241
def initialize(connection, logger, conn_params, config)
super(connection, logger, conn_params, config)
crdb_version_string = query_value("SHOW crdb_version")
if crdb_version_string.include? "v22.1"
version_num = query_value(<<~SQL, "VERSION")
SELECT
CASE
WHEN crdb_internal.is_at_least_version('22.2') THEN 2220
WHEN crdb_internal.is_at_least_version('22.1') THEN 2210
ELSE 2120
END;
SQL
else
if crdb_version_string.include? "v1."
version_num = 1
elsif crdb_version_string.include? "v2."
version_num 2
elsif crdb_version_string.include? "v19.1."
version_num = 1910
elsif crdb_version_string.include? "v19.2."
version_num = 1920
elsif crdb_version_string.include? "v20.1."
version_num = 2010
elsif crdb_version_string.include? "v20.2."
version_num = 2020
elsif crdb_version_string.include? "v21.1."
version_num = 2110
else
version_num = 2120
end
end
@crdb_version = version_num.to_i
if @crdb_version >= 2120
begin
execute("SET intervalstyle_enabled = true", "SCHEMA")
execute("SET intervalstyle = iso_8601", "SCHEMA")
rescue
end
end
end
|
Class Method Details
.database_exists?(config) ⇒ Boolean
[View source]
298
299
300
301
302
|
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 298
def self.database_exists?(config)
!!ActiveRecord::Base.cockroachdb_connection(config)
rescue ActiveRecord::NoDatabaseError
false
end
|
.initialize_type_map(m = type_map) ⇒ Object
[View source]
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
|
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 318
def initialize_type_map(m = type_map)
%w(
geography
geometry
geometry_collection
line_string
multi_line_string
multi_point
multi_polygon
st_point
st_polygon
).each do |geo_type|
m.register_type(geo_type) do |oid, _, sql_type|
CockroachDB::OID::Spatial.new(oid, sql_type)
end
end
super(m)
m.register_type "numeric" do |_, fmod, sql_type|
precision = (sql_type)
scale = (sql_type)
if fmod && ((fmod == -1 && !precision.nil?) || (fmod - 4 & 0xffff).zero?)
Type::DecimalWithoutScale.new(precision: precision)
else
ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Decimal.new(precision: precision, scale: scale)
end
end
end
|
.spatial_column_options(key) ⇒ Object
[View source]
128
129
130
|
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 128
def self.spatial_column_options(key)
SPATIAL_COLUMN_OPTIONS[key]
end
|
Instance Method Details
#create_enum(name, values) ⇒ Object
override The PostgreSQLAdapter uses syntax for an anonymous function (DO $$) that CockroachDB does not support.
Given a name and an array of values, creates an enum type.
[View source]
309
310
311
312
313
314
315
|
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 309
def create_enum(name, values)
sql_values = values.map { |s| "'#{s}'" }.join(", ")
query = <<~SQL
CREATE TYPE IF NOT EXISTS \"#{name}\" AS ENUM (#{sql_values});
SQL
exec_query(query)
end
|
#debugging? ⇒ Boolean
[View source]
149
150
151
|
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 149
def debugging?
!!ENV["DEBUG_COCKROACHDB_ADAPTER"]
end
|
#default_srid ⇒ Object
[View source]
136
137
138
|
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 136
def default_srid
DEFAULT_SRID
end
|
#max_identifier_length ⇒ Object
Also known as:
index_name_length, table_alias_length
This is hardcoded to 63 (as previously was in ActiveRecord 5.0) to aid in migration from PostgreSQL to CockroachDB. In practice, this limitation is arbitrary since CockroachDB supports index name lengths and table alias lengths far greater than this value. For the time being though, we match the original behavior for PostgreSQL to simplify migrations.
Note that in the migration to ActiveRecord 5.1, this was changed in PostgreSQLAdapter to use ‘SHOW max_identifier_length` (which does not exist in CockroachDB). Therefore, we have to redefine this here.
[View source]
235
236
237
|
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 235
def max_identifier_length
63
end
|
#max_transaction_retries ⇒ Object
[View source]
153
154
155
|
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 153
def max_transaction_retries
@max_transaction_retries ||= @config.fetch(:max_transaction_retries, 3)
end
|
#postgis_lib_version ⇒ Object
[View source]
132
133
134
|
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 132
def postgis_lib_version
@postgis_lib_version ||= select_value("SELECT PostGIS_Lib_Version()")
end
|
#postgresql_version ⇒ Object
CockroachDB 20.1 can run queries that work against PostgreSQL 10+.
[View source]
158
159
160
|
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 158
def postgresql_version
100000
end
|
#srs_database_columns ⇒ Object
[View source]
140
141
142
143
144
145
146
147
|
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 140
def srs_database_columns
{
auth_name_column: "auth_name",
auth_srid_column: "auth_srid",
proj4text_column: "proj4text",
srtext_column: "srtext",
}
end
|
#supports_advisory_locks? ⇒ Boolean
[View source]
206
207
208
|
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 206
def supports_advisory_locks?
false
end
|
#supports_bulk_alter? ⇒ Boolean
[View source]
162
163
164
|
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 162
def supports_bulk_alter?
false
end
|
[View source]
198
199
200
|
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 198
def
@crdb_version >= 2010
end
|
[View source]
202
203
204
|
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 202
def
false
end
|
#supports_datetime_with_precision? ⇒ Boolean
[View source]
194
195
196
|
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 194
def supports_datetime_with_precision?
false
end
|
#supports_ddl_transactions? ⇒ Boolean
[View source]
171
172
173
|
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 171
def supports_ddl_transactions?
false
end
|
#supports_deferrable_constraints? ⇒ Boolean
[View source]
222
223
224
|
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 222
def supports_deferrable_constraints?
false
end
|
#supports_expression_index? ⇒ Boolean
[View source]
187
188
189
190
191
192
|
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 187
def supports_expression_index?
false
end
|
#supports_extensions? ⇒ Boolean
[View source]
175
176
177
|
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 175
def supports_extensions?
false
end
|
#supports_json? ⇒ Boolean
[View source]
166
167
168
169
|
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 166
def supports_json?
true
end
|
#supports_materialized_views? ⇒ Boolean
[View source]
179
180
181
|
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 179
def supports_materialized_views?
false
end
|
#supports_partial_index? ⇒ Boolean
[View source]
183
184
185
|
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 183
def supports_partial_index?
@crdb_version >= 2020
end
|
#supports_partitioned_indexes? ⇒ Boolean
[View source]
218
219
220
|
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 218
def supports_partitioned_indexes?
false
end
|
#supports_string_to_array_coercion? ⇒ Boolean
[View source]
214
215
216
|
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 214
def supports_string_to_array_coercion?
@crdb_version >= 2020
end
|
#supports_virtual_columns? ⇒ Boolean
[View source]
210
211
212
|
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 210
def supports_virtual_columns?
@crdb_version >= 2110
end
|