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
Open
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 21 additions & 1 deletion lib/Doctrine/Common/DataFixtures/Purger/ORMPurger.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Doctrine\Common\DataFixtures\Purger;

use Doctrine\Common\DataFixtures\Sorter\TopologicalSorter;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\AbstractMySQLDriver;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Schema\Identifier;
use Doctrine\ORM\EntityManagerInterface;
Expand Down Expand Up @@ -160,7 +162,7 @@ public function purge()
if ($this->purgeMode === self::PURGE_MODE_DELETE) {
$connection->executeStatement($this->getDeleteFromTableSQL($tbl, $platform));
} else {
$connection->executeStatement($platform->getTruncateTableSQL($tbl, true));
$connection->executeStatement($this->getTruncateTableSQL($platform, $connection, $tbl));
}
}
}
Expand Down Expand Up @@ -280,4 +282,22 @@ private function getDeleteFromTableSQL(string $tableName, AbstractPlatform $plat

return 'DELETE FROM ' . $tableIdentifier->getQuotedName($platform);
}

/**
* @param AbstractPlatform $platform
* @param Connection $connection
* @param string $tbl
*
* @return string
*/
private function getTruncateTableSQL(AbstractPlatform $platform, Connection $connection, $tbl)
croensch marked this conversation as resolved.
Show resolved Hide resolved
{
$sql = $platform->getTruncateTableSQL($tbl, true);

if ($connection->getDriver() instanceof AbstractMySQLDriver) {
croensch marked this conversation as resolved.
Show resolved Hide resolved
$sql = 'SET FOREIGN_KEY_CHECKS = 0;'.$sql.';SET FOREIGN_KEY_CHECKS = 1;';
}

return $sql;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace Doctrine\Tests\Common\DataFixtures;

use Doctrine\Common\DataFixtures\Purger\ORMPurger;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\AbstractMySQLDriver;
use Doctrine\DBAL\Driver\AbstractSQLiteDriver;
use ReflectionClass;

/**
* @author Robert Freigang <[email protected]>
*
* @covers ORMPurger::getTruncateTableSQL
croensch marked this conversation as resolved.
Show resolved Hide resolved
*/
class ORMPurgerForeignKeyCheckTest extends BaseTest
{
const FOREIGN_KEY_CHECK_STRING_START = 'SET FOREIGN_KEY_CHECKS = 0;';
const FOREIGN_KEY_CHECK_STRING_END = ';SET FOREIGN_KEY_CHECKS = 1;';
const TEST_TABLE_NAME = 'test_table_name';

/**
* @param Driver $driver
* @param bool $hasForeignKeyCheckString
*
* @dataProvider purgeForDifferentDriversProvider
*/
public function testPurgeForDifferentDrivers(Driver $driver, $hasForeignKeyCheckString)
croensch marked this conversation as resolved.
Show resolved Hide resolved
{
$truncateTableSQL = $this->getTruncateTableSQLForDriver($driver);

if ($hasForeignKeyCheckString) {
$this->assertStringStartsWith(self::FOREIGN_KEY_CHECK_STRING_START, $truncateTableSQL);
$this->assertStringEndsWith(self::FOREIGN_KEY_CHECK_STRING_END, $truncateTableSQL);
} else {
$this->assertNotContains(self::FOREIGN_KEY_CHECK_STRING_START, $truncateTableSQL);
$this->assertNotContains(self::FOREIGN_KEY_CHECK_STRING_END, $truncateTableSQL);
}

$this->assertContains(self::TEST_TABLE_NAME, $truncateTableSQL);
}

/**
* @return array
croensch marked this conversation as resolved.
Show resolved Hide resolved
*/
public function purgeForDifferentDriversProvider()
{
return [
[$this->createMock(AbstractMySQLDriver::class), true],
[$this->createMock(AbstractSQLiteDriver::class), false],
];
}

/**
* @param Driver $driver
*
* @return string
*/
private function getTruncateTableSQLForDriver(Driver $driver)
croensch marked this conversation as resolved.
Show resolved Hide resolved
{
$em = $this->getMockAnnotationReaderEntityManager();

$platform = $em->getConnection()->getDatabasePlatform();

$connection = $this->createMock(Connection::class);
$connection->method('getDriver')->willReturn($driver);

$purger = new ORMPurger($em);
$purgerClass = new ReflectionClass(ORMPurger::class);
$getTruncateTableSQLMethod = $purgerClass->getMethod('getTruncateTableSQL');
$getTruncateTableSQLMethod->setAccessible(true);

$truncateTableSQL = $getTruncateTableSQLMethod->invokeArgs(
$purger,
[$platform, $connection, self::TEST_TABLE_NAME]
);

return $truncateTableSQL;
}
}