8000 [8.x] Fix issues with dumping PostgreSQL databases that contain multi… · laravel/framework@306826f · GitHub
[go: up one dir, main page]

Skip to content

Commit 306826f

Browse files
authored
[8.x] Fix issues with dumping PostgreSQL databases that contain multiple schemata (#36046)
* 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. * Re-implement pg_restore fix that was reverted #36054 was fixed in a previous commit, but appears to have been lost during subsequent edits in 7be50a5 . This commit restores the changes made in 502e75b . Fixes #36054 * 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
1 parent 15c1ddd commit 306826f

File tree

1 file changed

+3
-7
lines changed

1 file changed

+3
-7
lines changed

src/Illuminate/Database/Schema/PostgresSchemaState.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,12 @@ class PostgresSchemaState extends SchemaState
1616
*/
1717
public function dump(Connection $connection, $path)
1818
{
19-
$schema = $connection->getConfig('schema', 'public');
20-
21-
$schema = $schema === 'public' ? '' : $schema.'.';
22-
2319
$excludedTables = collect($connection->getSchemaBuilder()->getAllTables())
2420
->map->tablename
2521
->reject(function ($table) {
2622
return $table === $this->migrationTable;
27-
})->map(function ($table) use ($schema) {
28-
return '--exclude-table-data='.$schema.$table;
23+
})->map(function ($table) {
24+
return '--exclude-table-data="*.'.$table.'"';
2925
})->implode(' ');
3026

3127
$this->makeProcess(
@@ -43,7 +39,7 @@ public function dump(Connection $connection, $path)
4339
*/
4440
public function load($path)
4541
{
46-
$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';
42+
$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';
4743

4844
if (Str::endsWith($path, '.sql')) {
4945
$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';

0 commit comments

Comments
 (0)
0