diff --git a/src/DBAL/Models/PgSQL/PgSQLView.php b/src/DBAL/Models/PgSQL/PgSQLView.php index 68e3af63..e7378bf5 100644 --- a/src/DBAL/Models/PgSQL/PgSQLView.php +++ b/src/DBAL/Models/PgSQL/PgSQLView.php @@ -16,7 +16,9 @@ protected function handle(DoctrineDBALView $view): void { $this->createViewSQL = $this->makeCreateViewSQL($this->quotedName, $view->getSql()); - if ($view->getNamespaceName() === DB::connection()->getConfig('schema')) { + $searchPath = DB::connection()->getConfig('search_path') ?: DB::connection()->getConfig('schema'); + + if ($view->getNamespaceName() === $searchPath) { // Strip namespace from name. $name = $view->getShortestName($view->getNamespaceName()); $this->name = $this->makeName($name); diff --git a/src/DBAL/PgSQLSchema.php b/src/DBAL/PgSQLSchema.php index 847942f0..6b299e7d 100644 --- a/src/DBAL/PgSQLSchema.php +++ b/src/DBAL/PgSQLSchema.php @@ -28,12 +28,12 @@ public function getTableNames(): Collection } // Schema name defined in the framework configuration. - $schema = DB::connection()->getConfig('schema'); + $searchPath = DB::connection()->getConfig('search_path') ?: DB::connection()->getConfig('schema'); $parts = explode('.', $table); $namespace = $parts[0]; - return $namespace === $schema; + return $namespace === $searchPath; }) ->values(); } @@ -75,7 +75,11 @@ public function getViews(): Collection return false; } - return $view->getNamespaceName() === DB::connection()->getConfig('schema'); + // Start from Laravel 9, the `schema` configuration option used to configure Postgres connection search paths renamed to `search_path`. + // Fallback to `schema` if Laravel version is older than 9. + $searchPath = DB::connection()->getConfig('search_path') ?: DB::connection()->getConfig('schema'); + + return $view->getNamespaceName() === $searchPath; }) ->map(function (DoctrineDBALView $view) { return new PgSQLView($view); diff --git a/tests/Feature/FeatureTestCase.php b/tests/Feature/FeatureTestCase.php index 8dd839c1..fbc8ac48 100644 --- a/tests/Feature/FeatureTestCase.php +++ b/tests/Feature/FeatureTestCase.php @@ -31,7 +31,6 @@ protected function setUp(): void parent::setUp(); $this->prepareStorage(); - $this->dropAllTables(); } protected function tearDown(): void diff --git a/tests/Feature/PgSQL/CommandTest.php b/tests/Feature/PgSQL/CommandTest.php index 18020def..dca49172 100644 --- a/tests/Feature/PgSQL/CommandTest.php +++ b/tests/Feature/PgSQL/CommandTest.php @@ -2,8 +2,10 @@ namespace KitLoong\MigrationsGenerator\Tests\Feature\PgSQL; +use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\File; +use KitLoong\MigrationsGenerator\Support\CheckLaravelVersion; /** * @runTestsInSeparateProcesses @@ -11,9 +13,8 @@ */ class CommandTest extends PgSQLTestCase { - /** - * @throws \Doctrine\DBAL\Exception - */ + use CheckLaravelVersion; + public function testRun() { $migrateTemplates = function () { @@ -48,9 +49,6 @@ public function testRun() $this->verify($migrateTemplates, $generateMigrations, $beforeVerify); } - /** - * @throws \Doctrine\DBAL\Exception - */ public function testCollation() { $migrateTemplates = function () { @@ -64,9 +62,6 @@ public function testCollation() $this->verify($migrateTemplates, $generateMigrations); } - /** - * @throws \Doctrine\DBAL\Exception - */ public function testIgnore() { $this->migrateGeneral('pgsql'); @@ -92,9 +87,36 @@ public function testIgnore() } /** - * @throws \Doctrine\DBAL\Exception + * Start from Laravel 9, the `schema` configuration option used to configure Postgres connection search paths renamed to `search_path`. + * @see https://laravel.com/docs/9.x/upgrade#postgres-schema-configuration + * + * @return void */ - public function verify(callable $migrateTemplates, callable $generateMigrations, callable $beforeVerify = null) + public function testRunWithSearchPath() + { + if (!$this->atLeastLaravel9()) { + $this->markTestSkipped(); + } + + // Unset `schema` + Config::set('database.connections.pgsql.schema'); + $this->assertNull(Config::get('database.connections.pgsql.schema')); + + // Use `search_path` + Config::set('database.connections.pgsql.search_path', 'public'); + + $migrateTemplates = function () { + $this->migrateGeneral('pgsql'); + }; + + $generateMigrations = function () { + $this->generateMigrations(); + }; + + $this->verify($migrateTemplates, $generateMigrations); + } + + private function verify(callable $migrateTemplates, callable $generateMigrations, callable $beforeVerify = null) { $migrateTemplates(); diff --git a/tests/Feature/SQLSrv/CommandTest.php b/tests/Feature/SQLSrv/CommandTest.php index 7056eec9..f5481a96 100644 --- a/tests/Feature/SQLSrv/CommandTest.php +++ b/tests/Feature/SQLSrv/CommandTest.php @@ -125,7 +125,7 @@ public function testGenerateXml() * @param callable $generateMigrations * @throws \Doctrine\DBAL\Exception */ - public function verify(callable $migrateTemplates, callable $generateMigrations) + private function verify(callable $migrateTemplates, callable $generateMigrations) { $migrateTemplates();