remove crdb_internal columns from model
Currently, if you create a hash sharded index on a table then the active records model will contain crdb_internal shard column inside it. We should in general filter out any hash sharded indexes from the active records model, since these are not user writable. For example if we have the following create table statement:
CREATE TABLE public.t1 (
n INT)
-- crdb_internal_n_shard_16 will exist on this table now
CREATE INDEX ON t1(n) USING HASH
Probably the easiest way of dealing with this is to filter any columns with crdb_internal prefixes, minus crdb_internal_region.
Probably the easiest way of dealing with this is to filter any columns with crdb_internal prefixes, minus crdb_internal_region.
I am not sure if that way is ideal. Instead, we could ignore all columns that are marked as HIDDEN. @BuonOmo made a PR recently that does that https://github.com/cockroachdb/activerecord-cockroachdb-adapter/pull/311.
@fqazi Can you say more about this bug report? Which version was the problematic behavior happening on. Did they have the fix I linked above?
@fqazi @rafiss the HIDDEN way seems nice indeed. But I can also filter out anything, I don't see it being blocking.
Currently, a hidden column still shows up:
ActiveRecord::Base.connection.execute "
CREATE TABLE public.foos (
n INT);
-- crdb_internal_n_shard_16 will exist on this table now
CREATE INDEX ON foos(n) USING HASH;
"
class Foo < ActiveRecord::Base
end
col = Foo.column_for_attribute(:crdb_internal_n_shard_16)
puts "Included in #columns" if Foo.columns.include?(col)
puts ":crdb_internal_n_shard_16 is hidden" if col.hidden?
On implementation
We would have to filter out the columns we want in #column_definitions. Since it is already overriden, this is pretty easy. It's just about what we exclude.
For now, that is what we exclude:
fields.delete_if do |field|
# Don't include rowid column if it is hidden and the primary key
# is not defined (meaning CRDB implicitly created it).
field[0] == CockroachDBAdapter::DEFAULT_PRIMARY_KEY && # field[0] is `attname`
field[10] && !primary_key(table_name) # field[10] is `is_hidden`
end
@fqazi any news about that bug report by any chance ?
@BuonOmo I forgot to circle back on this one. They didn't have the fix Rafi linked above and were on 6.1.11 when they ran into the issue against CRDB 23.2.
@fqazi ok I guess we should be closing this one then