Skip to content

Commit d225c8a

Browse files
authored
Pass options as to schema tool class (#7)
1 parent 037d1fb commit d225c8a

8 files changed

Lines changed: 116 additions & 73 deletions

File tree

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22

33
declare(strict_types=1);
44

5-
namespace Fabiang\Doctrine\Migrations\Liquibase\Output;
5+
namespace Fabiang\Doctrine\Migrations\Liquibase;
66

7-
class LiquibaseOutputOptions
7+
class Options
88
{
99
private bool $usePlatformTypes = false;
1010
private bool $changeSetUniqueId = true;
1111
private string $changeSetAuthor = 'doctrine-migrations-liquibase';
12+
private array $ignoreTables = [
13+
'DATABASECHANGELOG',
14+
'DATABASECHANGELOGLOCK',
15+
];
1216

1317
public function isUsePlatformTypes(): bool
1418
{
@@ -51,4 +55,21 @@ public function setChangeSetAuthor(string $changeSetAuthor): self
5155
$this->changeSetAuthor = $changeSetAuthor;
5256
return $this;
5357
}
58+
59+
/**
60+
* @return string[]
61+
*/
62+
public function getIgnoreTables(): array
63+
{
64+
return $this->ignoreTables;
65+
}
66+
67+
/**
68+
* @psalm-suppress PossiblyUnusedMethod
69+
* @params string[] $ignoreTables
70+
*/
71+
public function setIgnoreTables(array $ignoreTables): void
72+
{
73+
$this->ignoreTables = $ignoreTables;
74+
}
5475
}
Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Fabiang\Doctrine\Migrations\Liquibase\DBAL\IndexColumns;
2424
use Fabiang\Doctrine\Migrations\Liquibase\DBAL\QualifiedName;
2525
use Fabiang\Doctrine\Migrations\Liquibase\Helper\VersionHelper;
26+
use Fabiang\Doctrine\Migrations\Liquibase\Options;
2627
use Override;
2728

2829
use function array_key_exists;
@@ -39,17 +40,19 @@
3940
use function strval;
4041
use function uniqid;
4142

42-
class LiquibaseDOMDocumentOutput implements LiquibaseOutputInterface
43+
class DOMDocumentOutput implements OutputInterface
4344
{
4445
private DOMDocument $document;
45-
private LiquibaseOutputOptions $options;
46+
private Options $options;
4647
private AbstractPlatform $platform;
4748
private DOMElement $root;
4849

49-
public function __construct(?LiquibaseOutputOptions $options = null, ?DOMDocument $document = null)
50-
{
50+
public function __construct(
51+
?Options $options = null,
52+
?DOMDocument $document = null
53+
) {
5154
if (null === $options) {
52-
$options = new LiquibaseOutputOptions();
55+
$options = new Options();
5356
}
5457

5558
$this->options = $options;
@@ -58,11 +61,10 @@ public function __construct(?LiquibaseOutputOptions $options = null, ?DOMDocumen
5861
$document = new DOMDocument();
5962
$document->preserveWhiteSpace = false;
6063
$document->formatOutput = true;
61-
$this->document = $document;
62-
} else {
63-
$this->document = $document;
6464
}
6565

66+
$this->document = $document;
67+
6668
$this->root = $this->document->createElement('databaseChangeLog');
6769
$this->platform = new MySQLPlatform();
6870
}
@@ -78,7 +80,7 @@ public function getDocument(): DOMDocument
7880
/**
7981
* @psalm-suppress PossiblyUnusedMethod
8082
*/
81-
public function getOptions(): LiquibaseOutputOptions
83+
public function getOptions(): Options
8284
{
8385
return $this->options;
8486
}
@@ -704,7 +706,19 @@ protected function alterTableRenamedIndexes(
704706
return;
705707
}
706708

707-
foreach ($tableDiff->getRenamedIndexes() as $oldName => $index) {
709+
$renamedIndex = $tableDiff->getRenamedIndexes();
710+
711+
if (count($renamedIndex) > 0) {
712+
/**
713+
* @psalm-suppress InternalMethod
714+
*/
715+
$commentElt = $this->document->createComment(
716+
' Renamed Indexes '
717+
);
718+
$changeSetElt->appendChild($commentElt);
719+
}
720+
721+
foreach ($renamedIndex as $oldName => $index) {
708722
$this->dropIndex($fromTableName, $oldName, $changeSetElt);
709723

710724
$this->createIndex(
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use Doctrine\DBAL\Schema\TableDiff;
1111
use Doctrine\ORM\EntityManagerInterface;
1212

13-
interface LiquibaseOutputInterface
13+
interface OutputInterface
1414
{
1515
public function createSchema(string $newNamespace): void;
1616

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,32 @@
1111
use Doctrine\ORM\EntityManagerInterface;
1212
use Doctrine\ORM\Exception\ORMException;
1313
use Doctrine\ORM\Mapping\ClassMetadata;
14-
use Doctrine\ORM\Tools\SchemaTool;
14+
use Doctrine\ORM\Tools\SchemaTool as DoctrineSchemaTool;
1515
use DOMDocument;
16-
use Fabiang\Doctrine\Migrations\Liquibase\Output\LiquibaseDOMDocumentOutput;
17-
use Fabiang\Doctrine\Migrations\Liquibase\Output\LiquibaseOutputInterface;
18-
use Fabiang\Doctrine\Migrations\Liquibase\Output\LiquibaseOutputOptions;
16+
use Fabiang\Doctrine\Migrations\Liquibase\Output\DOMDocumentOutput;
17+
use Fabiang\Doctrine\Migrations\Liquibase\Output\OutputInterface;
1918

20-
use function array_merge;
2119
use function strcmp;
2220
use function usort;
2321

2422
/**
2523
* @psalm-suppress UnusedClass
2624
*/
27-
class LiquibaseSchemaTool extends SchemaTool
25+
class SchemaTool extends DoctrineSchemaTool
2826
{
29-
private const array LIQUIBASE_TABLES = ['liquibase', 'liquibase_lock'];
27+
private Options $options;
3028

31-
/**
32-
* @param string[] $ignoreTables
33-
*/
34-
public function __construct(private EntityManagerInterface $em, private array $ignoreTables = [])
35-
{
29+
public function __construct(
30+
private EntityManagerInterface $em,
31+
?Options $options = null
32+
) {
3633
parent::__construct($em);
34+
35+
if ($options === null) {
36+
$options = new Options();
37+
}
38+
39+
$this->options = $options;
3740
}
3841

3942
/**
@@ -42,7 +45,7 @@ public function __construct(private EntityManagerInterface $em, private array $i
4245
* @throws ORMException
4346
*/
4447
public function diffChangeLog(
45-
LiquibaseOutputInterface|LiquibaseOutputOptions|null $output = null,
48+
?OutputInterface $output = null,
4649
?array $metadata = null
4750
): DOMDocument {
4851
$soutput = $this->sanitizeOutputParameter($output);
@@ -70,7 +73,7 @@ public function diffChangeLog(
7073
* @throws ORMException
7174
*/
7275
public function changeLog(
73-
LiquibaseOutputInterface|LiquibaseOutputOptions|null $output = null,
76+
?OutputInterface $output = null,
7477
?array $metadata = null
7578
): DOMDocument {
7679
$soutput = $this->sanitizeOutputParameter($output);
@@ -93,7 +96,7 @@ public function changeLog(
9396
*/
9497
public function diffChangeLogFromSchemaDiff(
9598
SchemaDiff $schemaDiff,
96-
LiquibaseOutputInterface|LiquibaseOutputOptions|null $output = null
99+
?OutputInterface $output = null
97100
): DOMDocument {
98101
$soutput = $this->sanitizeOutputParameter($output);
99102

@@ -148,7 +151,7 @@ public function diffChangeLogFromSchemaDiff(
148151

149152
private function removeLiquibaseTables(Schema $fromSchema): void
150153
{
151-
$tables = array_merge(self::LIQUIBASE_TABLES, $this->ignoreTables);
154+
$tables = $this->options->getIgnoreTables();
152155

153156
foreach ($tables as $table) {
154157
if ($fromSchema->hasTable($table)) {
@@ -157,16 +160,13 @@ private function removeLiquibaseTables(Schema $fromSchema): void
157160
}
158161
}
159162

160-
private function sanitizeOutputParameter(
161-
LiquibaseOutputInterface|LiquibaseOutputOptions|null $output = null
162-
): LiquibaseOutputInterface {
163-
if ($output instanceof LiquibaseOutputOptions) {
164-
return new LiquibaseDOMDocumentOutput($output);
165-
} elseif ($output instanceof LiquibaseOutputInterface) {
163+
private function sanitizeOutputParameter(?OutputInterface $output = null): OutputInterface
164+
{
165+
if ($output instanceof OutputInterface) {
166166
return $output;
167167
}
168168

169-
return new LiquibaseDOMDocumentOutput();
169+
return new DOMDocumentOutput($this->options);
170170
}
171171

172172
/**

tests/features/bootstrap/AbstractDBContext.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use Doctrine\ORM\Configuration;
1414
use Doctrine\ORM\EntityManager;
1515
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
16-
use Fabiang\Doctrine\Migrations\Liquibase\Output\LiquibaseOutputOptions;
1716
use SebastianBergmann\Comparator\ComparisonFailure;
1817
use Webmozart\Assert\Assert;
1918

@@ -61,14 +60,14 @@ protected function connectionToDatabaseIsEstablished(): void
6160

6261
protected function changelogIsExecuted(): void
6362
{
64-
$schemaTool = new LiquibaseSchemaTool($this->em, $this->ignoreTables);
65-
$this->output = $schemaTool->changeLog($this->options())->saveXML();
63+
$schemaTool = new SchemaTool($this->em, $this->options());
64+
$this->output = $schemaTool->changeLog()->saveXML();
6665
}
6766

6867
protected function diffChangelogIsExecuted(): void
6968
{
70-
$schemaTool = new LiquibaseSchemaTool($this->em, $this->ignoreTables);
71-
$this->output = $schemaTool->diffChangeLog($this->options())->saveXML();
69+
$schemaTool = new SchemaTool($this->em, $this->options());
70+
$this->output = $schemaTool->diffChangeLog()->saveXML();
7271
}
7372

7473
protected function theOutputXmlShouldBe(PyStringNode $expected): void
@@ -103,10 +102,11 @@ public function close(): void
103102
$this->em = null;
104103
}
105104

106-
protected function options(): LiquibaseOutputOptions
105+
protected function options(): Options
107106
{
108-
$options = new LiquibaseOutputOptions();
107+
$options = new Options();
109108
$options->setChangeSetUniqueId(false);
109+
$options->setIgnoreTables($this->ignoreTables);
110110
return $options;
111111
}
112112
}

tests/units/src/Output/LiquibaseOutputOptionsTest.php renamed to tests/units/src/OptionsTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@
22

33
declare(strict_types=1);
44

5-
namespace Fabiang\Doctrine\Migrations\Liquibase\Output;
5+
namespace Fabiang\Doctrine\Migrations\Liquibase;
66

77
use PHPUnit\Framework\Attributes\CoversClass;
88
use PHPUnit\Framework\TestCase;
99
use Prophecy\PhpUnit\ProphecyTrait;
1010

11-
#[CoversClass(LiquibaseOutputOptions::class)]
12-
final class LiquibaseOutputOptionsTest extends TestCase
11+
#[CoversClass(Options::class)]
12+
final class OptionsTest extends TestCase
1313
{
1414
use ProphecyTrait;
1515

16-
private LiquibaseOutputOptions $object;
16+
private Options $object;
1717

1818
protected function setUp(): void
1919
{
20-
$this->object = new LiquibaseOutputOptions();
20+
$this->object = new Options();
2121
}
2222

2323
public function testSetterAndGetter(): void

tests/units/src/Output/LiquibaseDOMDocumentOutputTest.php renamed to tests/units/src/Output/DOMDocumentOutputTest.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use DOMDocument;
1919
use Fabiang\Doctrine\Migrations\Liquibase\ColumnDiffTrait;
2020
use Fabiang\Doctrine\Migrations\Liquibase\Helper\VersionHelper;
21+
use Fabiang\Doctrine\Migrations\Liquibase\Options;
2122
use Fabiang\Doctrine\Migrations\Liquibase\TableDiffTrait;
2223
use PHPUnit\Framework\Attributes\CoversClass;
2324
use PHPUnit\Framework\TestCase;
@@ -26,30 +27,30 @@
2627
use Prophecy\PhpUnit\ProphecyTrait;
2728
use Prophecy\Prophecy\ObjectProphecy;
2829

29-
#[CoversClass(LiquibaseDOMDocumentOutput::class)]
30-
final class LiquibaseDOMDocumentOutputTest extends TestCase
30+
#[CoversClass(DOMDocumentOutput::class)]
31+
final class DOMDocumentOutputTest extends TestCase
3132
{
3233
use ColumnDiffTrait;
3334
use ProphecyTrait;
3435
use TableDiffTrait;
3536
use XPathAssert;
3637

37-
private LiquibaseDOMDocumentOutput $output;
38-
private LiquibaseOutputOptions $options;
38+
private DOMDocumentOutput $output;
39+
private Options $options;
3940
private DOMDocument $document;
4041
private ObjectProphecy $em;
4142
private ObjectProphecy $connection;
4243
private ObjectProphecy $platform;
4344

4445
protected function setUp(): void
4546
{
46-
$this->options = new LiquibaseOutputOptions();
47+
$this->options = new Options();
4748
$this->options->setChangeSetUniqueId(false);
4849
$this->options->setChangeSetAuthor('phpunit');
4950

5051
$this->document = new DOMDocument();
5152

52-
$this->output = new LiquibaseDOMDocumentOutput($this->options, $this->document);
53+
$this->output = new DOMDocumentOutput($this->options, $this->document);
5354

5455
$this->platform = $this->prophesize(AbstractPlatform::class);
5556
$this->platform->getStringTypeDeclarationSQL(Argument::any())->willReturn('test');
@@ -66,8 +67,8 @@ protected function setUp(): void
6667

6768
public function testDefaultConstructorOptions(): void
6869
{
69-
$output = new LiquibaseDOMDocumentOutput();
70-
$this->assertInstanceOf(LiquibaseOutputOptions::class, $output->getOptions());
70+
$output = new DOMDocumentOutput();
71+
$this->assertInstanceOf(Options::class, $output->getOptions());
7172
$this->assertInstanceOf(DOMDocument::class, $output->getDocument());
7273
$this->assertInstanceOf(DOMDocument::class, $output->getResult());
7374
}

0 commit comments

Comments
 (0)