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, #foreign_keys, #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.



228
229
230
231
232
233
234
235
236
237
238
239
240
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
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 228

def initialize(connection, logger, conn_params, config)
  super(connection, logger, conn_params, config)

  # crdb_version is the version of the binary running on the node. We
  # really want to use `SHOW CLUSTER SETTING version` to get the cluster
  # version, but that is only available to admins. Instead, we can use
  # crdb_internal.is_at_least_version, but that's only available in 22.1.
  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
    # This branch can be removed once the dialect stops supporting v21.2
    # and earlier.
    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

  # NOTE: this is normally in configure_connection, but that is run
  # before crdb_version is determined. Once all supported versions
  # of CockroachDB support SET intervalstyle it can safely be moved
  # back.
  # Set interval output format to ISO 8601 for ease of parsing by ActiveSupport::Duration.parse
  if @crdb_version >= 2120
    begin
      execute("SET intervalstyle_enabled = true", "SCHEMA")
      execute("SET intervalstyle = iso_8601", "SCHEMA")
    rescue
      # Ignore any error. This can happen with a cluster that has
      # not yet finalized the v21.2 upgrade. v21.2 does not have
      # a way to tell if the upgrade was finalized (see comment above).
    end
  end
end

Class Method Details

.database_exists?(config) ⇒ Boolean

Returns:

  • (Boolean)


285
286
287
288
289
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 285

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

.spatial_column_options(key) ⇒ Object



122
123
124
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 122

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

Instance Method Details

#debugging?Boolean

Returns:

  • (Boolean)


143
144
145
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 143

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

#default_sridObject



130
131
132
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 130

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.



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

def max_identifier_length
  63
end

#max_transaction_retriesObject



147
148
149
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 147

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

#postgis_lib_versionObject



126
127
128
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 126

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+.



152
153
154
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 152

def postgresql_version
  100000
end

#srs_database_columnsObject



134
135
136
137
138
139
140
141
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 134

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)


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

def supports_advisory_locks?
  false
end

#supports_bulk_alter?Boolean

Returns:

  • (Boolean)


156
157
158
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 156

def supports_bulk_alter?
  false
end

#supports_comments?Boolean

Returns:

  • (Boolean)


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

def supports_comments?
  @crdb_version >= 2010
end

#supports_comments_in_create?Boolean

Returns:

  • (Boolean)


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

def supports_comments_in_create?
  false
end

#supports_datetime_with_precision?Boolean

Returns:

  • (Boolean)


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

def supports_datetime_with_precision?
  false
end

#supports_ddl_transactions?Boolean

Returns:

  • (Boolean)


165
166
167
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 165

def supports_ddl_transactions?
  false
end

#supports_expression_index?Boolean

Returns:

  • (Boolean)


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

def supports_expression_index?
  @crdb_version >= 2122
end

#supports_extensions?Boolean

Returns:

  • (Boolean)


169
170
171
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 169

def supports_extensions?
  false
end

#supports_json?Boolean

Returns:

  • (Boolean)


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

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

#supports_materialized_views?Boolean

Returns:

  • (Boolean)


173
174
175
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 173

def supports_materialized_views?
  false
end

#supports_partial_index?Boolean

Returns:

  • (Boolean)


177
178
179
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 177

def supports_partial_index?
  @crdb_version >= 2020
end

#supports_partitioned_indexes?Boolean

Returns:

  • (Boolean)


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

def supports_partitioned_indexes?
  false
end

#supports_string_to_array_coercion?Boolean

Returns:

  • (Boolean)


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

def supports_string_to_array_coercion?
  @crdb_version >= 2020
end

#supports_virtual_columns?Boolean

Returns:

  • (Boolean)


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

def supports_virtual_columns?
  false
end