CommonContexts
CommonContexts copied to clipboard
Why not truncate tables instead of dropping and recreating schema?
Disabling foreign key checks, truncating all tables and re-enabling foreign key checks is lightning-fast vs. the crawl of dropping and recreating all tables each time. What would be the downsides implementing this way?
# SymfonyDoctrineContext::buildSchema method
$connection = $entityManager->getConnection();
$connection->exec('SET FOREIGN_KEY_CHECKS = 0;');
$schemaManager = $connection->getSchemaManager();
$tables = $schemaManager->listTables();
foreach ($tables as $table) {
$connection->exec(sprintf('TRUNCATE TABLE %s', $table->getName()));
}
$connection->exec('SET FOREIGN_KEY_CHECKS = 1;');
+1
I also have an issue with the fact that schema is dropped an re-create because I use triggers to perform some tasks (mainly to create history for some table, stored inside an another table). As trigger are not managed by doctrine, they are not re-created when schema is re-created.