From e22cf765a792a54d932c7c93311aab6d89184f29 Mon Sep 17 00:00:00 2001 From: Kit Loong Date: Thu, 7 Jul 2022 22:23:56 +0800 Subject: [PATCH 1/3] Change to private --- tests/Feature/SQLSrv/CommandTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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(); From 30c5db2dccfead088dd0462edc4d609700b60439 Mon Sep 17 00:00:00 2001 From: Kit Loong Date: Thu, 7 Jul 2022 22:59:07 +0800 Subject: [PATCH 2/3] Fix #99 View migrations not generated. --- src/DBAL/PgSQLSchema.php | 6 +++- tests/Feature/FeatureTestCase.php | 1 - tests/Feature/PgSQL/CommandTest.php | 44 +++++++++++++++++++++-------- 3 files changed, 38 insertions(+), 13 deletions(-) diff --git a/src/DBAL/PgSQLSchema.php b/src/DBAL/PgSQLSchema.php index 847942f0..0eaf2b0b 100644 --- a/src/DBAL/PgSQLSchema.php +++ b/src/DBAL/PgSQLSchema.php @@ -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(); From 3764e696adc909a8b791c4393d0ba918870e52ee Mon Sep 17 00:00:00 2001 From: Kit Loong Date: Thu, 7 Jul 2022 23:27:37 +0800 Subject: [PATCH 3/3] Use search_path --- src/DBAL/Models/PgSQL/PgSQLView.php | 4 +++- src/DBAL/PgSQLSchema.php | 6 +++--- 2 files changed, 6 insertions(+), 4 deletions(-) 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 0eaf2b0b..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(); } @@ -77,7 +77,7 @@ public function getViews(): Collection // 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'); + $searchPath = DB::connection()->getConfig('search_path') ?: DB::connection()->getConfig('schema'); return $view->getNamespaceName() === $searchPath; })