-
Notifications
You must be signed in to change notification settings - Fork 16
IBX-9617: Fixed DB schema reinstallation #485
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
base: 4.6
Are you sure you want to change the base?
Conversation
|
$tables = $newSchema->getTables(); | ||
if ($databasePlatform->supportsForeignKeyConstraints()) { | ||
// cleanup pre-existing database: drop foreign keys | ||
foreach ($tables as $table) { | ||
if ($existingSchema->hasTable($table->getName())) { | ||
foreach ($this->db->getSchemaManager()->listTableForeignKeys($table->getName()) as $foreignKeyConstraint) { | ||
$statements[] = $databasePlatform->getDropForeignKeySQL($foreignKeyConstraint->getName(), $table->getName()); | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels to me like a workaround. Tables should be dropped in a correct order. Have you tried to see if we can reorganize tables order in Yaml for doctrine schema instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@alongosz I didn't. I replaced the not-working array_reverse
trick with another trick.
Studying how the table list is loaded/built from YAML schema is probably also necessary to understand why 3 tables are missing. But my knowledge of Doctrine is limited and I don't manage to explore this schema declaration.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels to me like a workaround. Tables should be dropped in a correct order. Have you tried to see if we can reorganize tables order in Yaml for doctrine schema instead?
I didn't. I replaced the not-working
array_reverse
trick with another trick. Studying how the table list is loaded/built from YAML schema is probably also necessary to understand why 3 tables are missing. But my knowledge of Doctrine is limited and I don't manage to explore this schema declaration.
Ok. Looks like that this could go in. I'm a bit worried about performance of listing foreign keys for every table, but on the other hand it's just for re-installation. Maybe QA can focus on this when testing?
Given the method became more complicated, I'd need to see here 2 things:
- Unit test coverage for CoreInstaller. I can help with that if it's too complicated.
- Refactoring. Correct me if I'm wrong, but instead of processing the tables list 2 times, we can in one single loop for each table provide both drop foreign key statements and drop table statement? Or do we really need to drop all foreign keys first and then drop tables?
Something I cooked up on the fly would look like this:
/**
* @param \Doctrine\DBAL\Schema\Schema $newSchema
* @param \Doctrine\DBAL\Platforms\AbstractPlatform $databasePlatform
*
* @return string[]
*/
protected function getDropSqlStatementsForExistingSchema(
Schema $newSchema,
AbstractPlatform $databasePlatform
): array {
$schemaManager = $this->db->getSchemaManager();
if ($schemaManager === null) {
throw new \LogicException('CoreInstaller: Unable to get Schema manager');
}
$existingSchema = $schemaManager->createSchema();
$statements = [];
$tables = $newSchema->getTables();
$supportsForeignKeyConstraints = $databasePlatform->supportsForeignKeyConstraints();
foreach ($tables as $table) {
if (!$existingSchema->hasTable($table->getName())) {
continue;
}
if ($supportsForeignKeyConstraints) {
// cleanup pre-existing database: drop foreign keys
foreach ($schemaManager->listTableForeignKeys($table->getName()) as $foreignKeyConstraint) {
$statements[] = $databasePlatform->getDropForeignKeySQL($foreignKeyConstraint->getName(), $table->getName());
}
}
// cleanup pre-existing database: drop tables
$statements[] = $databasePlatform->getDropTableSQL($table);
}
return $statements;
}
Haven't tested it however.
Answering "yes" to "Continue?" just throw errors and fail. SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails
More cross-platform solution.
If written only for FKs, it didn't work.
f1038e9
to
da2fe07
Compare
|
Description:
Before:
Issues:
Fixes:
After:
Remaining issue I'm not able to fix by myself:
ibexa_payment_token
,ibexa_taxonomy_assignments
, andibexa_taxonomy_entries
.SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'tags-root' for key 'ibexa_taxonomy_entries_identifier_idx'
is thrown, and probably other errors of this kind are waiting behind it.For QA:
Documentation: