Skip to content

Commit

Permalink
Merge pull request #101 from kitloong/feature/view
Browse files Browse the repository at this point in the history
Fix view migrations not generated
  • Loading branch information
kitloong authored Jul 7, 2022
2 parents 658c6e5 + 3764e69 commit 7b33b6f
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 17 deletions.
4 changes: 3 additions & 1 deletion src/DBAL/Models/PgSQL/PgSQLView.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 7 additions & 3 deletions src/DBAL/PgSQLSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion tests/Feature/FeatureTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ protected function setUp(): void
parent::setUp();

$this->prepareStorage();
$this->dropAllTables();
}

protected function tearDown(): void
Expand Down
44 changes: 33 additions & 11 deletions tests/Feature/PgSQL/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

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
* @preserveGlobalState disabled
*/
class CommandTest extends PgSQLTestCase
{
/**
* @throws \Doctrine\DBAL\Exception
*/
use CheckLaravelVersion;

public function testRun()
{
$migrateTemplates = function () {
Expand Down Expand Up @@ -48,9 +49,6 @@ public function testRun()
$this->verify($migrateTemplates, $generateMigrations, $beforeVerify);
}

/**
* @throws \Doctrine\DBAL\Exception
*/
public function testCollation()
{
$migrateTemplates = function () {
Expand All @@ -64,9 +62,6 @@ public function testCollation()
$this->verify($migrateTemplates, $generateMigrations);
}

/**
* @throws \Doctrine\DBAL\Exception
*/
public function testIgnore()
{
$this->migrateGeneral('pgsql');
Expand All @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/SQLSrv/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit 7b33b6f

Please sign in to comment.