Skip to content

Instantly share code, notes, and snippets.

@nikos-glikis
Created January 21, 2017 11:05
Show Gist options
  • Save nikos-glikis/61b4e0e29185467b09fcf7c714b45e32 to your computer and use it in GitHub Desktop.
Save nikos-glikis/61b4e0e29185467b09fcf7c714b45e32 to your computer and use it in GitHub Desktop.
/**
* 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