Class: ActiveRecord::ConnectionAdapters::CockroachDBAdapter

Inherits:
PostgreSQLAdapter
  • Object
show all
Defined in:
lib/active_record/connection_adapters/cockroachdb_adapter.rb

Constant Summary collapse

ADAPTER_NAME =
"CockroachDB".freeze
Utils =

Note that in the migration from ActiveRecord 5.0 to 5.1, the ‘extract_schema_qualified_name` method was aliased in the PostgreSQLAdapter. To ensure backward compatibility with both <5.1 and 5.1, we rename it here to use the same original `Utils` module.

ActiveRecord::ConnectionAdapters::PostgreSQL::Utils

Instance Method Summary collapse

Instance Method Details

#indexes(table_name, name = nil) ⇒ Object

:nodoc:



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 59

def indexes(table_name, name = nil) # :nodoc:
  # The PostgreSQL adapter uses a correlated subquery in the following query,
  # which CockroachDB does not yet support. That portion of the query fetches
  # any non-standard opclasses that each index uses. CockroachDB also doesn't
  # support opclasses at this time, so the query is modified to just remove
  # the section about opclasses entirely.
  if name
    ActiveSupport::Deprecation.warn("      Passing name to #indexes is deprecated without replacement.\n    MSG\n  end\n\n  table = Utils.extract_schema_qualified_name(table_name.to_s)\n\n  result = query(<<-SQL, \"SCHEMA\")\n    SELECT distinct i.relname, d.indisunique, d.indkey, pg_get_indexdef(d.indexrelid), t.oid,\n                    pg_catalog.obj_description(i.oid, 'pg_class') AS comment\n    FROM pg_class t\n    INNER JOIN pg_index d ON t.oid = d.indrelid\n    INNER JOIN pg_class i ON d.indexrelid = i.oid\n    LEFT JOIN pg_namespace n ON n.oid = i.relnamespace\n    WHERE i.relkind = 'i'\n      AND d.indisprimary = 'f'\n      AND t.relname = '\#{table.identifier}'\n      AND n.nspname = \#{table.schema ? \"'\#{table.schema}'\" : 'ANY (current_schemas(false))'}\n    ORDER BY i.relname\n  SQL\n\n  result.map do |row|\n    index_name = row[0]\n    unique = row[1]\n    indkey = row[2].split(\" \").map(&:to_i)\n    inddef = row[3]\n    oid = row[4]\n    comment = row[5]\n\n    expressions, where = inddef.scan(/\\((.+?)\\)(?: WHERE (.+))?\\z/).flatten\n\n    if indkey.include?(0)\n      columns = expressions\n    else\n      columns = Hash[query(<<-SQL.strip_heredoc, \"SCHEMA\")].values_at(*indkey).compact\n        SELECT a.attnum, a.attname\n        FROM pg_attribute a\n        WHERE a.attrelid = \#{oid}\n        AND a.attnum IN (\#{indkey.join(\",\")})\n      SQL\n\n      # add info on sort order for columns (only desc order is explicitly specified, asc is the default)\n      orders = Hash[\n        expressions.scan(/(\\w+) DESC/).flatten.map { |order_column| [order_column, :desc] }\n      ]\n    end\n\n    ActiveRecord::ConnectionAdapters::IndexDefinition.new(table_name, index_name, unique, columns, [], orders, where, nil, nil, comment.presence)\n  end.compact\nend\n".squish)

#primary_keys(table_name) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 118

def primary_keys(table_name)
    name = Utils.extract_schema_qualified_name(table_name.to_s)
    select_values("    SELECT column_name\n        FROM information_schema.key_column_usage kcu\n        JOIN information_schema.table_constraints tc\n        ON kcu.table_name = tc.table_name\n        AND kcu.table_schema = tc.table_schema\n        AND kcu.constraint_name = tc.constraint_name\n        WHERE constraint_type = 'PRIMARY KEY'\n        AND kcu.table_name = \#{quote(name.identifier)}\n        AND kcu.table_schema = \#{name.schema ? quote(name.schema) : \"ANY (current_schemas(false))\"}\n        ORDER BY kcu.ordinal_position\n    SQL\nend\n".strip_heredoc, "SCHEMA")

#supports_ddl_transactions?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 39

def supports_ddl_transactions?
    false
end

#supports_extensions?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 43

def supports_extensions?
    false
end

#supports_json?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 35

def supports_json?
    false
end

#supports_materialized_views?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 51

def supports_materialized_views?
    false
end

#supports_pg_crypto_uuid?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 55

def supports_pg_crypto_uuid?
    false
end

#supports_ranges?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/active_record/connection_adapters/cockroachdb_adapter.rb', line 47

def supports_ranges?
    false
end

#table_alias_lengthObject Also known as: index_name_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.



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

def table_alias_length
  63
end