From a9c7961322c78866e4abe996cc9f3aee3dddfd80 Mon Sep 17 00:00:00 2001 From: Ulysse Buonomo Date: Sat, 2 Sep 2023 10:13:07 -0500 Subject: [PATCH] feat: support schema search path --- .../cockroachdb/database_tasks.rb | 55 +++++++++++-------- test/cases/adapters/postgresql/schema_test.rb | 36 ++++++++++++ ...faultsUsingMultipleSchemasAndDomainTest.rb | 6 -- test/excludes/SchemaAuthorizationTest.rb | 6 -- test/excludes/SchemaForeignKeyTest.rb | 1 - test/excludes/SchemaTest.rb | 42 ++------------ test/excludes/SchemaWithDotsTest.rb | 2 - 7 files changed, 71 insertions(+), 77 deletions(-) create mode 100644 test/cases/adapters/postgresql/schema_test.rb delete mode 100644 test/excludes/DefaultsUsingMultipleSchemasAndDomainTest.rb delete mode 100644 test/excludes/SchemaAuthorizationTest.rb delete mode 100644 test/excludes/SchemaForeignKeyTest.rb delete mode 100644 test/excludes/SchemaWithDotsTest.rb diff --git a/lib/active_record/connection_adapters/cockroachdb/database_tasks.rb b/lib/active_record/connection_adapters/cockroachdb/database_tasks.rb index 130fb235..42c96c0d 100644 --- a/lib/active_record/connection_adapters/cockroachdb/database_tasks.rb +++ b/lib/active_record/connection_adapters/cockroachdb/database_tasks.rb @@ -10,37 +10,44 @@ def structure_dump(filename, extra_flags=nil) "https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/new" end - case ActiveRecord.dump_schemas - when :all, String - raise "Custom schemas are not supported in CockroachDB. " \ - "See https://github.com/cockroachdb/cockroach/issues/26443." - when :schema_search_path - if configuration_hash[:schema_search_path] - raise "Custom schemas are not supported in CockroachDB. " \ - "See https://github.com/cockroachdb/cockroach/issues/26443." + # "See https://github.com/cockroachdb/cockroach/issues/26443." + search_path = + case ActiveRecord.dump_schemas + when :schema_search_path + configuration_hash[:schema_search_path] + when :all + nil + when String + ActiveRecord.dump_schemas end - end conn = ActiveRecord::Base.connection - File.open(filename, "w") do |file| - %w(SCHEMAS TYPES).each do |object_kind| - ActiveRecord::Base.connection.execute("SHOW CREATE ALL #{object_kind}").each_row { file.puts _1 } - end + begin + old_search_path = conn.schema_search_path + conn.schema_search_path = search_path + File.open(filename, "w") do |file| + %w(SCHEMAS TYPES).each do |object_kind| + ActiveRecord::Base.connection.execute("SHOW CREATE ALL #{object_kind}").each_row { file.puts _1 } + end - ignore_tables = ActiveRecord::SchemaDumper.ignore_tables.to_set + ignore_tables = ActiveRecord::SchemaDumper.ignore_tables.to_set - conn.execute("SHOW CREATE ALL TABLES").each_row do |(sql)| - if sql.start_with?("CREATE") - table_name = sql[/CREATE TABLE (?:.*?\.)?\"?(.*?)[\" ]/, 1] - next if ignore_tables.member?(table_name) - elsif sql.start_with?("ALTER") - table_name = sql[/ALTER TABLE (?:.*?\.)?\"?(.*?)[\" ]/, 1] - ref_table_name = sql[/REFERENCES (?:.*?\.)?\"?(.*?)[\" ]/, 1] - next if ignore_tables.member?(table_name) || ignore_tables.member?(ref_table_name) - end + conn.execute("SHOW CREATE ALL TABLES").each_row do |(sql)| + if sql.start_with?("CREATE") + table_name = sql[/CREATE TABLE (?:.*?\.)?\"?(.*?)[\" ]/, 1] + next if ignore_tables.member?(table_name) + elsif sql.start_with?("ALTER") + table_name = sql[/ALTER TABLE (?:.*?\.)?\"?(.*?)[\" ]/, 1] + ref_table_name = sql[/REFERENCES (?:.*?\.)?\"?(.*?)[\" ]/, 1] + next if ignore_tables.member?(table_name) || ignore_tables.member?(ref_table_name) + end - file.puts sql + file.puts sql + end + file.puts "SET seach_path TO #{conn.schema_search_path};\n\n" end + ensure + conn.schema_search_path = old_search_path end end diff --git a/test/cases/adapters/postgresql/schema_test.rb b/test/cases/adapters/postgresql/schema_test.rb new file mode 100644 index 00000000..30c75887 --- /dev/null +++ b/test/cases/adapters/postgresql/schema_test.rb @@ -0,0 +1,36 @@ +require "cases/helper" +require "support/paths_cockroachdb" + +require File.expand_path( + "cases/adapters/postgresql/schema_test.rb", + ARTest::CockroachDB.root_activerecord_test +) + +# See: +# - https://www.cockroachlabs.com/docs/stable/create-schema +# - https://www.postgresql.org/docs/current/sql-createschema.html +class SchemaTest < ActiveRecord::PostgreSQLTestCase + def setup + @connection = ActiveRecord::Base.connection + @connection.execute "CREATE SCHEMA #{SCHEMA_NAME}" + @connection.execute "CREATE TABLE #{SCHEMA_NAME}.#{TABLE_NAME} (#{COLUMNS.join(',')})" + @connection.execute "CREATE TABLE #{SCHEMA_NAME}.\"#{TABLE_NAME}.table\" (#{COLUMNS.join(',')})" + @connection.execute "CREATE TABLE #{SCHEMA_NAME}.\"#{CAPITALIZED_TABLE_NAME}\" (#{COLUMNS.join(',')})" + @connection.execute "CREATE SCHEMA #{SCHEMA2_NAME}" + @connection.execute "CREATE TABLE #{SCHEMA2_NAME}.#{TABLE_NAME} (#{COLUMNS.join(',')})" + @connection.execute "CREATE INDEX #{INDEX_A_NAME} ON #{SCHEMA_NAME}.#{TABLE_NAME} USING btree (#{INDEX_A_COLUMN});" + @connection.execute "CREATE INDEX #{INDEX_A_NAME} ON #{SCHEMA2_NAME}.#{TABLE_NAME} USING btree (#{INDEX_A_COLUMN});" + @connection.execute "CREATE INDEX #{INDEX_B_NAME} ON #{SCHEMA_NAME}.#{TABLE_NAME} USING btree (#{INDEX_B_COLUMN_S1});" + @connection.execute "CREATE INDEX #{INDEX_B_NAME} ON #{SCHEMA2_NAME}.#{TABLE_NAME} USING btree (#{INDEX_B_COLUMN_S2});" + @connection.execute "CREATE INDEX #{INDEX_C_NAME} ON #{SCHEMA_NAME}.#{TABLE_NAME} USING gin (#{INDEX_C_COLUMN});" + @connection.execute "CREATE INDEX #{INDEX_C_NAME} ON #{SCHEMA2_NAME}.#{TABLE_NAME} USING gin (#{INDEX_C_COLUMN});" + @connection.execute "CREATE INDEX #{INDEX_D_NAME} ON #{SCHEMA_NAME}.#{TABLE_NAME} USING btree (#{INDEX_D_COLUMN} DESC);" + @connection.execute "CREATE INDEX #{INDEX_D_NAME} ON #{SCHEMA2_NAME}.#{TABLE_NAME} USING btree (#{INDEX_D_COLUMN} DESC);" + @connection.execute "CREATE INDEX #{INDEX_E_NAME} ON #{SCHEMA_NAME}.#{TABLE_NAME} USING gin (#{INDEX_E_COLUMN});" + @connection.execute "CREATE INDEX #{INDEX_E_NAME} ON #{SCHEMA2_NAME}.#{TABLE_NAME} USING gin (#{INDEX_E_COLUMN});" + @connection.execute "CREATE TABLE #{SCHEMA_NAME}.#{PK_TABLE_NAME} (id serial primary key)" + @connection.execute "CREATE TABLE #{SCHEMA2_NAME}.#{PK_TABLE_NAME} (id serial primary key)" + @connection.execute "CREATE SEQUENCE #{SCHEMA_NAME}.#{UNMATCHED_SEQUENCE_NAME}" + @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))" + end +end diff --git a/test/excludes/DefaultsUsingMultipleSchemasAndDomainTest.rb b/test/excludes/DefaultsUsingMultipleSchemasAndDomainTest.rb deleted file mode 100644 index 4745c812..00000000 --- a/test/excludes/DefaultsUsingMultipleSchemasAndDomainTest.rb +++ /dev/null @@ -1,6 +0,0 @@ -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." -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." -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." -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." -exclude :test_text_defaults_after_updating_column_default, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443." -exclude :test_default_containing_quote_and_colons, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443." diff --git a/test/excludes/SchemaAuthorizationTest.rb b/test/excludes/SchemaAuthorizationTest.rb deleted file mode 100644 index d1b701bb..00000000 --- a/test/excludes/SchemaAuthorizationTest.rb +++ /dev/null @@ -1,6 +0,0 @@ -exclude :test_schema_invisible, "Custom schemas (and schema authorizations) are not supported by CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443." -exclude :test_session_auth=, "Custom schemas (and schema authorizations) are not supported by CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443." -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." -exclude :test_auth_with_bind, "Custom schemas (and schema authorizations) are not supported by CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443." -exclude :test_sequence_schema_caching, "Custom schemas (and schema authorizations) are not supported by CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443." -exclude :test_tables_in_current_schemas, "Custom schemas (and schema authorizations) are not supported by CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443." diff --git a/test/excludes/SchemaForeignKeyTest.rb b/test/excludes/SchemaForeignKeyTest.rb deleted file mode 100644 index 78de0021..00000000 --- a/test/excludes/SchemaForeignKeyTest.rb +++ /dev/null @@ -1 +0,0 @@ -exclude :test_dump_foreign_key_targeting_different_schema, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443." diff --git a/test/excludes/SchemaTest.rb b/test/excludes/SchemaTest.rb index 99e40596..62360292 100644 --- a/test/excludes/SchemaTest.rb +++ b/test/excludes/SchemaTest.rb @@ -1,38 +1,4 @@ -exclude :test_schema_names, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443." -exclude :test_create_schema, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443." -exclude :test_raise_create_schema_with_existing_schema, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443." -exclude :test_drop_schema, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443." -exclude :test_drop_schema_if_exists, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443." -exclude :test_habtm_table_name_with_schema, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443." -exclude :test_drop_schema_with_nonexisting_schema, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443." -exclude :test_raise_wrapped_exception_on_bad_prepare, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443." -exclude :test_schema_change_with_prepared_stmt, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443." -exclude :test_data_source_exists?, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443." -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." -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." -exclude :test_data_source_exists_wrong_schema, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443." -exclude :test_data_source_exists_quoted_names, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443." -exclude :test_data_source_exists_quoted_table, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443." -exclude :test_with_schema_prefixed_table_name, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443." -exclude :test_with_schema_prefixed_capitalized_table_name, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443." -exclude :test_with_schema_search_path, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443." -exclude :test_proper_encoding_of_table_name, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443." -exclude :test_classes_with_qualified_schema_name, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443." -exclude :test_raise_on_unquoted_schema_name, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443." -exclude :test_without_schema_search_path, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443." -exclude :test_ignore_nil_schema_search_path, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443." -exclude :test_index_name_exists, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443." -exclude :test_dump_indexes_for_schema_one, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443." -exclude :test_dump_indexes_for_schema_two, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443." -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." -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." -exclude :test_with_uppercase_index_name, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443." -exclude :test_remove_index_when_schema_specified, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443." -exclude :test_primary_key_with_schema_specified, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443." -exclude :test_primary_key_assuming_schema_search_path, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443." -exclude :test_pk_and_sequence_for_with_schema_specified, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443." -exclude :test_current_schema, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443." -exclude :test_prepared_statements_with_multiple_schemas, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443." -exclude :test_schema_exists?, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443." -exclude :test_reset_pk_sequence, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443." -exclude :test_set_pk_sequence, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443." +instance_methods.grep(/\Atest_/) do + exclude _1, "Re-implementing this test suit due to setup incompatibility. " \ + "See TODO" +end diff --git a/test/excludes/SchemaWithDotsTest.rb b/test/excludes/SchemaWithDotsTest.rb deleted file mode 100644 index 73467561..00000000 --- a/test/excludes/SchemaWithDotsTest.rb +++ /dev/null @@ -1,2 +0,0 @@ -exclude :test_rename_table, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443." -exclude :test_Active_Record_basics, "Custom schemas are not supported in CockroachDB. See https://github.com/cockroachdb/cockroach/issues/26443."