Skip to content
Permalink
Browse files

Insert data in a different transaction than schema.

Postgres will puke if you try to alter schema and update rows in the
same transaction. While this makes the tests a bit slower, it is
necessary to have foreign key fixtures work with postgres.
  • Loading branch information...
markstory committed Sep 7, 2014
1 parent 4d43914 commit e4dedd759749cab88e5298ac0efc2153b604b4e4
@@ -475,4 +475,18 @@ public function truncateTableSql(Table $table) {
];
}
/**
* Generate the SQL to drop a table.
*
* @param \Cake\Database\Schema\Table $table Table instance
* @return array SQL statements to drop a table.
*/
public function dropTableSql(Table $table) {
$sql = sprintf(
'DROP TABLE %s CASCADE',
$this->_driver->quoteIdentifier($table->name())
);
return [$sql];
}
}
@@ -234,10 +234,17 @@ public function load($test) {
if (!$test->dropTables) {
$fixture->truncate($db);
}
$fixture->insert($db);
}
};
$this->_runOperation($fixtures, $createTables);
// Use a separate transaction because of postgres.
$insert = function($db, $fixtures) {
foreach ($fixtures as $fixture) {
$fixture->insert($db);
}
};
$this->_runOperation($fixtures, $insert);
} catch (\PDOException $e) {
$msg = sprintf('Unable to insert fixtures for "%s" test case. %s', get_class($test), $e->getMessage());
throw new Exception($msg);
@@ -287,7 +294,9 @@ protected function _fixtureConnections($fixtures) {
* @return void
*/
public function unload($test) {
$fixtures = !empty($test->fixtures) ? $test->fixtures : [];
if (empty($test->fixtures)) {
return;
}
$truncate = function($db, $fixtures) {
$connection = $db->configName();
foreach ($fixtures as $fixture) {
@@ -296,7 +305,7 @@ public function unload($test) {
}
}
};
$this->_runOperation($fixtures, $truncate);
$this->_runOperation($test->fixtures, $truncate);
}
/**
@@ -878,7 +878,7 @@ public function testDropSql() {
$table = new Table('schema_articles');
$result = $table->dropSql($connection);
$this->assertCount(1, $result);
$this->assertEquals('DROP TABLE "schema_articles"', $result[0]);
$this->assertEquals('DROP TABLE "schema_articles" CASCADE', $result[0]);
}
/**

0 comments on commit e4dedd7

Please sign in to comment.
You can’t perform that action at this time.