Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplified foreign key definition for single column fields #20

Merged
merged 2 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading