diff --git a/composer.json b/composer.json index e34094d..526e501 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/RowLevelSecurityAwareComparator.php b/src/RowLevelSecurityAwareComparator.php index 147048a..82a138d 100644 --- a/src/RowLevelSecurityAwareComparator.php +++ b/src/RowLevelSecurityAwareComparator.php @@ -4,6 +4,7 @@ namespace Linkage\DoctrineRowLevelSecurity; +use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Schema\Comparator; use Doctrine\DBAL\Schema\Schema; use Doctrine\DBAL\Schema\SchemaDiff; @@ -11,18 +12,16 @@ 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) { @@ -32,9 +31,9 @@ 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; } } @@ -42,13 +41,12 @@ public function compareSchemas(Schema $fromSchema, Schema $toSchema): RowLevelSe 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); @@ -56,13 +54,12 @@ public function diffTable(Table $fromTable, Table $toTable): TableDiff|bool } 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; } } diff --git a/src/RowLevelSecurityAwarePostgreSqlConnection.php b/src/RowLevelSecurityAwarePostgreSqlConnection.php index 20a38d9..084a45b 100644 --- a/src/RowLevelSecurityAwarePostgreSqlConnection.php +++ b/src/RowLevelSecurityAwarePostgreSqlConnection.php @@ -11,6 +11,6 @@ class RowLevelSecurityAwarePostgreSqlConnection extends Connection { public function getSchemaManager(): AbstractSchemaManager { - return new RowLevelSecurityAwarePostgreSqlSchemaManager($this); + return new RowLevelSecurityAwarePostgreSqlSchemaManager($this, $this->getDatabasePlatform()); } } diff --git a/src/RowLevelSecurityAwarePostgreSqlSchemaManager.php b/src/RowLevelSecurityAwarePostgreSqlSchemaManager.php index c889962..ca8c2c8 100644 --- a/src/RowLevelSecurityAwarePostgreSqlSchemaManager.php +++ b/src/RowLevelSecurityAwarePostgreSqlSchemaManager.php @@ -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 @@ -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); } } diff --git a/src/RowLevelSecurityAwareTableDiff.php b/src/RowLevelSecurityAwareTableDiff.php index ee8c766..39b816b 100644 --- a/src/RowLevelSecurityAwareTableDiff.php +++ b/src/RowLevelSecurityAwareTableDiff.php @@ -15,7 +15,7 @@ public function __construct( TableDiff $baseDiff, ) { parent::__construct( - $baseDiff->name, + $baseDiff->getOldTable()->getName(), $baseDiff->addedColumns, $baseDiff->changedColumns, $baseDiff->removedColumns, @@ -37,7 +37,7 @@ public function getRowLevelSecuritySqls(): array $using = $this->addedRowLevelSecurity['using'] ?? '## TODO 手動設定要 ##'; return $sqlFactory->createEnableSqls( $this->addedRowLevelSecurity['name'], - $this->name, + $this->getOldTable()->getName(), $this->addedRowLevelSecurity['role'], $using, ); @@ -45,7 +45,7 @@ public function getRowLevelSecuritySqls(): array if ($this->removedRowLevelSecurity !== null) { return $sqlFactory->createDisableSqls( $this->removedRowLevelSecurity['name'], - $this->name, + $this->getOldTable()->getName(), ); }