Merge pull request #49164 from lsylvester/dump-postgresql-schemas

dump PostgreSQL schemas as part of the schema dump
This commit is contained in:
Yasuo Honda 2023-09-20 10:00:03 +09:00 committed by GitHub
commit b3df4f25ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 0 deletions

@ -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.

@ -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|

@ -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

@ -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|