Module: ActiveRecord::ConnectionAdapters::CockroachDB::SchemaStatements
- Includes:
- PostgreSQL::SchemaStatements
- Included in:
- ActiveRecord::ConnectionAdapters::CockroachDBAdapter
- Defined in:
- lib/active_record/connection_adapters/cockroachdb/schema_statements.rb
Instance Method Summary collapse
- #add_index(table_name, column_name, **options) ⇒ Object
- #create_schema_dumper(options) ⇒ Object
-
#create_table_definition(*args, **kwargs) ⇒ Object
override.
-
#default_sequence_name(table_name, pk = "id") ⇒ Object
CockroachDB uses unique_rowid() for primary keys, not sequences.
-
#foreign_keys(table_name) ⇒ Object
override Modified version of the postgresql foreign_keys method.
-
#native_database_types ⇒ Object
override.
- #new_column_from_field(table_name, field, _definition) ⇒ Object
-
#primary_key(table_name) ⇒ Object
ActiveRecord allows for tables to exist without primary keys.
-
#reset_pk_sequence!(table, pk = nil, sequence = nil) ⇒ Object
This overrides the method from PostegreSQL adapter Resets the sequence of a table’s primary key to the maximum value.
- #schema_creation ⇒ Object
-
#spatial_column_info(table_name) ⇒ Object
memoize hash of column infos for tables.
-
#type_to_sql(type, limit: nil, precision: nil, scale: nil, array: nil) ⇒ Object
CockroachDB will use INT8 if the SQL type is INTEGER, so we make it use INT4 explicitly when needed.
Instance Method Details
#add_index(table_name, column_name, **options) ⇒ Object
7 8 9 10 11 12 13 14 15 |
# File 'lib/active_record/connection_adapters/cockroachdb/schema_statements.rb', line 7 def add_index(table_name, column_name, **) super rescue ActiveRecord::StatementInvalid => error if debugging? && error.cause.class == PG::FeatureNotSupported warn "#{error}\n\nThis error will be ignored and the index will not be created.\n\n" else raise error end end |
#create_schema_dumper(options) ⇒ Object
220 221 222 |
# File 'lib/active_record/connection_adapters/cockroachdb/schema_statements.rb', line 220 def create_schema_dumper() CockroachDB::SchemaDumper.create(self, ) end |
#create_table_definition(*args, **kwargs) ⇒ Object
override
210 211 212 |
# File 'lib/active_record/connection_adapters/cockroachdb/schema_statements.rb', line 210 def create_table_definition(*args, **kwargs) CockroachDB::TableDefinition.new(self, *args, **kwargs) end |
#default_sequence_name(table_name, pk = "id") ⇒ Object
CockroachDB uses unique_rowid() for primary keys, not sequences. It’s possible to force a table to use sequences, but since it’s not the default behavior we’ll always return nil for default_sequence_name.
96 97 98 |
# File 'lib/active_record/connection_adapters/cockroachdb/schema_statements.rb', line 96 def default_sequence_name(table_name, pk = "id") nil end |
#foreign_keys(table_name) ⇒ Object
override Modified version of the postgresql foreign_keys method. Replaces t2.oid::regclass::text with t2.relname since this is more efficient in CockroachDB. Also, CockroachDB does not append the schema name in relname, so we append it manually.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 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 |
# File 'lib/active_record/connection_adapters/cockroachdb/schema_statements.rb', line 41 def foreign_keys(table_name) scope = quoted_scope(table_name) fk_info = exec_query(<<~SQL, "SCHEMA") SELECT CASE WHEN n2.nspname = current_schema() THEN '' ELSE n2.nspname || '.' END || t2.relname AS to_table, a1.attname AS column, a2.attname AS primary_key, c.conname AS name, c.confupdtype AS on_update, c.confdeltype AS on_delete, c.convalidated AS valid, c.condeferrable AS deferrable, c.condeferred AS deferred, c.conkey, c.confkey, c.conrelid, c.confrelid FROM pg_constraint c JOIN pg_class t1 ON c.conrelid = t1.oid JOIN pg_class t2 ON c.confrelid = t2.oid JOIN pg_attribute a1 ON a1.attnum = c.conkey[1] AND a1.attrelid = t1.oid JOIN pg_attribute a2 ON a2.attnum = c.confkey[1] AND a2.attrelid = t2.oid JOIN pg_namespace t3 ON c.connamespace = t3.oid JOIN pg_namespace n2 ON t2.relnamespace = n2.oid WHERE c.contype = 'f' AND t1.relname = #{scope[:name]} AND t3.nspname = #{scope[:schema]} ORDER BY c.conname SQL fk_info.map do |row| to_table = PostgreSQL::Utils.unquote_identifier(row["to_table"]) conkey = row["conkey"].scan(/\d+/).map(&:to_i) confkey = row["confkey"].scan(/\d+/).map(&:to_i) if conkey.size > 1 column = column_names_from_column_numbers(row["conrelid"], conkey) primary_key = column_names_from_column_numbers(row["confrelid"], confkey) else column = PostgreSQL::Utils.unquote_identifier(row["column"]) primary_key = row["primary_key"] end = { column: column, name: row["name"], primary_key: primary_key } [:on_delete] = extract_foreign_key_action(row["on_delete"]) [:on_update] = extract_foreign_key_action(row["on_update"]) [:deferrable] = extract_constraint_deferrable(row["deferrable"], row["deferred"]) [:validate] = row["valid"] to_table = PostgreSQL::Utils.unquote_identifier(row["to_table"]) ForeignKeyDefinition.new(table_name, to_table, ) end end |
#native_database_types ⇒ Object
override
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/active_record/connection_adapters/cockroachdb/schema_statements.rb', line 193 def native_database_types # Add spatial types super.merge( geography: { name: "geography" }, geometry: { name: "geometry" }, geometry_collection: { name: "geometry_collection" }, line_string: { name: "line_string" }, multi_line_string: { name: "multi_line_string" }, multi_point: { name: "multi_point" }, multi_polygon: { name: "multi_polygon" }, spatial: { name: "geometry" }, st_point: { name: "st_point" }, st_polygon: { name: "st_polygon" } ) end |
#new_column_from_field(table_name, field, _definition) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/active_record/connection_adapters/cockroachdb/schema_statements.rb', line 102 def new_column_from_field(table_name, field, _definition) column_name, type, default, notnull, oid, fmod, collation, comment, identity, attgenerated, hidden = field = (column_name, type, oid.to_i, fmod.to_i) default_value = extract_value_from_default(default) if attgenerated.present? default_function = default else default_function = extract_default_function(default_value, default) end if match = default_function&.match(/\Anextval\('"?(?<sequence_name>.+_(?<suffix>seq\d*))"?'::regclass\)\z/) serial = sequence_name_from_parts(table_name, column_name, match[:suffix]) == match[:sequence_name] end # {:dimension=>2, :has_m=>false, :has_z=>false, :name=>"latlon", :srid=>0, :type=>"GEOMETRY"} spatial = spatial_column_info(table_name).get(column_name, .sql_type) CockroachDB::Column.new( column_name, default_value, , !notnull, default_function, collation: collation, comment: comment.presence, serial: serial, identity: identity.presence, spatial: spatial, generated: attgenerated, hidden: hidden ) end |
#primary_key(table_name) ⇒ Object
ActiveRecord allows for tables to exist without primary keys. Databases like PostgreSQL support this behavior, but CockroachDB does not. If a table is created without a primary key, CockroachDB will add a rowid column to serve as its primary key. This breaks a lot of ActiveRecord’s assumptions so we’ll treat tables with rowid primary keys as if they didn’t have primary keys at all. www.cockroachlabs.com/docs/v19.2/create-table.html#create-a-table api.rubyonrails.org/v5.2.4/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-create_table
25 26 27 28 29 30 31 32 33 |
# File 'lib/active_record/connection_adapters/cockroachdb/schema_statements.rb', line 25 def primary_key(table_name) pk = super if pk == CockroachDBAdapter::DEFAULT_PRIMARY_KEY nil else pk end end |
#reset_pk_sequence!(table, pk = nil, sequence = nil) ⇒ Object
This overrides the method from PostegreSQL adapter Resets the sequence of a table’s primary key to the maximum value.
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/active_record/connection_adapters/cockroachdb/schema_statements.rb', line 169 def reset_pk_sequence!(table, pk = nil, sequence = nil) unless pk && sequence default_pk, default_sequence = pk_and_sequence_for(table) pk ||= default_pk sequence ||= default_sequence end if @logger && pk && !sequence @logger.warn "#{table} has primary key #{pk} with no default sequence." end if pk && sequence quoted_sequence = quote_table_name(sequence) max_pk = query_value("SELECT MAX(#{quote_column_name pk}) FROM #{quote_table_name(table)}", "SCHEMA") if max_pk.nil? minvalue = query_value("SELECT seqmin FROM pg_sequence WHERE seqrelid = #{quote(quoted_sequence)}::regclass", "SCHEMA") end query_value("SELECT setval(#{quote(quoted_sequence)}, #{max_pk ? max_pk : minvalue}, #{max_pk ? true : false})", "SCHEMA") end end |
#schema_creation ⇒ Object
224 225 226 |
# File 'lib/active_record/connection_adapters/cockroachdb/schema_statements.rb', line 224 def schema_creation CockroachDB::SchemaCreation.new(self) end |
#spatial_column_info(table_name) ⇒ Object
memoize hash of column infos for tables
215 216 217 218 |
# File 'lib/active_record/connection_adapters/cockroachdb/schema_statements.rb', line 215 def spatial_column_info(table_name) @spatial_column_info ||= {} @spatial_column_info[table_name.to_sym] ||= SpatialColumnInfo.new(self, table_name.to_s) end |
#type_to_sql(type, limit: nil, precision: nil, scale: nil, array: nil) ⇒ Object
CockroachDB will use INT8 if the SQL type is INTEGER, so we make it use INT4 explicitly when needed.
For spatial columns, include the limit to properly format the column name since type alone is not enough to format the column. Ex. type_to_sql(:geography, limit: “Point,4326”)
> “geography(Point,4326)”
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/active_record/connection_adapters/cockroachdb/schema_statements.rb', line 144 def type_to_sql(type, limit: nil, precision: nil, scale: nil, array: nil, **) # :nodoc: sql = \ case type.to_s when "integer" case limit when nil; "int" when 1, 2; "int2" when 3, 4; "int4" when 5..8; "int8" else super end when "geometry", "geography" "#{type}(#{limit})" else super end # The call to super might have appeneded [] already. if array && type != :primary_key && !sql.end_with?("[]") sql = "#{sql}[]" end sql end |