Skip to content

Commit bbb320a

Browse files
BuonOmorafiss
authored andcommitted
feat: support schema search path
Fixes #276
1 parent 41fff74 commit bbb320a

File tree

7 files changed

+109
-87
lines changed

7 files changed

+109
-87
lines changed

lib/active_record/connection_adapters/cockroachdb/database_tasks.rb

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,37 +10,44 @@ def structure_dump(filename, extra_flags=nil)
1010
"https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/new"
1111
end
1212

13-
case ActiveRecord.dump_schemas
14-
when :all, String
15-
raise "Custom schemas are not supported in CockroachDB. " \
16-
"See https://github.com/cockroachdb/cockroach/issues/26443."
17-
when :schema_search_path
18-
if configuration_hash[:schema_search_path]
19-
raise "Custom schemas are not supported in CockroachDB. " \
20-
"See https://github.com/cockroachdb/cockroach/issues/26443."
13+
# "See https://github.com/cockroachdb/cockroach/issues/26443."
14+
search_path =
15+
case ActiveRecord.dump_schemas
16+
when :schema_search_path
17+
configuration_hash[:schema_search_path]
18+
when :all
19+
nil
20+
when String
21+
ActiveRecord.dump_schemas
2122
end
22-
end
2323

2424
conn = ActiveRecord::Base.connection
25-
File.open(filename, "w") do |file|
26-
%w(SCHEMAS TYPES).each do |object_kind|
27-
ActiveRecord::Base.connection.execute("SHOW CREATE ALL #{object_kind}").each_row { file.puts _1 }
28-
end
25+
begin
26+
old_search_path = conn.schema_search_path
27+
conn.schema_search_path = search_path
28+
File.open(filename, "w") do |file|
29+
%w(SCHEMAS TYPES).each do |object_kind|
30+
ActiveRecord::Base.connection.execute("SHOW CREATE ALL #{object_kind}").each_row { file.puts _1 }
31+
end
2932

30-
ignore_tables = ActiveRecord::SchemaDumper.ignore_tables.to_set
33+
ignore_tables = ActiveRecord::SchemaDumper.ignore_tables.to_set
3134

