Skip to content

Commit

Permalink
Merge branch '4.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
Steveb-p committed Jun 28, 2023
2 parents e82bf54 + dd5e0e1 commit 5f53917
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 3 deletions.
14 changes: 14 additions & 0 deletions src/lib/Importer/SchemaImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ private function addSchemaTableColumns(Table $table, array $columnList): void
'nullable',
'options',
'index',
'foreignKey',
]);

if (isset($columnConfiguration['length'])) {
Expand Down Expand Up @@ -175,6 +176,19 @@ private function addSchemaTableColumns(Table $table, array $columnList): void
$column->getName(),
);
}

if (isset($columnConfiguration['foreignKey'])) {
$foreignKeyConfig = $columnConfiguration['foreignKey'];
$foreignField = $foreignKeyConfig['field'];

$table->addForeignKeyConstraint(
$foreignKeyConfig['table'],
[$column->getName()],
[$foreignField],
$foreignKeyConfig['options'] ?? [],
$foreignKeyConfig['name'] ?? null,
);
}
}
}

Expand Down
67 changes: 64 additions & 3 deletions tests/lib/Importer/SchemaImporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,69 @@ public function providerForTestImportFromFile(): iterable
]
),
];

$table = new Table(
'my_table',
[
new Column('id', Type::getType('integer')),
new Column('data1', Type::getType('integer')),
new Column('data2', Type::getType('integer')),
new Column('data3', Type::getType('string')),
new Column('data4', Type::getType('string')),
],
[
// Index for data1 is intentionally omitted
new Index('data2_idx', ['data2'], false, false),
new Index('data3_idx', ['data3'], false, false),
new Index('data4_uidx', ['data4'], true, false),
],
[
new ForeignKeyConstraint(
['id'],
'foreign_table_id',
['foreign_id'],
'id_fk',
),
new ForeignKeyConstraint(
['data1'],
'foreign_table_1',
['foreign_data1'],
'FK_9AEF3D8257CA2CA6', // Autogenerated
),
new ForeignKeyConstraint(
['data2'],
'foreign_table_2',
['foreign_data2'],
'foreign_data2_fk_name',
),
new ForeignKeyConstraint(
['data3'],
'foreign_table_3',
['foreign_data3'],
'foreign_data3_fk_name',
),
new ForeignKeyConstraint(
['data4'],
'foreign_table_4',
['foreign_data4'],
'foreign_data4_fk_name',
[
'onDelete' => 'CASCADE',
'onUpdate' => 'RESTRICT',
],
),
]
);
$table->setPrimaryKey(['id']);

yield [
'simple-foreign-key.yaml',
new Schema(
[
$table,
]
),
];
}

/**
Expand Down Expand Up @@ -258,10 +321,8 @@ public function testColumnImportFailsIfUnhandledKeys(): void
$this->expectException(InvalidConfigurationException::class);
$this->expectExceptionMessage(
'Unhandled property in schema configuration for "my_table.fields.foo". "bar" keys are not allowed. Allowed keys:'
. ' "length", "scale", "precision", "type", "nullable", "options", "index".'
. ' "length", "scale", "precision", "type", "nullable", "options", "index", "foreignKey".'
);
$importer->importFromFile(__DIR__ . '/_fixtures/failing-import-column.yaml');
}
}

class_alias(SchemaImporterTest::class, 'EzSystems\Tests\DoctrineSchema\Importer\SchemaImporterTest');
47 changes: 47 additions & 0 deletions tests/lib/Importer/_fixtures/simple-foreign-key.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
tables:
my_table:
id:
id:
type: integer
nullable: false
foreignKey:
name: id_fk
table: foreign_table_id
field: foreign_id
fields:
data1:
type: integer
nullable: false
foreignKey:
table: foreign_table_1
field: foreign_data1
data2:
type: integer
nullable: false
foreignKey:
table: foreign_table_2
field: foreign_data2
name: foreign_data2_fk_name
index: data2_idx
data3:
type: string
nullable: false
foreignKey:
table: foreign_table_3
field: foreign_data3
name: foreign_data3_fk_name
index:
name: data3_idx
data4:
type: string
nullable: false
foreignKey:
table: foreign_table_4
field: foreign_data4
name: foreign_data4_fk_name
options:
onDelete: CASCADE
onUpdate: RESTRICT
index:
name: data4_uidx
unique: true

0 comments on commit 5f53917

Please sign in to comment.