From 5f6efdab0f9acef8d04ca9a018667bfcd1cf59b5 Mon Sep 17 00:00:00 2001 From: Lachlan Sylvester Date: Wed, 6 Sep 2023 18:42:29 +1000 Subject: [PATCH] dump PostgreSQL schemas as part of the schema dump --- activerecord/CHANGELOG.md | 4 ++++ .../connection_adapters/postgresql/schema_dumper.rb | 11 +++++++++++ activerecord/lib/active_record/schema_dumper.rb | 5 +++++ .../test/cases/adapters/postgresql/schema_test.rb | 9 +++++++++ 4 files changed, 29 insertions(+) diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index d47582528c..271be0c676 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,7 @@ +* Dump PostgreSQL schemas as part of the schema dump. + + *Lachlan Sylvester* + ## Rails 7.1.0.beta1 (September 13, 2023) ## * Encryption now supports `support_unencrypted_data` being set per-attribute. diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/schema_dumper.rb b/activerecord/lib/active_record/connection_adapters/postgresql/schema_dumper.rb index a565be72a2..c33f686fba 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/schema_dumper.rb @@ -28,6 +28,17 @@ def types(stream) end end + def schemas(stream) + schema_names = @connection.schema_names - ["public"] + + if schema_names.any? + schema_names.sort.each do |name| + stream.puts " create_schema #{name.inspect}" + end + stream.puts + end + end + def exclusion_constraints_in_create(table, stream) if (exclusion_constraints = @connection.exclusion_constraints(table)).any? add_exclusion_constraint_statements = exclusion_constraints.map do |exclusion_constraint| diff --git a/activerecord/lib/active_record/schema_dumper.rb b/activerecord/lib/active_record/schema_dumper.rb index 50f87d9f08..e80266200e 100644 --- a/activerecord/lib/active_record/schema_dumper.rb +++ b/activerecord/lib/active_record/schema_dumper.rb @@ -57,6 +57,7 @@ def generate_options(config) def dump(stream) header(stream) + schemas(stream) extensions(stream) types(stream) tables(stream) @@ -119,6 +120,10 @@ def extensions(stream) def types(stream) end + # schemas are only supported by PostgreSQL + def schemas(stream) + end + def tables(stream) sorted_tables = @connection.tables.sort diff --git a/activerecord/test/cases/adapters/postgresql/schema_test.rb b/activerecord/test/cases/adapters/postgresql/schema_test.rb index 2053e97bd5..e52ee907be 100644 --- a/activerecord/test/cases/adapters/postgresql/schema_test.rb +++ b/activerecord/test/cases/adapters/postgresql/schema_test.rb @@ -17,6 +17,7 @@ def with_schema_search_path(schema_search_path) class SchemaTest < ActiveRecord::PostgreSQLTestCase include PGSchemaHelper + include SchemaDumpingHelper self.use_transactional_tests = false SCHEMA_NAME = "test_schema" @@ -487,6 +488,14 @@ def test_rename_index assert @connection.index_name_exists?("#{SCHEMA_NAME}.#{TABLE_NAME}", new_name) end + def test_dumping_schemas + output = dump_all_table_schema(/./) + + assert_no_match %r{create_schema "public"}, output + assert_match %r{create_schema "test_schema"}, output + assert_match %r{create_schema "test_schema2"}, output + end + private def columns(table_name) @connection.send(:column_definitions, table_name).map do |name, type, default|