From f7ea4e912ce9bacfdb327de4648d07b4c7ee3cb7 Mon Sep 17 00:00:00 2001 From: NickSdot <32384907+NickSdot@users.noreply.github.com> Date: Thu, 20 Jun 2024 00:05:23 +0800 Subject: [PATCH] [10.x] Ensure`schema:dump` will dump the migrations table only if it exists (#51827) * fix: migrations table must only be dumped on default connection * fix: migrations table must only be dumped on default connection * Style * Use `$this->connection->getSchemaBuilder()->hasTable()` instead of `Config::get()`` * Update SchemaState.php --------- Co-authored-by: Taylor Otwell --- src/Illuminate/Database/Schema/MySqlSchemaState.php | 4 +++- src/Illuminate/Database/Schema/PostgresSchemaState.php | 5 ++++- src/Illuminate/Database/Schema/SchemaState.php | 10 ++++++++++ src/Illuminate/Database/Schema/SqliteSchemaState.php | 4 +++- 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Database/Schema/MySqlSchemaState.php b/src/Illuminate/Database/Schema/MySqlSchemaState.php index 2514c18bd6c1..5bed2f003974 100644 --- a/src/Illuminate/Database/Schema/MySqlSchemaState.php +++ b/src/Illuminate/Database/Schema/MySqlSchemaState.php @@ -26,7 +26,9 @@ public function dump(Connection $connection, $path) $this->removeAutoIncrementingState($path); - $this->appendMigrationData($path); + if ($this->hasMigrationTable()) { + $this->appendMigrationData($path); + } } /** diff --git a/src/Illuminate/Database/Schema/PostgresSchemaState.php b/src/Illuminate/Database/Schema/PostgresSchemaState.php index b3f9361bc92b..70ccd25b5fd3 100644 --- a/src/Illuminate/Database/Schema/PostgresSchemaState.php +++ b/src/Illuminate/Database/Schema/PostgresSchemaState.php @@ -17,9 +17,12 @@ public function dump(Connection $connection, $path) { $commands = collect([ $this->baseDumpCommand().' --schema-only > '.$path, - $this->baseDumpCommand().' -t '.$this->migrationTable.' --data-only >> '.$path, ]); + if ($this->hasMigrationTable()) { + $commands->push($this->baseDumpCommand().' -t '.$this->migrationTable.' --data-only >> '.$path); + } + $commands->map(function ($command, $path) { $this->makeProcess($command)->mustRun($this->output, array_merge($this->baseVariables($this->connection->getConfig()), [ 'LARAVEL_LOAD_PATH' => $path, diff --git a/src/Illuminate/Database/Schema/SchemaState.php b/src/Illuminate/Database/Schema/SchemaState.php index 58d9c3a438aa..b7fa34c168c9 100644 --- a/src/Illuminate/Database/Schema/SchemaState.php +++ b/src/Illuminate/Database/Schema/SchemaState.php @@ -94,6 +94,16 @@ public function makeProcess(...$arguments) return call_user_func($this->processFactory, ...$arguments); } + /** + * Determine if the current connection has a migration table. + * + * @return bool + */ + public function hasMigrationTable(): bool + { + return $this->connection->getSchemaBuilder()->hasTable($this->migrationTable); + } + /** * Specify the name of the application's migration table. * diff --git a/src/Illuminate/Database/Schema/SqliteSchemaState.php b/src/Illuminate/Database/Schema/SqliteSchemaState.php index 10efc7c0aba9..4b66542923e4 100644 --- a/src/Illuminate/Database/Schema/SqliteSchemaState.php +++ b/src/Illuminate/Database/Schema/SqliteSchemaState.php @@ -28,7 +28,9 @@ public function dump(Connection $connection, $path) $this->files->put($path, implode(PHP_EOL, $migrations).PHP_EOL); - $this->appendMigrationData($path); + if ($this->hasMigrationTable()) { + $this->appendMigrationData($path); + } } /**