1
0

repairsqliteautoincrement.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * Copyright (c) 2015 Vincent Petry <pvince81@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace Test\Repair;
  9. /**
  10. * Tests for fixing the SQLite id recycling
  11. *
  12. * @group DB
  13. */
  14. class RepairSqliteAutoincrement extends \Test\TestCase {
  15. /**
  16. * @var \OC\Repair\SqliteAutoincrement
  17. */
  18. private $repair;
  19. /**
  20. * @var \Doctrine\DBAL\Connection
  21. */
  22. private $connection;
  23. /**
  24. * @var string
  25. */
  26. private $tableName;
  27. /**
  28. * @var \OCP\IConfig
  29. */
  30. private $config;
  31. protected function setUp() {
  32. parent::setUp();
  33. $this->connection = \OC::$server->getDatabaseConnection();
  34. $this->config = \OC::$server->getConfig();
  35. if (!$this->connection->getDatabasePlatform() instanceof \Doctrine\DBAL\Platforms\SqlitePlatform) {
  36. $this->markTestSkipped("Test only relevant on Sqlite");
  37. }
  38. $dbPrefix = $this->config->getSystemValue('dbtableprefix', 'oc_');
  39. $this->tableName = $this->getUniqueID($dbPrefix . 'autoinc_test');
  40. $this->connection->exec('CREATE TABLE ' . $this->tableName . '("someid" INTEGER NOT NULL, "text" VARCHAR(16), PRIMARY KEY("someid"))');
  41. $this->repair = new \OC\Repair\SqliteAutoincrement($this->connection);
  42. }
  43. protected function tearDown() {
  44. $this->connection->getSchemaManager()->dropTable($this->tableName);
  45. parent::tearDown();
  46. }
  47. /**
  48. * Tests whether autoincrement works
  49. *
  50. * @return boolean true if autoincrement works, false otherwise
  51. */
  52. protected function checkAutoincrement() {
  53. $this->connection->executeUpdate('INSERT INTO ' . $this->tableName . ' ("text") VALUES ("test")');
  54. $insertId = $this->connection->lastInsertId();
  55. $this->connection->executeUpdate('DELETE FROM ' . $this->tableName . ' WHERE "someid" = ?', array($insertId));
  56. // insert again
  57. $this->connection->executeUpdate('INSERT INTO ' . $this->tableName . ' ("text") VALUES ("test2")');
  58. $newInsertId = $this->connection->lastInsertId();
  59. return ($insertId !== $newInsertId);
  60. }
  61. public function testConvertIdColumn() {
  62. $this->assertFalse($this->checkAutoincrement());
  63. $this->repair->run();
  64. $this->assertTrue($this->checkAutoincrement());
  65. }
  66. }