|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\DBAL\Functional; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Schema\AbstractSchemaManager; |
|
6
|
|
|
use Doctrine\DBAL\Schema\Table; |
|
7
|
|
|
|
|
8
|
|
|
class ResetAutoincrementTest extends \Doctrine\Tests\DbalFunctionalTestCase |
|
9
|
|
|
{ |
|
10
|
|
|
public function testAutoincrementResetsOnTruncate() |
|
11
|
|
|
{ |
|
12
|
|
|
$table = new Table('autoincremented_table'); |
|
13
|
|
|
$table->addColumn('id', 'integer', ['autoincrement' => true]); |
|
14
|
|
|
$table->addColumn('test_int', 'integer'); |
|
15
|
|
|
$table->setPrimaryKey(['id']); |
|
16
|
|
|
|
|
17
|
|
|
/* @var $sm AbstractSchemaManager */ |
|
18
|
|
|
$sm = $this->connection->getSchemaManager(); |
|
19
|
|
|
$sm->createTable($table); |
|
20
|
|
|
|
|
21
|
|
|
$this->connection->insert('autoincremented_table', ['test_int' => 1]); |
|
22
|
|
|
$this->connection->insert('autoincremented_table', ['test_int' => 2]); |
|
23
|
|
|
$this->connection->insert('autoincremented_table', ['test_int' => 3]); |
|
24
|
|
|
|
|
25
|
|
|
$lastId = $this->getIdByTestInt(3); |
|
26
|
|
|
|
|
27
|
|
|
$this->assertEquals(3, $lastId); |
|
28
|
|
|
|
|
29
|
|
|
$this->connection->exec($this->connection->getDatabasePlatform()->getTruncateTableSQL('autoincremented_table')); |
|
30
|
|
|
|
|
31
|
|
|
$this->connection->insert('autoincremented_table', ['test_int' => 4]); |
|
32
|
|
|
$lastId = $this->getIdByTestInt(4); |
|
33
|
|
|
$this->assertEquals(1, $lastId); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
protected function getIdByTestInt(int $whereTestInt) |
|
37
|
|
|
{ |
|
38
|
|
|
$row = $this->connection->fetchAssoc('SELECT id FROM autoincremented_table WHERE test_int = ?', [$whereTestInt]); |
|
39
|
|
|
$row = array_change_key_case($row, \CASE_LOWER); |
|
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
return $row['id']; |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|