From b6a8a092a2b284fad6e59626330b0444900ebfaa Mon Sep 17 00:00:00 2001 From: Ben Johnson Date: Mon, 25 Jan 2021 19:21:59 -0500 Subject: [PATCH 1/3] Eliminate the need for search_path logic The PostgreSQL search_path logic that this commit removes was added with the intention of enabling support for a schema named anything other than "public". While the theory was sound, the implementation didn't take into account the behavior in databases in which *multiple* schemas exist. In multi-schema databases, the list of tables for which data should not be dumped was incorrect, leading to unexpected behavior. This revised approach takes advantage of PostgreSQL's support for pattern-based object references when specifying the list of tables for which data should not be dumped, and eliminates the need to perform complex search_path parsing altogether. The attendant Pull Request documentation explains how this technique works in detail. --- src/Illuminate/Database/Schema/PostgresSchemaState.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/Illuminate/Database/Schema/PostgresSchemaState.php b/src/Illuminate/Database/Schema/PostgresSchemaState.php index 7231118f1899..a6cc5e4cc763 100644 --- a/src/Illuminate/Database/Schema/PostgresSchemaState.php +++ b/src/Illuminate/Database/Schema/PostgresSchemaState.php @@ -16,16 +16,12 @@ class PostgresSchemaState extends SchemaState */ public function dump(Connection $connection, $path) { - $schema = $connection->getConfig('schema', 'public'); - - $schema = $schema === 'public' ? '' : $schema.'.'; - $excludedTables = collect($connection->getSchemaBuilder()->getAllTables()) ->map->tablename ->reject(function ($table) { return $table === $this->migrationTable; - })->map(function ($table) use ($schema) { - return '--exclude-table-data='.$schema.$table; + })->map(function ($table) { + return "--exclude-table-data=*.$table"; })->implode(' '); $this->makeProcess( From b5de1f05b79ab3995b1d96cf7b7b74160b0ae89b Mon Sep 17 00:00:00 2001 From: Ben Johnson Date: Tue, 26 Jan 2021 13:30:08 -0500 Subject: [PATCH 2/3] Re-implement pg_restore fix that was reverted https://github.com/laravel/framework/issues/36054 was fixed in a previous commit, but appears to have been lost during subsequent edits in https://github.com/laravel/framework/commit/7be50a511955dea2bf4d6e30208b6fbf07eaa36e . This commit restores the changes made in https://github.com/laravel/framework/commit/502e75b2c8a4469363ed03adc974a5194e820ba7 . Fixes #36054 --- src/Illuminate/Database/Schema/PostgresSchemaState.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Schema/PostgresSchemaState.php b/src/Illuminate/Database/Schema/PostgresSchemaState.php index a6cc5e4cc763..96b13133a6d8 100644 --- a/src/Illuminate/Database/Schema/PostgresSchemaState.php +++ b/src/Illuminate/Database/Schema/PostgresSchemaState.php @@ -39,7 +39,7 @@ public function dump(Connection $connection, $path) */ public function load($path) { - $command = 'PGPASSWORD=$LARAVEL_LOAD_PASSWORD pg_restore --no-owner --no-acl --host=$LARAVEL_LOAD_HOST --port=$LARAVEL_LOAD_PORT --username=$LARAVEL_LOAD_USER --dbname=$LARAVEL_LOAD_DATABASE $LARAVEL_LOAD_PATH'; + $command = 'PGPASSWORD=$LARAVEL_LOAD_PASSWORD pg_restore --no-owner --no-acl --clean --if-exists --host=$LARAVEL_LOAD_HOST --port=$LARAVEL_LOAD_PORT --username=$LARAVEL_LOAD_USER --dbname=$LARAVEL_LOAD_DATABASE $LARAVEL_LOAD_PATH'; if (Str::endsWith($path, '.sql')) { $command = 'PGPASSWORD=$LARAVEL_LOAD_PASSWORD psql --file=$LARAVEL_LOAD_PATH --host=$LARAVEL_LOAD_HOST --port=$LARAVEL_LOAD_PORT --username=$LARAVEL_LOAD_USER --dbname=$LARAVEL_LOAD_DATABASE'; From aef571551cf8eae67f712408fce9048f4641b303 Mon Sep 17 00:00:00 2001 From: Ben Johnson Date: Tue, 26 Jan 2021 14:41:30 -0500 Subject: [PATCH 3/3] Fix PostgreSQL object reference pattern quoting While not required in a psql interactive terminal, this pattern requires outer double-quotes to function as intended when passed as a CLI argument. While simple in this specific instance, pattern quoting can grow complicated (depending on the pattern), but is well explained in the PostgreSQL manual: https://www.postgresql.org/docs/current/app-psql.html#APP-PSQL-PATTERNS --- src/Illuminate/Database/Schema/PostgresSchemaState.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Schema/PostgresSchemaState.php b/src/Illuminate/Database/Schema/PostgresSchemaState.php index 96b13133a6d8..c844ec542e01 100644 --- a/src/Illuminate/Database/Schema/PostgresSchemaState.php +++ b/src/Illuminate/Database/Schema/PostgresSchemaState.php @@ -21,7 +21,7 @@ public function dump(Connection $connection, $path) ->reject(function ($table) { return $table === $this->migrationTable; })->map(function ($table) { - return "--exclude-table-data=*.$table"; + return '--exclude-table-data="*.'.$table.'"'; })->implode(' '); $this->makeProcess(