repairsqliteautoincrement.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. class RepairSqliteAutoincrement extends \Test\TestCase {
  13. /**
  14. * @var \OC\Repair\SqliteAutoincrement
  15. */
  16. private $repair;
  17. /**
  18. * @var \Doctrine\DBAL\Connection
  19. */
  20. private $connection;
  21. /**
  22. * @var string
  23. */
  24. private $tableName;
  25. /**
  26. * @var \OCP\IConfig
  27. */
  28. private $config;
  29. protected function setUp() {
  30. parent::setUp();
  31. $this->connection = \OC_DB::getConnection();
  32. $this->config = \OC::$server->getConfig();
  33. if (!$this->connection->getDatabasePlatform() instanceof \Doctrine\DBAL\Platforms\SqlitePlatform) {
  34. $this->markTestSkipped("Test only relevant on Sqlite");
  35. }
  36. $dbPrefix = $this->config->getSystemValue('dbtableprefix', 'oc_');
  37. $this->tableName = $this->getUniqueID($dbPrefix . 'autoinc_test');
  38. $this->connection->exec('CREATE TABLE ' . $this->tableName . '("someid" INTEGER NOT NULL, "text" VARCHAR(16), PRIMARY KEY("someid"))');
  39. $this->repair = new \OC\Repair\SqliteAutoincrement($this->connection);
  40. }
  41. protected function tearDown() {
  42. $this->connection->getSchemaManager()->dropTable($this->tableName);
  43. parent::tearDown();
  44. }
  45. /**
  46. * Tests whether autoincrement works
  47. *
  48. * @return boolean true if autoincrement works, false otherwise
  49. */
  50. protected function checkAutoincrement() {
  51. $this->connection->executeUpdate('INSERT INTO ' . $this->tableName . ' ("text") VALUES ("test")');
  52. $insertId = $this->connection->lastInsertId();
  53. $this->connection->executeUpdate('DELETE FROM ' . $this->tableName . ' WHERE "someid" = ?', array($insertId));
  54. // insert again
  55. $this->connection->executeUpdate('INSERT INTO ' . $this->tableName . ' ("text") VALUES ("test2")');
  56. $newInsertId = $this->connection->lastInsertId();
  57. return ($insertId !== $newInsertId);
  58. }
  59. public function testConvertIdColumn() {
  60. $this->assertFalse($this->checkAutoincrement());
  61. $this->repair->run();
  62. $this->assertTrue($this->checkAutoincrement());
  63. }
  64. }