Skip to content

Commit 7f4efb0

Browse files
committed
fix: handle indexes with a hidden compound
This fixes regional related parts, hence fixes #344
1 parent 622adbb commit 7f4efb0

File tree

4 files changed

+39
-6
lines changed

4 files changed

+39
-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/excludes/AttributeMethodsTest.rb

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

0 commit comments

Comments
 (0)