+
Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$schemaManager = $this->connection->getSchemaManager();
if (!empty($schemaManager->listTables())) {
$io = new SymfonyStyle($input, $output);
if (!$io->confirm('Running this command will delete data in all Ibexa generated tables. Continue?', )) {
if (!$io->confirm('Running this command will delete data in all Ibexa generated tables. Continue?', false)) {
return 0;
}
}
Expand Down
15 changes: 12 additions & 3 deletions src/bundle/RepositoryInstaller/Installer/CoreInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,18 @@ protected function getDropSqlStatementsForExistingSchema(
): array {
$existingSchema = $this->db->getSchemaManager()->createSchema();
$statements = [];
// reverse table order for clean-up (due to FKs)
$tables = array_reverse($newSchema->getTables());
// cleanup pre-existing database
$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());
}
}
}
}
Comment on lines +102 to +112
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member

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:

  1. Unit test coverage for CoreInstaller. I can help with that if it's too complicated.
  2. 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.

// cleanup pre-existing database: drop tables
foreach ($tables as $table) {
if ($existingSchema->hasTable($table->getName())) {
$statements[] = $databasePlatform->getDropTableSQL($table);
Expand Down
Loading
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载