Skip to content

Commit 7b33b6f

Browse files
authored
Merge pull request #101 from kitloong/feature/view
Fix view migrations not generated
2 parents 658c6e5 + 3764e69 commit 7b33b6f

File tree

5 files changed

+44
-17
lines changed

5 files changed

+44
-17
lines changed

src/DBAL/Models/PgSQL/PgSQLView.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ protected function handle(DoctrineDBALView $view): void
1616
{
1717
$this->createViewSQL = $this->makeCreateViewSQL($this->quotedName, $view->getSql());
1818

19-
if ($view->getNamespaceName() === DB::connection()->getConfig('schema')) {
19+
$searchPath = DB::connection()->getConfig('search_path') ?: DB::connection()->getConfig('schema');
20+
21+
if ($view->getNamespaceName() === $searchPath) {
2022
// Strip namespace from name.
2123
$name = $view->getShortestName($view->getNamespaceName());
2224
$this->name = $this->makeName($name);

src/DBAL/PgSQLSchema.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ public function getTableNames(): Collection
2828
}
2929

3030
// Schema name defined in the framework configuration.
31-
$schema = DB::connection()->getConfig('schema');
31+
$searchPath = DB::connection()->getConfig('search_path') ?: DB::connection()->getConfig('schema');
3232

3333
$parts = explode('.', $table);
3434
$namespace = $parts[0];
3535

36-
return $namespace === $schema;
36+
return $namespace === $searchPath;
3737
})
3838
->values();
3939
}
@@ -75,7 +75,11 @@ public function getViews(): Collection
7575
return false;
7676
}
7777

78-
return $view->getNamespaceName() === DB::connection()->getConfig('schema');
78+
// Start from Laravel 9, the `schema` configuration option used to configure Postgres connection search paths renamed to `search_path`.
79+
// Fallback to `schema` if Laravel version is older than 9.
80+
$searchPath = DB::connection()->getConfig('search_path') ?: DB::connection()->getConfig('schema');
81+
82+
return $view->getNamespaceName() === $searchPath;
7983
})
8084
->map(function (DoctrineDBALView $view) {
8185
return new PgSQLView($view);

tests/Feature/FeatureTestCase.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ protected function setUp(): void
3131
parent::setUp();
3232

3333
$this->prepareStorage();
34-
$this->dropAllTables();
3534
}
3635

3736
protected function tearDown(): void

tests/Feature/PgSQL/CommandTest.php

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@
22

33
namespace KitLoong\MigrationsGenerator\Tests\Feature\PgSQL;
44

5+
use Illuminate\Support\Facades\Config;
56
use Illuminate\Support\Facades\DB;
67
use Illuminate\Support\Facades\File;
8+
use KitLoong\MigrationsGenerator\Support\CheckLaravelVersion;
79

810
/**
911
* @runTestsInSeparateProcesses
1012
* @preserveGlobalState disabled
1113
*/
1214
class CommandTest extends PgSQLTestCase
1315
{
14-
/**
15-
* @throws \Doctrine\DBAL\Exception
16-
*/
16+
use CheckLaravelVersion;
17+
1718
public function testRun()
1819
{
1920
$migrateTemplates = function () {
@@ -48,9 +49,6 @@ public function testRun()
4849
$this->verify($migrateTemplates, $generateMigrations, $beforeVerify);
4950
}
5051

51-
/**
52-
* @throws \Doctrine\DBAL\Exception
53-
*/
5452
public function testCollation()
5553
{
5654
$migrateTemplates = function () {
@@ -64,9 +62,6 @@ public function testCollation()
6462
$this->verify($migrateTemplates, $generateMigrations);
6563
}
6664

67-
/**
68-
* @throws \Doctrine\DBAL\Exception
69-
*/
7065
public function testIgnore()
7166
{
7267
$this->migrateGeneral('pgsql');
@@ -92,9 +87,36 @@ public function testIgnore()
9287
}
9388

9489
/**
95-
* @throws \Doctrine\DBAL\Exception
90+
* Start from Laravel 9, the `schema` configuration option used to configure Postgres connection search paths renamed to `search_path`.
91+
* @see https://laravel.com/docs/9.x/upgrade#postgres-schema-configuration
92+
*
93+
* @return void
9694
*/
97-
public function verify(callable $migrateTemplates, callable $generateMigrations, callable $beforeVerify = null)
95+
public function testRunWithSearchPath()
96+
{
97+
if (!$this->atLeastLaravel9()) {
98+
$this->markTestSkipped();
99+
}
100+
101+
// Unset `schema`
102+
Config::set('database.connections.pgsql.schema');
103+
$this->assertNull(Config::get('database.connections.pgsql.schema'));
104+
105+
// Use `search_path`
106+
Config::set('database.connections.pgsql.search_path', 'public');
107+
108+
$migrateTemplates = function () {
109+
$this->migrateGeneral('pgsql');
110+
};
111+
112+
$generateMigrations = function () {
113+
$this->generateMigrations();
114+
};
115+
116+
$this->verify($migrateTemplates, $generateMigrations);
117+
}
118+
119+
private function verify(callable $migrateTemplates, callable $generateMigrations, callable $beforeVerify = null)
98120
{
99121
$migrateTemplates();
100122

tests/Feature/SQLSrv/CommandTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public function testGenerateXml()
125125
* @param callable $generateMigrations
126126
* @throws \Doctrine\DBAL\Exception
127127
*/
128-
public function verify(callable $migrateTemplates, callable $generateMigrations)
128+
private function verify(callable $migrateTemplates, callable $generateMigrations)
129129
{
130130
$migrateTemplates();
131131

0 commit comments

Comments
 (0)