Skip to content

Commit 2028a7e

Browse files
committed
✨ Support default_table_options to lock file (from SchemaConfig)
1 parent 14a3693 commit 2028a7e

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

src/SchemaVersionControl/SchemaBuilder.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Doctrine\DBAL\Schema\Column;
66
use Doctrine\DBAL\Schema\Schema;
7+
use Doctrine\DBAL\Schema\SchemaConfig;
78
use Doctrine\DBAL\Schema\Table;
89

910
/**
@@ -25,7 +26,9 @@ class SchemaBuilder
2526
public function build(array $schemaDesc): Schema
2627
{
2728
$this->schemaDesc = $schemaDesc;
28-
$schema = new Schema();
29+
$schemaConfig = new SchemaConfig();
30+
$schemaConfig->setDefaultTableOptions($schemaDesc['default_table_options'] ?? []);
31+
$schema = new Schema([], [], $schemaConfig);
2932
foreach ($schemaDesc['tables'] as $name => $tableDesc) {
3033
$table = $schema->createTable($name);
3134
$this->buildTable($tableDesc, $table);

src/SchemaVersionControl/SchemaNormalizer.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
77
use Doctrine\DBAL\Schema\Index;
88
use Doctrine\DBAL\Schema\Schema;
9+
use Doctrine\DBAL\Schema\SchemaConfig;
910
use Doctrine\DBAL\Schema\Table;
1011

1112
/**
@@ -16,18 +17,21 @@
1617
*/
1718
class SchemaNormalizer
1819
{
19-
/** @var Schema */
20-
protected $schema;
20+
protected Schema $schema;
21+
protected SchemaConfig $schemaConfig;
2122

2223
/**
2324
* Normalize a Schema object into an array descriptor
24-
* @param Schema $schema
2525
* @return array
2626
*/
27-
public function normalize(Schema $schema): array
27+
public function normalize(Schema $schema, SchemaConfig $schemaConfig): array
2828
{
2929
$this->schema = $schema;
30+
$this->schemaConfig = $schemaConfig;
3031
$schemaDesc = [];
32+
if (!empty($this->schemaConfig->getDefaultTableOptions())) {
33+
$schemaDesc['default_table_options'] = $this->schemaConfig->getDefaultTableOptions();
34+
}
3135
$schemaDesc['tables'] = [];
3236
foreach ($schema->getTables() as $table) {
3337
$schemaDesc['tables'][$table->getName()] = $this->normalizeTable($table);
@@ -107,7 +111,10 @@ protected function normalizeColumn(Column $column, bool $isPrimaryKey)
107111
$columnDesc['comment'] = $column->getComment();
108112
}
109113
if (!empty($column->getPlatformOptions())) {
110-
$columnDesc['custom'] = $column->getPlatformOptions();
114+
$custom = array_diff_assoc($column->getPlatformOptions(), $this->schemaConfig->getDefaultTableOptions());
115+
if (!empty($custom)) {
116+
$columnDesc['custom'] = $custom;
117+
}
111118
}
112119

113120
if (count($columnDesc) > 1) {

src/SchemaVersionControl/SchemaVersionControlService.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,10 @@ public function loadSchemaFile(): Schema
5656
public function dumpSchema(): void
5757
{
5858
$schemaManager = $this->connection->createSchemaManager();
59+
$schemaConfig = $schemaManager->createSchemaConfig();
5960
$schema = $schemaManager->createSchema();
6061
$normalizer = new SchemaNormalizer();
61-
$desc = $normalizer->normalize($schema);
62+
$desc = $normalizer->normalize($schema, $schemaConfig);
6263
$yamlSchema = Yaml::dump(['schema' => $desc], 10, 2);
6364
$directory = dirname($this->schemaFile);
6465
if (!file_exists($directory)) {

0 commit comments

Comments
 (0)