Skip to content

doctrine/dbal v3対応(laravel10対応のために必要) #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"name": "linkage/doctrine-row-level-security",
"type": "library",
"require": {
"doctrine/dbal": "^2.13",
"doctrine/dbal": "^3.0",
"doctrine/orm": "^2.14",
"php": ">=8.1"
"php": ">=8.2"
},
"require-dev": {
"phpunit/phpunit": "^10.0",
Expand Down
23 changes: 10 additions & 13 deletions src/RowLevelSecurityAwareComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,24 @@

namespace Linkage\DoctrineRowLevelSecurity;

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Schema\Comparator;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\SchemaDiff;
use Doctrine\DBAL\Schema\SchemaException;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff;

class RowLevelSecurityAwareComparator
class RowLevelSecurityAwareComparator extends Comparator
{
private readonly Comparator $delegate;

public function __construct()
public function __construct(AbstractPlatform $platform)
{
$this->delegate = new Comparator();
parent::__construct($platform);
}

public function compareSchemas(Schema $fromSchema, Schema $toSchema): RowLevelSecurityAwareSchemaDiff|SchemaDiff
{
$baseDiff = $this->delegate->compare($fromSchema, $toSchema);
$baseDiff = parent::compareSchemas($fromSchema, $toSchema);

$hasRlsDiff = false;
foreach ($toSchema->getTables() as $toTable) {
Expand All @@ -32,37 +31,35 @@ public function compareSchemas(Schema $fromSchema, Schema $toSchema): RowLevelSe
// 新規テーブルは差分がある扱いなのでcreateのイベントリスナに任せる
continue;
}
$tableDiff = $this->diffTable($fromTable, $toTable);
$tableDiff = $this->compareTable($fromTable, $toTable);
if ($tableDiff instanceof RowLevelSecurityAwareTableDiff) {
$baseDiff->changedTables[$tableDiff->name] = $tableDiff;
$baseDiff->changedTables[$tableDiff->getOldTable()->getName()] = $tableDiff;
$hasRlsDiff = true;
}
}

return $hasRlsDiff ? new RowLevelSecurityAwareSchemaDiff($baseDiff) : $baseDiff;
}

public function diffTable(Table $fromTable, Table $toTable): TableDiff|bool
public function compareTable(Table $fromTable, Table $toTable): TableDiff
{
$baseDiff = $this->delegate->diffTable($fromTable, $toTable);
$baseTableDiff = parent::compareTable($fromTable, $toTable);

if ($toTable->hasOption(RowLevelSecurityConfig::RLS_OPTION_NAME) && !$fromTable->hasOption(RowLevelSecurityConfig::RLS_OPTION_NAME)) {
// RLSがなかったテーブルにRLSを足した時
$baseTableDiff = $baseDiff instanceof TableDiff ? $baseDiff : new TableDiff($toTable->getName(), fromTable: $fromTable);
$rlsTableDiff = new RowLevelSecurityAwareTableDiff($baseTableDiff);
$rlsTableDiff->addedRowLevelSecurity = $toTable->getOption(RowLevelSecurityConfig::RLS_OPTION_NAME);

return $rlsTableDiff;
}
if (!$toTable->hasOption(RowLevelSecurityConfig::RLS_OPTION_NAME) && $fromTable->hasOption(RowLevelSecurityConfig::RLS_OPTION_NAME)) {
// RLSがあったテーブルのRLSを消した時
$baseTableDiff = $baseDiff instanceof TableDiff ? $baseDiff : new TableDiff($toTable->getName(), fromTable: $fromTable);
$rlsTableDiff = new RowLevelSecurityAwareTableDiff($baseTableDiff);
$rlsTableDiff->removedRowLevelSecurity = $fromTable->getOption(RowLevelSecurityConfig::RLS_OPTION_NAME);

return $rlsTableDiff;
}

return $baseDiff;
return $baseTableDiff;
}
}
2 changes: 1 addition & 1 deletion src/RowLevelSecurityAwarePostgreSqlConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ class RowLevelSecurityAwarePostgreSqlConnection extends Connection
{
public function getSchemaManager(): AbstractSchemaManager
{
return new RowLevelSecurityAwarePostgreSqlSchemaManager($this);
return new RowLevelSecurityAwarePostgreSqlSchemaManager($this, $this->getDatabasePlatform());
}
}
11 changes: 7 additions & 4 deletions src/RowLevelSecurityAwarePostgreSqlSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Exception\DriverException;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
use Doctrine\DBAL\Schema\Comparator;
use Doctrine\DBAL\Schema\PostgreSqlSchemaManager;
use Doctrine\DBAL\Schema\Table;

class RowLevelSecurityAwarePostgreSqlSchemaManager extends PostgreSqlSchemaManager
{
public function __construct(
Connection $conn
Connection $conn,
AbstractPlatform $platform,
) {
parent::__construct($conn);
parent::__construct($conn, $platform);
}

public function listTableDetails($name): Table
Expand Down Expand Up @@ -53,8 +56,8 @@ public function listTableDetails($name): Table
return $table;
}

public function createComparator(): RowLevelSecurityAwareComparator
public function createComparator(): Comparator
{
return new RowLevelSecurityAwareComparator();
return new RowLevelSecurityAwareComparator($this->_platform);
}
}
6 changes: 3 additions & 3 deletions src/RowLevelSecurityAwareTableDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function __construct(
TableDiff $baseDiff,
) {
parent::__construct(
$baseDiff->name,
$baseDiff->getOldTable()->getName(),
$baseDiff->addedColumns,
$baseDiff->changedColumns,
$baseDiff->removedColumns,
Expand All @@ -37,15 +37,15 @@ public function getRowLevelSecuritySqls(): array
$using = $this->addedRowLevelSecurity['using'] ?? '## TODO 手動設定要 ##';
return $sqlFactory->createEnableSqls(
$this->addedRowLevelSecurity['name'],
$this->name,
$this->getOldTable()->getName(),
$this->addedRowLevelSecurity['role'],
$using,
);
}
if ($this->removedRowLevelSecurity !== null) {
return $sqlFactory->createDisableSqls(
$this->removedRowLevelSecurity['name'],
$this->name,
$this->getOldTable()->getName(),
);
}

Expand Down