From 92f219264f2ed627a87d2461e8380635a8163343 Mon Sep 17 00:00:00 2001 From: mark_story Date: Mon, 8 Sep 2014 23:13:02 -0400 Subject: [PATCH] Fix up double truncation causing SQLServer to do silly things. --- src/Database/Schema/SqlserverSchema.php | 13 +++++++------ src/TestSuite/Fixture/FixtureManager.php | 6 +++--- .../Database/Schema/SqlserverSchemaTest.php | 7 +++---- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/Database/Schema/SqlserverSchema.php b/src/Database/Schema/SqlserverSchema.php index 15274768eca..a999f161b1c 100644 --- a/src/Database/Schema/SqlserverSchema.php +++ b/src/Database/Schema/SqlserverSchema.php @@ -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; diff --git a/src/TestSuite/Fixture/FixtureManager.php b/src/TestSuite/Fixture/FixtureManager.php index d6f91184492..1b11d9631cc 100644 --- a/src/TestSuite/Fixture/FixtureManager.php +++ b/src/TestSuite/Fixture/FixtureManager.php @@ -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); } } @@ -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); diff --git a/tests/TestCase/Database/Schema/SqlserverSchemaTest.php b/tests/TestCase/Database/Schema/SqlserverSchemaTest.php index 457720f9131..bd936c4f329 100644 --- a/tests/TestCase/Database/Schema/SqlserverSchemaTest.php +++ b/tests/TestCase/Database/Schema/SqlserverSchemaTest.php @@ -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); } /** @@ -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]); }