From 9f317bba26379e7dd9e61aefd7ad0a9caf6dc79d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Gonz=C3=A1lez?= <2051199+rbngzlv@users.noreply.github.com> Date: Thu, 28 Aug 2025 10:48:33 +0200 Subject: [PATCH 1/2] switch to psql when importing database schema This approach avoids the need to explicitly blacklist `\restrict` and `\unrestrict` psql meta-commands issued in newer `pg_dump` versions, and should increase resilience to future changes in `pg_dump` output. --- lib/apartment/adapters/postgresql_adapter.rb | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/apartment/adapters/postgresql_adapter.rb b/lib/apartment/adapters/postgresql_adapter.rb index 1c41b49f..9a74a4d8 100644 --- a/lib/apartment/adapters/postgresql_adapter.rb +++ b/lib/apartment/adapters/postgresql_adapter.rb @@ -175,14 +175,28 @@ def preserving_search_path # def clone_pg_schema pg_schema_sql = patch_search_path(pg_dump_schema) - Apartment.connection.execute(pg_schema_sql) + + # Create a temporary file to store the SQL + Tempfile.create do |temp_file| + temp_file.write(pg_schema_sql) + temp_file.close # Close the file so psql can read it + + with_pg_env { `psql -d #{dbname} -f #{temp_file.path}` } + end end # Copy data from schema_migrations into new schema # def copy_schema_migrations pg_migrations_data = patch_search_path(pg_dump_schema_migrations_data) - Apartment.connection.execute(pg_migrations_data) + + # Create a temporary file to store the SQL + Tempfile.create do |temp_file| + temp_file.write(pg_migrations_data) + temp_file.close # Close the file so psql can read it + + with_pg_env { `psql -d #{dbname} -f #{temp_file.path}` } + end end # Dump postgres default schema From 468c14d49ab582b04e899b066ab509a9556ef365 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Gonz=C3=A1lez?= <2051199+rbngzlv@users.noreply.github.com> Date: Thu, 28 Aug 2025 12:01:33 +0200 Subject: [PATCH 2/2] use system instead of backticks for psql calls `system` passes command line parameters safely --- lib/apartment/adapters/postgresql_adapter.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/apartment/adapters/postgresql_adapter.rb b/lib/apartment/adapters/postgresql_adapter.rb index 9a74a4d8..4c4770e9 100644 --- a/lib/apartment/adapters/postgresql_adapter.rb +++ b/lib/apartment/adapters/postgresql_adapter.rb @@ -181,7 +181,7 @@ def clone_pg_schema temp_file.write(pg_schema_sql) temp_file.close # Close the file so psql can read it - with_pg_env { `psql -d #{dbname} -f #{temp_file.path}` } + with_pg_env { system("psql", "-q", "-d", dbname, "-f", temp_file.path) } end end @@ -195,7 +195,7 @@ def copy_schema_migrations temp_file.write(pg_migrations_data) temp_file.close # Close the file so psql can read it - with_pg_env { `psql -d #{dbname} -f #{temp_file.path}` } + with_pg_env { system("psql", "-q", "-d", dbname, "-f", temp_file.path) } end end