Skip to content

Commit 92f2192

Browse files
committed
Fix up double truncation causing SQLServer to do silly things.
1 parent e3fcc62 commit 92f2192

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

src/Database/Schema/SqlserverSchema.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -430,17 +430,18 @@ public function createTableSql(Table $table, $columns, $constraints, $indexes) {
430430
public function truncateTableSql(Table $table) {
431431
$name = $this->_driver->quoteIdentifier($table->name());
432432
$queries = [
433-
sprintf('DELETE FROM TABLE %s', $name)
433+
sprintf('DELETE FROM %s', $name)
434434
];
435+
436+
// Restart identity sequences
435437
$pk = $table->primaryKey();
436-
foreach ($pk as $column) {
437-
$column = $table->column($column);
438-
if (!empty($column['autoIncrement'])) {
438+
if (count($pk) === 1) {
439+
$column = $table->column($pk[0]);
440+
if (in_array($column['type'], ['integer', 'biginteger'])) {
439441
$queries[] = sprintf(
440442
'DBCC CHECKIDENT(%s, RESEED, 0)',
441-
$this->_driver->quoteIdentifier($column)
443+
$name
442444
);
443-
break;
444445
}
445446
}
446447
return $queries;

src/TestSuite/Fixture/FixtureManager.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,7 @@ public function load($test) {
230230
foreach ($fixtures as $fixture) {
231231
if (!in_array($db->configName(), (array)$fixture->created)) {
232232
$this->_setupTable($fixture, $db, $tables, $test->dropTables);
233-
}
234-
if (!$test->dropTables) {
233+
} else {
235234
$fixture->truncate($db);
236235
}
237236
}
@@ -325,7 +324,8 @@ public function loadSingle($name, $db = null, $dropTables = true) {
325324
}
326325

327326
if (!in_array($db->configName(), (array)$fixture->created)) {
328-
$this->_setupTable($fixture, $db, $dropTables);
327+
$sources = $db->schemaCollection()->listTables();
328+
$this->_setupTable($fixture, $db, $sources, $dropTables);
329329
}
330330
if (!$dropTables) {
331331
$fixture->truncate($db);

tests/TestCase/Database/Schema/SqlserverSchemaTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,8 @@ public function testListTables() {
269269
$schema = new SchemaCollection($connection);
270270
$result = $schema->listTables();
271271
$this->assertInternalType('array', $result);
272-
$this->assertCount(2, $result);
273-
$this->assertEquals('schema_articles', $result[0]);
274-
$this->assertEquals('schema_authors', $result[1]);
272+
$this->assertContains('schema_articles', $result);
273+
$this->assertContains('schema_authors', $result);
275274
}
276275

277276
/**
@@ -711,7 +710,7 @@ public function testTruncateSql() {
711710
]);
712711
$result = $table->truncateSql($connection);
713712
$this->assertCount(2, $result);
714-
$this->assertEquals('TRUNCATE TABLE [schema_articles]', $result[0]);
713+
$this->assertEquals('DELETE FROM [schema_articles]', $result[0]);
715714
$this->assertEquals('DBCC CHECKIDENT([schema_articles], RESEED, 0)', $result[1]);
716715
}
717716

0 commit comments

Comments
 (0)