32-
conn.execute("SHOW CREATE ALL TABLES").each_row do |(sql)|
33-
if sql.start_with?("CREATE")
34-
table_name = sql[/CREATE TABLE (?:.*?\.)?\"?(.*?)[\" ]/, 1]
35-
next if ignore_tables.member?(table_name)
36-
elsif sql.start_with?("ALTER")
37-
table_name = sql[/ALTER TABLE (?:.*?\.)?\"?(.*?)[\" ]/, 1]
38-
ref_table_name = sql[/REFERENCES (?:.*?\.)?\"?(.*?)[\" ]/, 1]
39-
next if ignore_tables.member?(table_name) || ignore_tables.member?(ref_table_name)
40-
end
35+
conn.execute("SHOW CREATE ALL TABLES").each_row do |(sql)|
36+
if sql.start_with?("CREATE")
37+
table_name = sql[/CREATE TABLE (?:.*?\.)?\"?(.*?)[\" ]/, 1]
38+
next if ignore_tables.member?(table_name)
39+
elsif sql.start_with?("ALTER")
40+
table_name = sql[/ALTER TABLE (?:.*?\.)?\"?(.*?)[\" ]/, 1]
41+
ref_table_name = sql[/REFERENCES (?:.*?\.)?\"?(.*?)[\" ]/, 1]
42+
next if ignore_tables.member?(table_name) || ignore_tables.member?(ref_table_name)
43+
end
4144

42-
file.puts sql
45+
file.puts sql
46+
end
47+
file.puts "SET seach_path TO #{conn.schema_search_path};\n\n"
4348
end
49+
ensure
50+
conn.schema_search_path = old_search_path
4451
end
4552
end
4653

lib/active_record/connection_adapters/cockroachdb_adapter.rb

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -517,14 +517,8 @@ def column_definitions(table_name)
517517

518518
# Use regex comparison because if a type is an array it will
519519
# have [] appended to the end of it.
520-
target_types = [
521-
/geometry/,
522-
/geography/,
523-
/interval/,
524-
/numeric/
525-
]
526-
527-
re = Regexp.union(target_types)
520+
re = /\A(?:geometry|geography|interval|numeric)/
521+
528522
fields.map do |field|
529523
dtype = field[1]
530524
field[1] = crdb_fields[field[0]][2].downcase if re.match(dtype)
@@ -549,11 +543,14 @@ def column_definitions(table_name)
549543
# precision, and scale information in the type.
550544
# Ex. geometry -> geometry(point, 4326)
551545
def crdb_column_definitions(table_name)
546+
table_name = PostgreSQL::Utils.extract_schema_qualified_name(table_name)
547+
table = table_name.identifier
548+
with_schema = " AND c.table_schema = #{quote(table_name.schema)}" if table_name.schema
552549
fields = \
553550
query(<<~SQL, "SCHEMA")
554551
SELECT c.column_name, c.column_comment, c.crdb_sql_type, c.is_hidden::BOOLEAN
555-
FROM information_schema.columns c
556-
WHERE c.table_name = #{quote(table_name)}
552+
FROM information_schema.columns c
553+
WHERE c.table_name = #{quote(table)}#{with_schema}
557554
SQL
558555

559556
fields.reduce({}) do |a, e|
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
exclude :test_text_defaults_in_new_schema_when_overriding_domain, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443."
2-
exclude :test_string_defaults_in_new_schema_when_overriding_domain, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443."
3-
exclude :test_decimal_defaults_in_new_schema_when_overriding_domain, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443."
4-
exclude :test_bpchar_defaults_in_new_schema_when_overriding_domain, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443."
5-
exclude :test_text_defaults_after_updating_column_default, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443."
6-
exclude :test_default_containing_quote_and_colons, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443."
1+
message = "Custom domains are not supported. See https://github.com/cockroachdb/cockroach/issues/27796"
2+
exclude :test_text_defaults_in_new_schema_when_overriding_domain, message
3+
exclude :test_string_defaults_in_new_schema_when_overriding_domain, message
4+
exclude :test_decimal_defaults_in_new_schema_when_overriding_domain, message
5+
exclude :test_bpchar_defaults_in_new_schema_when_overriding_domain, message
6+
exclude :test_text_defaults_after_updating_column_default, message
7+
exclude :test_default_containing_quote_and_colons, message
Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
exclude :test_schema_invisible, "Custom schemas (and schema authorizations) are not supported by CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443."
2-
exclude :test_session_auth=, "Custom schemas (and schema authorizations) are not supported by CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443."
3-
exclude :test_setting_auth_clears_stmt_cache, "Custom schemas (and schema authorizations) are not supported by CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443."
4-
exclude :test_auth_with_bind, "Custom schemas (and schema authorizations) are not supported by CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443."
5-
exclude :test_sequence_schema_caching, "Custom schemas (and schema authorizations) are not supported by CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443."
6-
exclude :test_tables_in_current_schemas, "Custom schemas (and schema authorizations) are not supported by CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443."
1+
message = 'CockroachDB does not support variable session authorization. ' \
2+
'See https://github.com/cockroachdb/cockroach/issues/40283'
3+
exclude :test_sequence_schema_caching, message
4+
exclude :test_session_auth=, message
5+
exclude :test_schema_invisible, message
6+
exclude :test_schema_invisible, message
7+
exclude :test_tables_in_current_schemas, message
8+
exclude :test_tables_in_current_schemas, message
9+
exclude :test_auth_with_bind, message
10+
exclude :test_auth_with_bind, message
11+
exclude :test_setting_auth_clears_stmt_cache, message
12+
exclude :test_setting_auth_clears_stmt_cache, message

test/excludes/SchemaForeignKeyTest.rb

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/excludes/SchemaTest.rb

Lines changed: 52 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,52 @@
1-
exclude :test_schema_names, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443."
2-
exclude :test_create_schema, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443."
3-
exclude :test_raise_create_schema_with_existing_schema, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443."
4-
exclude :test_drop_schema, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443."
5-
exclude :test_drop_schema_if_exists, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443."
6-
exclude :test_habtm_table_name_with_schema, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443."
7-
exclude :test_drop_schema_with_nonexisting_schema, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443."
8-
exclude :test_raise_wrapped_exception_on_bad_prepare, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443."
9-
exclude :test_schema_change_with_prepared_stmt, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443."
10-
exclude :test_data_source_exists?, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443."
11-
exclude :test_data_source_exists_when_on_schema_search_path, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443."
12-
exclude :test_data_source_exists_when_not_on_schema_search_path, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443."
13-
exclude :test_data_source_exists_wrong_schema, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443."
14-
exclude :test_data_source_exists_quoted_names, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443."
15-
exclude :test_data_source_exists_quoted_table, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443."
16-
exclude :test_with_schema_prefixed_table_name, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443."
17-
exclude :test_with_schema_prefixed_capitalized_table_name, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443."
18-
exclude :test_with_schema_search_path, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443."
19-
exclude :test_proper_encoding_of_table_name, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443."
20-
exclude :test_classes_with_qualified_schema_name, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443."
21-
exclude :test_raise_on_unquoted_schema_name, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443."
22-
exclude :test_without_schema_search_path, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443."
23-
exclude :test_ignore_nil_schema_search_path, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443."
24-
exclude :test_index_name_exists, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443."
25-
exclude :test_dump_indexes_for_schema_one, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443."
26-
exclude :test_dump_indexes_for_schema_two, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443."
27-
exclude :test_dump_indexes_for_schema_multiple_schemas_in_search_path, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443."
28-
exclude :test_dump_indexes_for_table_with_scheme_specified_in_name, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443."
29-
exclude :test_with_uppercase_index_name, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443."
30-
exclude :test_remove_index_when_schema_specified, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443."
31-
exclude :test_primary_key_with_schema_specified, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443."
32-
exclude :test_primary_key_assuming_schema_search_path, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443."
33-
exclude :test_pk_and_sequence_for_with_schema_specified, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443."
34-
exclude :test_current_schema, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443."
35-
exclude :test_prepared_statements_with_multiple_schemas, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443."
36-
exclude :test_schema_exists?, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443."
37-
exclude :test_reset_pk_sequence, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443."
38-
exclude :test_set_pk_sequence, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443."
1+
exclude :test_set_pk_sequence, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
2+
exclude :test_pk_and_sequence_for_with_schema_specified, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
3+
4+
# This hack allows us to redefine the
5+
# setup. We are using various weird
6+
# routes that might be simplified:
7+
# 1. We use `Ext` to prepend to the
8+
# current class to be sure `#setup`
9+
# is overriden.
10+
# 2. Current is a reference to self to
11+
# access it in `Ext`
12+
# 3. const_missing is used to avoid
13+
# having to rewrite every constant.
14+
Current = self
15+
module Ext
16+
def self.const_missing(const)
17+
Current.const_get(const)
18+
end
19+
20+
def setup
21+
@connection = ActiveRecord::Base.connection
22+
@connection.execute "SET default_int_size = 4"
23+
@connection.execute "SET serial_normalization = sql_sequence_cached"
24+
@connection.execute "CREATE SCHEMA #{SCHEMA_NAME}"
25+
@connection.execute "CREATE TABLE #{SCHEMA_NAME}.#{TABLE_NAME} (#{COLUMNS.join(',')})"
26+
@connection.execute "CREATE TABLE #{SCHEMA_NAME}.\"#{TABLE_NAME}.table\" (#{COLUMNS.join(',')})"
27+
@connection.execute "CREATE TABLE #{SCHEMA_NAME}.\"#{CAPITALIZED_TABLE_NAME}\" (#{COLUMNS.join(',')})"
28+
@connection.execute "CREATE SCHEMA #{SCHEMA2_NAME}"
29+
@connection.execute "CREATE TABLE #{SCHEMA2_NAME}.#{TABLE_NAME} (#{COLUMNS.join(',')})"
30+
@connection.execute "CREATE INDEX #{INDEX_A_NAME} ON #{SCHEMA_NAME}.#{TABLE_NAME} USING btree (#{INDEX_A_COLUMN});"
31+
@connection.execute "CREATE INDEX #{INDEX_A_NAME} ON #{SCHEMA2_NAME}.#{TABLE_NAME} USING btree (#{INDEX_A_COLUMN});"
32+
@connection.execute "CREATE INDEX #{INDEX_B_NAME} ON #{SCHEMA_NAME}.#{TABLE_NAME} USING btree (#{INDEX_B_COLUMN_S1});"
33+
@connection.execute "CREATE INDEX #{INDEX_B_NAME} ON #{SCHEMA2_NAME}.#{TABLE_NAME} USING btree (#{INDEX_B_COLUMN_S2});"
34+
@connection.execute "CREATE INDEX #{INDEX_C_NAME} ON #{SCHEMA_NAME}.#{TABLE_NAME} USING gin (#{INDEX_C_COLUMN});"
35+
@connection.execute "CREATE INDEX #{INDEX_C_NAME} ON #{SCHEMA2_NAME}.#{TABLE_NAME} USING gin (#{INDEX_C_COLUMN});"
36+
@connection.execute "CREATE INDEX #{INDEX_D_NAME} ON #{SCHEMA_NAME}.#{TABLE_NAME} USING btree (#{INDEX_D_COLUMN} DESC);"
37+
@connection.execute "CREATE INDEX #{INDEX_D_NAME} ON #{SCHEMA2_NAME}.#{TABLE_NAME} USING btree (#{INDEX_D_COLUMN} DESC);"
38+
@connection.execute "CREATE INDEX #{INDEX_E_NAME} ON #{SCHEMA_NAME}.#{TABLE_NAME} USING gin (#{INDEX_E_COLUMN});"
39+
@connection.execute "CREATE INDEX #{INDEX_E_NAME} ON #{SCHEMA2_NAME}.#{TABLE_NAME} USING gin (#{INDEX_E_COLUMN});"
40+
@connection.execute "CREATE TABLE #{SCHEMA_NAME}.#{PK_TABLE_NAME} (id serial primary key)"
41+
@connection.execute "CREATE TABLE #{SCHEMA2_NAME}.#{PK_TABLE_NAME} (id serial primary key)"
42+
@connection.execute "CREATE SEQUENCE #{SCHEMA_NAME}.#{UNMATCHED_SEQUENCE_NAME}"
43+
@connection.execute "CREATE TABLE #{SCHEMA_NAME}.#{UNMATCHED_PK_TABLE_NAME} (id integer NOT NULL DEFAULT nextval('#{SCHEMA_NAME}.#{UNMATCHED_SEQUENCE_NAME}'::regclass), CONSTRAINT unmatched_pkey PRIMARY KEY (id))"
44+
end
45+
46+
def teardown
47+
@connection.execute "SET default_int_size = DEFAULT"
48+
@connection.execute "SET serial_normalization = DEFAULT"
49+
super
50+
end
51+
end
52+
prepend Ext

test/excludes/SchemaWithDotsTest.rb

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

0 commit comments

Comments
 (0)