Created
January 21, 2017 11:05
-
-
Save nikos-glikis/61b4e0e29185467b09fcf7c714b45e32 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Clean database. Truncates all tables except from everything that loads with fixtures. | |
* Used as a fast clean. | |
*/ | |
public function cleanDB() | |
{ | |
$excludeTables = array(); | |
$em = $this->em; | |
$connection = $em->getConnection(); | |
//exclude tables | |
$excludeTables = array( | |
'ignoreTable1', | |
'ignoreTable2', | |
); | |
//get all tables | |
$query = $connection->prepare('show tables'); | |
$query->execute(); | |
$tables = $query->fetchAll(); | |
//clean data if not match with the $excludedTables array | |
foreach ($tables as $table) { | |
foreach ($table as $tableName) { | |
if (in_array($tableName, $excludeTables) !== true) { | |
$connection->query(' | |
START TRANSACTION; | |
SET FOREIGN_KEY_CHECKS=0; | |
TRUNCATE TABLE ' . $tableName . '; | |
SET FOREIGN_KEY_CHECKS=1; | |
COMMIT; | |
'); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment