Skip to content

Commit 356e7b4

Browse files
BuonOmorafiss
authored andcommitted
fix: handle indexes with a hidden compound
This fixes regional related parts, hence fixes #344
1 parent 6dde848 commit 356e7b4

File tree

6 files changed

+84
-6
lines changed

6 files changed

+84
-6
lines changed

bin/console

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,22 @@ require "activerecord-cockroachdb-adapter"
1212
# structure_load(Post.connection_db_config, "awesome-file.sql")
1313
require "active_record/connection_adapters/cockroachdb/database_tasks"
1414

15+
DB_NAME = "ar_crdb_console"
16+
1517
schema_kind = ENV.fetch("SCHEMA_KIND", ENV.fetch("SCHEMA", "default"))
1618

17-
system("cockroach sql --insecure --host=localhost:26257 --execute='drop database if exists ar_crdb_console'",
19+
system("cockroach sql --insecure --host=localhost:26257 --execute='drop database if exists #{DB_NAME}'",
1820
exception: true)
19-
system("cockroach sql --insecure --host=localhost:26257 --execute='create database ar_crdb_console'",
21+
system("cockroach sql --insecure --host=localhost:26257 --execute='create database #{DB_NAME}'",
2022
exception: true)
2123

2224
ActiveRecord::Base.establish_connection(
23-
#Alternative version: "cockroachdb://root@localhost:26257/ar_crdb_console"
25+
#Alternative version: "cockroachdb://root@localhost:26257/#{DB_NAME}"
2426
adapter: "cockroachdb",
2527
host: "localhost",
2628
port: 26257,
2729
user: "root",
28-
database: "ar_crdb_console"
30+
database: DB_NAME
2931
)
3032

3133
load "#{__dir__}/console_schemas/#{schema_kind}.rb"

bin/console_schemas/default.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ class Post < ActiveRecord::Base
66
t.string :title
77
t.text :body
88
end
9+
10+
add_index("posts", ["title"], name: "index_posts_on_title", unique: true)
911
end

lib/active_record/connection_adapters/cockroachdb/schema_statements.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,37 @@ def primary_key(table_name)
5555
end
5656
end
5757

58+
def primary_keys(table_name)
59+
return super unless database_version >= 24_02_02
60+
61+
query_values(<<~SQL, "SCHEMA")
62+
SELECT a.attname
63+
FROM (
64+
SELECT indrelid, indkey, generate_subscripts(indkey, 1) idx
65+
FROM pg_index
66+
WHERE indrelid = #{quote(quote_table_name(table_name))}::regclass
67+
AND indisprimary
68+
) i
69+
JOIN pg_attribute a
70+
ON a.attrelid = i.indrelid
71+
AND a.attnum = i.indkey[i.idx]
72+
AND NOT a.attishidden
73+
ORDER BY i.idx
74+
SQL
75+
end
76+
77+
def column_names_from_column_numbers(table_oid, column_numbers)
78+
return super unless database_version >= 24_02_02
79+
80+
Hash[query(<<~SQL, "SCHEMA")].values_at(*column_numbers).compact
81+
SELECT a.attnum, a.attname
82+
FROM pg_attribute a
83+
WHERE a.attrelid = #{table_oid}
84+
AND a.attnum IN (#{column_numbers.join(", ")})
85+
AND NOT a.attishidden
86+
SQL
87+
end
88+
5889
# OVERRIDE: CockroachDB does not support deferrable constraints.
5990
# See: https://go.crdb.dev/issue-v/31632/v23.1
6091
def foreign_key_options(from_table, to_table, options)

test/cases/migration/hidden_column_test.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@ def test_add_hidden_column
5353
output = dump_table_schema "rockets"
5454
assert_match %r{t.uuid "new_col", hidden: true$}, output
5555
end
56+
57+
# Since 24.2.2, hash sharded indexes add a hidden column to the table.
58+
# This tests ensure that the user can still drop the index even if they
59+
# call `#remove_index` with the column name rather than the index name.
60+
def test_remove_index_with_a_hidden_column
61+
@connection.execute <<-SQL
62+
CREATE INDEX hash_idx ON rockets (name) USING HASH WITH (bucket_count=8);
63+
SQL
64+
@connection.remove_index :rockets, :name
65+
# assert :ok
66+
end
5667
end
5768
end
5869
end

test/cases/primary_keys_test.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,38 @@ def test_schema_dump_primary_key_integer_with_default_nil
9797
assert_match %r{create_table "int_defaults", id: :bigint, default: nil}, schema
9898
end
9999
end
100+
101+
class PrimaryKeyHiddenColumnTest < ActiveRecord::TestCase
102+
class Rocket < ActiveRecord::Base
103+
end
104+
105+
def setup
106+
connection = ActiveRecord::Base.lease_connection
107+
connection.execute <<-SQL
108+
CREATE TABLE rockets(
109+
id SERIAL PRIMARY KEY USING HASH WITH (bucket_count=4)
110+
)
111+
SQL
112+
end
113+
114+
def teardown
115+
ActiveRecord::Base.connection.drop_table :rockets
116+
end
117+
118+
def test_to_key_with_hidden_primary_key_part
119+
rocket = Rocket.new
120+
assert_nil rocket.to_key
121+
rocket.save
122+
assert_equal rocket.to_key, [rocket.id]
123+
end
124+
125+
def test_read_attribute_with_hidden_primary_key_part
126+
rocket = Rocket.create!
127+
id = assert_not_deprecated(ActiveRecord.deprecator) do
128+
rocket.read_attribute(:id)
129+
end
130+
131+
assert_equal rocket.id, id
132+
end
133+
end
100134
end

test/excludes/AttributeMethodsTest.rb

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)