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

Disable foreign key checks on purge for all mysql versions #407

Open
wants to merge 10 commits into
base: 1.7.x
Choose a base branch
from

Conversation

croensch
Copy link

@croensch croensch commented Oct 2, 2022

For your consideration, see #272. I don't know if the discussion was a yes/no to this feature.

I can push a little refactor of getTruncateTableSQL() to make it fit better.

... when purge mode is truncate

* add test for change of foreign key check when using mysql
* add test for not set foreign key check when using sqlite
croensch and others added 4 commits October 2, 2022 20:11
as suggested

Co-authored-by: Grégoire Paris <postmaster@greg0ire.fr>
@croensch
Copy link
Author

croensch commented Oct 9, 2022

I initially picked up and rebased this PR to revive or end the discussion around this problem. Now that i looked into it, i understand a few things better:

  • The ORMPurger touches all tables anyway, so for MySQL where the TRUNCATE is a drop/re-create internally, it should theoretically not be much faster than the current workaround of running the drop & create commands. Still, it would be nice if this feature just works on MySQL too.
  • In AliceDataFixtures they implemented a fix referencing the original PR. We should not break that, i don't think it does, but it got me thinking... https://github.com/theofidry/AliceDataFixtures/blob/master/src/Bridge/Doctrine/Purger/Purger.php
  • So i like how DBUnit restores the variables state, perfectly fine pattern to be least-invasive. https://github.com/sebastianbergmann/dbunit/blob/master/src/Operation/Truncate.php
  • In DBUnit but also this PR i don't like how it's done around every TRUNCATE statement as it may spam the logger. I would rewrite it to call a method (like in DBUnit) before and after the tables loop.
  • I still think it's a good idea to sniff the connection instead of the platform since only AbstractMySQLDriver is the same since 3.0 while the AbstractMySQLPlatform is only available since DBAL 3.3, but 3.0 is the minimum dependency.

So would you mind if i rewrite it to address all this? In the same PR?

@greg0ire
Copy link
Member

greg0ire commented Oct 9, 2022

DBAL 3.3, but 3.0 is the minimum dependency.

I think it's fine to bump the minimum dependency to 3.3, sniffing the connection driver (why did you mention the connection?) is not a great idea because of middlewares that can wrap the driver in an arbitrary number of decorators.

So would you mind if i rewrite it to address all this? In the same PR?

Sure, go ahead 🙂

- sniff platform instead of driver
- restore variable in MySQL session
- only enable/disable once per purge
croensch and others added 2 commits October 11, 2022 00:08
as suggested

Co-authored-by: Grégoire Paris <postmaster@greg0ire.fr>
Co-authored-by: Grégoire Paris <postmaster@greg0ire.fr>
@croensch
Copy link
Author

Sorry for the spam. I got nothing to add or change - it's finished for me.

Regarding the DBAL (Common?) dependency i also check the previous class in a few easy-to-delete lines. Just in case you want to postpone upping the dependency.

@greg0ire
Copy link
Member

Reviewers: to be squash-merged

@croensch
Copy link
Author

croensch commented Nov 8, 2023

@SenseException @derrabus can we get another look at this?

Copy link
Member

@SenseException SenseException left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having a functional test with the foreign keys would be a nice-to-have, even though I don't expect breaking changes in the methods.

@derrabus derrabus changed the base branch from 1.5.x to 1.7.x November 13, 2023 18:51
@derrabus derrabus changed the title disable foreign key checks on purge for all mysql versions [rebased onto 1.5.x] Disable foreign key checks on purge for all mysql versions Nov 13, 2023
Copy link
Member

@derrabus derrabus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 1.5.x branch is closed for features. We need to target 1.7.x now.

$platform = $connection->getDatabasePlatform();

if (! class_exists(AbstractMySQLPlatform::class)) {
// before DBAL 3.3
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to support ancient DBAL versions anymore.

@@ -163,6 +169,8 @@ public function purge()
$connection->executeStatement($platform->getTruncateTableSQL($tbl, true));
}
}

$this->enableForeignKeyChecksForMySQL($connection);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If anything inside the foreach throws, we leave the connection in a state of disabled foreign key checks, don't we? We should do something about that.

public const TEST_TABLE_NAME = 'link';

/** @return MappingDriver&MockObject */
protected function getMockMetadataDriver(): MappingDriver
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
protected function getMockMetadataDriver(): MappingDriver
private function getMockMetadataDriver(): MappingDriver

}

/** @return Connection&MockObject */
protected function getMockConnectionForPlatform(AbstractPlatform $platform): Connection
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
protected function getMockConnectionForPlatform(AbstractPlatform $platform): Connection
private function getMockConnectionForPlatform(AbstractPlatform $platform): Connection

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants