Skip to content

Commit

Permalink
Fix up double truncation causing SQLServer to do silly things.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Sep 9, 2014
1 parent e3fcc62 commit 92f2192
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
13 changes: 7 additions & 6 deletions src/Database/Schema/SqlserverSchema.php
Expand Up @@ -430,17 +430,18 @@ public function createTableSql(Table $table, $columns, $constraints, $indexes) {
public function truncateTableSql(Table $table) {
$name = $this->_driver->quoteIdentifier($table->name());
$queries = [
sprintf('DELETE FROM TABLE %s', $name)
sprintf('DELETE FROM %s', $name)
];

// Restart identity sequences
$pk = $table->primaryKey();
foreach ($pk as $column) {
$column = $table->column($column);
if (!empty($column['autoIncrement'])) {
if (count($pk) === 1) {
$column = $table->column($pk[0]);
if (in_array($column['type'], ['integer', 'biginteger'])) {
$queries[] = sprintf(
'DBCC CHECKIDENT(%s, RESEED, 0)',
$this->_driver->quoteIdentifier($column)
$name
);
break;
}
}
return $queries;
Expand Down
6 changes: 3 additions & 3 deletions src/TestSuite/Fixture/FixtureManager.php
Expand Up @@ -230,8 +230,7 @@ public function load($test) {
foreach ($fixtures as $fixture) {
if (!in_array($db->configName(), (array)$fixture->created)) {
$this->_setupTable($fixture, $db, $tables, $test->dropTables);
}
if (!$test->dropTables) {
} else {
$fixture->truncate($db);
}
}
Expand Down Expand Up @@ -325,7 +324,8 @@ public function loadSingle($name, $db = null, $dropTables = true) {
}

if (!in_array($db->configName(), (array)$fixture->created)) {
$this->_setupTable($fixture, $db, $dropTables);
$sources = $db->schemaCollection()->listTables();
$this->_setupTable($fixture, $db, $sources, $dropTables);
}
if (!$dropTables) {
$fixture->truncate($db);
Expand Down
7 changes: 3 additions & 4 deletions tests/TestCase/Database/Schema/SqlserverSchemaTest.php
Expand Up @@ -269,9 +269,8 @@ public function testListTables() {
$schema = new SchemaCollection($connection);
$result = $schema->listTables();
$this->assertInternalType('array', $result);
$this->assertCount(2, $result);
$this->assertEquals('schema_articles', $result[0]);
$this->assertEquals('schema_authors', $result[1]);
$this->assertContains('schema_articles', $result);
$this->assertContains('schema_authors', $result);
}

/**
Expand Down Expand Up @@ -711,7 +710,7 @@ public function testTruncateSql() {
]);
$result = $table->truncateSql($connection);
$this->assertCount(2, $result);
$this->assertEquals('TRUNCATE TABLE [schema_articles]', $result[0]);
$this->assertEquals('DELETE FROM [schema_articles]', $result[0]);
$this->assertEquals('DBCC CHECKIDENT([schema_articles], RESEED, 0)', $result[1]);
}

Expand Down

0 comments on commit 92f2192

Please sign in to comment.