Class: ActiveRecord::ConnectionAdapters::CockroachDBAdapter

Inherits:
PostgreSQLAdapter
  • Object
show all
Includes:
ActiveRecord::ConnectionAdapters::CockroachDB::DatabaseStatements, ActiveRecord::ConnectionAdapters::CockroachDB::Quoting, ActiveRecord::ConnectionAdapters::CockroachDB::ReferentialIntegrity, ActiveRecord::ConnectionAdapters::CockroachDB::SchemaStatements
Defined in:
lib/active_record/connection_adapters/cockroachdb_adapter.rb

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

Methods included from ActiveRecord::ConnectionAdapters::CockroachDB::DatabaseStatements

#insert_fixtures_set, #transaction_isolation_levels

Methods included from ActiveRecord::ConnectionAdapters::CockroachDB::ReferentialIntegrity

#disable_referential_integrity

Methods included from ActiveRecord::ConnectionAdapters::CockroachDB::SchemaStatements

#add_index, #create_table_definition, #default_sequence_name, #native_database_types, #new_column_from_field, #primary_key, #reset_pk_sequence!, #spatial_column_info, #type_to_sql

Constructor Details

#initialize(connection, logger, conn_params, config) ⇒ CockroachDBAdapter

Returns a new instance of CockroachDBAdapter.



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 269

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? "v1."
    version_num = 1
  elsif crdb_version_string.include? "v2."
    version_num 2
  elsif crdb_version_string.include? "v19.1."
    version_num = 191
  elsif crdb_version_string.include? "v19.2."
    version_num = 192
  elsif crdb_version_string.include? "v20.1."
    version_num = 201
  else
    version_num = 202
  end
  @crdb_version = version_num
end

Class Method Details

.database_exists?(config) ⇒ Boolean

Returns:

  • (Boolean)


289
290
291
292
293
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 289

def self.database_exists?(config)
  !!ActiveRecord::Base.cockroachdb_connection(config)
rescue ActiveRecord::NoDatabaseError
  false
end

.spatial_column_options(key) ⇒ Object



159
160
161
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 159

def self.spatial_column_options(key)
  SPATIAL_COLUMN_OPTIONS[key]
end

Instance Method Details

#column_definitions(table_name) ⇒ Object

override This method makes a sql query to gather information about columns in a table. It returns an array of arrays (one for each col) and passes each to the SchemaStatements#new_column_from_field method as the field parameter. This data is then used to format the column objects for the model and sent to the OID for data casting.

The issue with the default method is that the sql_type field is retrieved with the ‘format_type` function, but this is implemented differently in CockroachDB than PostGIS, so geometry/geography types are missing information which makes parsing them impossible. Below is an example of what `format_type` returns for a geometry column.

column_type: geometry(POINT, 4326) Expected: geometry(POINT, 4326) Actual: geometry

The solution is to make the default query with super, then iterate through the columns and if it is a spatial type, access the proper column_type with the information_schema.columns table.

@see: github.com/rails/rails/blob/8695b028261bdd244e254993255c6641bdbc17a5/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb#L829



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 133

def column_definitions(table_name)
  fields = super
  # iterate through and identify all spatial fields based on format_type
  # being geometry or geography, then query for the information_schema.column
  # column_type because that contains the necessary information.
  fields.map do |field|
    dtype = field[1]
    if dtype == 'geometry' || dtype == 'geography'
      col_name = field[0]
      data_type = \
      query("        SELECT c.data_type\n          FROM information_schema.columns c\n        WHERE c.table_name = \#{quote(table_name)}\n          AND c.column_name = \#{quote(col_name)}\n      SQL\n      field[1] = data_type[0][0]\n    end\n    field\n  end\nend\n", "SCHEMA")

#debugging?Boolean

Returns:

  • (Boolean)


180
181
182
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 180

def debugging?
  !!ENV["DEBUG_COCKROACHDB_ADAPTER"]
end

#default_sridObject



167
168
169
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 167

def default_srid
  DEFAULT_SRID
end

#max_identifier_lengthObject 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.



263
264
265
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 263

def max_identifier_length
  63
end

#max_transaction_retriesObject



184
185
186
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 184

def max_transaction_retries
  @max_transaction_retries ||= @config.fetch(:max_transaction_retries, 3)
end

#postgis_lib_versionObject



163
164
165
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 163

def postgis_lib_version
  @postgis_lib_version ||= select_value("SELECT PostGIS_Lib_Version()")
end

#postgresql_versionObject

CockroachDB 20.1 can run queries that work against PostgreSQL 10+.



189
190
191
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 189

def postgresql_version
  100000
end

#srs_database_columnsObject



171
172
173
174
175
176
177
178
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 171

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

Returns:

  • (Boolean)


237
238
239
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 237

def supports_advisory_locks?
  false
end

#supports_bulk_alter?Boolean

Returns:

  • (Boolean)


193
194
195
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 193

def supports_bulk_alter?
  false
end

#supports_comments?Boolean

Returns:

  • (Boolean)


227
228
229
230
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 227

def supports_comments?
  # See cockroachdb/cockroach#19472.
  false
end

#supports_comments_in_create?Boolean

Returns:

  • (Boolean)


232
233
234
235
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 232

def supports_comments_in_create?
  # See cockroachdb/cockroach#19472.
  false
end

#supports_datetime_with_precision?Boolean

Returns:

  • (Boolean)


223
224
225
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 223

def supports_datetime_with_precision?
  false
end

#supports_ddl_transactions?Boolean

Returns:

  • (Boolean)


202
203
204
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 202

def supports_ddl_transactions?
  false
end

#supports_expression_index?Boolean

Returns:

  • (Boolean)


218
219
220
221
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 218

def supports_expression_index?
  # See cockroachdb/cockroach#9682
  false
end

#supports_extensions?Boolean

Returns:

  • (Boolean)


206
207
208
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 206

def supports_extensions?
  false
end

#supports_json?Boolean

Returns:

  • (Boolean)


197
198
199
200
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 197

def supports_json?
  # FIXME(joey): Add a version check.
  true
end

#supports_materialized_views?Boolean

Returns:

  • (Boolean)


210
211
212
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 210

def supports_materialized_views?
  false
end

#supports_partial_index?Boolean

Returns:

  • (Boolean)


214
215
216
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 214

def supports_partial_index?
  @crdb_version >= 202
end

#supports_partitioned_indexes?Boolean

Returns:

  • (Boolean)


250
251
252
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 250

def supports_partitioned_indexes?
  false
end

#supports_string_to_array_coercion?Boolean

Returns:

  • (Boolean)


246
247
248
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 246

def supports_string_to_array_coercion?
  @crdb_version >= 202
end

#supports_virtual_columns?Boolean

Returns:

  • (Boolean)


241
242
243
244
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 241

def supports_virtual_columns?
  # See cockroachdb/cockroach#20882.
  false
end