Skip to content

Commit 6203b01

Browse files
authored
Merge pull request #3 from linkage-corp/dbal-3
doctrine/dbal v3対応(laravel10対応のために必要)
2 parents dc9060c + 66ed33b commit 6203b01

5 files changed

+23
-23
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
"name": "linkage/doctrine-row-level-security",
33
"type": "library",
44
"require": {
5-
"doctrine/dbal": "^2.13",
5+
"doctrine/dbal": "^3.0",
66
"doctrine/orm": "^2.14",
7-
"php": ">=8.1"
7+
"php": ">=8.2"
88
},
99
"require-dev": {
1010
"phpunit/phpunit": "^10.0",

src/RowLevelSecurityAwareComparator.php

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,24 @@
44

55
namespace Linkage\DoctrineRowLevelSecurity;
66

7+
use Doctrine\DBAL\Platforms\AbstractPlatform;
78
use Doctrine\DBAL\Schema\Comparator;
89
use Doctrine\DBAL\Schema\Schema;
910
use Doctrine\DBAL\Schema\SchemaDiff;
1011
use Doctrine\DBAL\Schema\SchemaException;
1112
use Doctrine\DBAL\Schema\Table;
1213
use Doctrine\DBAL\Schema\TableDiff;
1314

14-
class RowLevelSecurityAwareComparator
15+
class RowLevelSecurityAwareComparator extends Comparator
1516
{
16-
private readonly Comparator $delegate;
17-
18-
public function __construct()
17+
public function __construct(AbstractPlatform $platform)
1918
{
20-
$this->delegate = new Comparator();
19+
parent::__construct($platform);
2120
}
2221

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

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

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

45-
public function diffTable(Table $fromTable, Table $toTable): TableDiff|bool
44+
public function compareTable(Table $fromTable, Table $toTable): TableDiff
4645
{
47-
$baseDiff = $this->delegate->diffTable($fromTable, $toTable);
46+
$baseTableDiff = parent::compareTable($fromTable, $toTable);
4847

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

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

6360
return $rlsTableDiff;
6461
}
6562

66-
return $baseDiff;
63+
return $baseTableDiff;
6764
}
6865
}

src/RowLevelSecurityAwarePostgreSqlConnection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ class RowLevelSecurityAwarePostgreSqlConnection extends Connection
1111
{
1212
public function getSchemaManager(): AbstractSchemaManager
1313
{
14-
return new RowLevelSecurityAwarePostgreSqlSchemaManager($this);
14+
return new RowLevelSecurityAwarePostgreSqlSchemaManager($this, $this->getDatabasePlatform());
1515
}
1616
}

src/RowLevelSecurityAwarePostgreSqlSchemaManager.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,19 @@
66

77
use Doctrine\DBAL\Connection;
88
use Doctrine\DBAL\Exception\DriverException;
9+
use Doctrine\DBAL\Platforms\AbstractPlatform;
910
use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
11+
use Doctrine\DBAL\Schema\Comparator;
1012
use Doctrine\DBAL\Schema\PostgreSqlSchemaManager;
1113
use Doctrine\DBAL\Schema\Table;
1214

1315
class RowLevelSecurityAwarePostgreSqlSchemaManager extends PostgreSqlSchemaManager
1416
{
1517
public function __construct(
16-
Connection $conn
18+
Connection $conn,
19+
AbstractPlatform $platform,
1720
) {
18-
parent::__construct($conn);
21+
parent::__construct($conn, $platform);
1922
}
2023

2124
public function listTableDetails($name): Table
@@ -53,8 +56,8 @@ public function listTableDetails($name): Table
5356
return $table;
5457
}
5558

56-
public function createComparator(): RowLevelSecurityAwareComparator
59+
public function createComparator(): Comparator
5760
{
58-
return new RowLevelSecurityAwareComparator();
61+
return new RowLevelSecurityAwareComparator($this->_platform);
5962
}
6063
}

src/RowLevelSecurityAwareTableDiff.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function __construct(
1515
TableDiff $baseDiff,
1616
) {
1717
parent::__construct(
18-
$baseDiff->name,
18+
$baseDiff->getOldTable()->getName(),
1919
$baseDiff->addedColumns,
2020
$baseDiff->changedColumns,
2121
$baseDiff->removedColumns,
@@ -37,15 +37,15 @@ public function getRowLevelSecuritySqls(): array
3737
$using = $this->addedRowLevelSecurity['using'] ?? '## TODO 手動設定要 ##';
3838
return $sqlFactory->createEnableSqls(
3939
$this->addedRowLevelSecurity['name'],
40-
$this->name,
40+
$this->getOldTable()->getName(),
4141
$this->addedRowLevelSecurity['role'],
4242
$using,
4343
);
4444
}
4545
if ($this->removedRowLevelSecurity !== null) {
4646
return $sqlFactory->createDisableSqls(
4747
$this->removedRowLevelSecurity['name'],
48-
$this->name,
48+
$this->getOldTable()->getName(),
4949
);
5050
}
5151

0 commit comments

Comments
 (0)