2015-03-25 20:31:49 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2015 Vincent Petry <pvince81@owncloud.com>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Test\Repair;
|
2020-04-09 12:48:10 +03:00
|
|
|
|
2016-04-22 16:35:39 +03:00
|
|
|
use OCP\Migration\IOutput;
|
2015-03-25 20:31:49 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests for fixing the SQLite id recycling
|
2015-11-03 03:52:41 +03:00
|
|
|
*
|
|
|
|
* @group DB
|
2015-03-25 20:31:49 +03:00
|
|
|
*/
|
2016-05-20 16:38:20 +03:00
|
|
|
class RepairSqliteAutoincrementTest extends \Test\TestCase {
|
2015-03-25 20:31:49 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \OC\Repair\SqliteAutoincrement
|
|
|
|
*/
|
|
|
|
private $repair;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Doctrine\DBAL\Connection
|
|
|
|
*/
|
|
|
|
private $connection;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $tableName;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \OCP\IConfig
|
|
|
|
*/
|
|
|
|
private $config;
|
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
protected function setUp(): void {
|
2015-03-25 20:31:49 +03:00
|
|
|
parent::setUp();
|
|
|
|
|
2016-01-07 12:26:00 +03:00
|
|
|
$this->connection = \OC::$server->getDatabaseConnection();
|
2015-03-25 20:31:49 +03:00
|
|
|
$this->config = \OC::$server->getConfig();
|
|
|
|
if (!$this->connection->getDatabasePlatform() instanceof \Doctrine\DBAL\Platforms\SqlitePlatform) {
|
|
|
|
$this->markTestSkipped("Test only relevant on Sqlite");
|
|
|
|
}
|
|
|
|
|
|
|
|
$dbPrefix = $this->config->getSystemValue('dbtableprefix', 'oc_');
|
|
|
|
$this->tableName = $this->getUniqueID($dbPrefix . 'autoinc_test');
|
|
|
|
$this->connection->exec('CREATE TABLE ' . $this->tableName . '("someid" INTEGER NOT NULL, "text" VARCHAR(16), PRIMARY KEY("someid"))');
|
|
|
|
|
|
|
|
$this->repair = new \OC\Repair\SqliteAutoincrement($this->connection);
|
|
|
|
}
|
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
protected function tearDown(): void {
|
2015-03-25 20:31:49 +03:00
|
|
|
$this->connection->getSchemaManager()->dropTable($this->tableName);
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests whether autoincrement works
|
|
|
|
*
|
|
|
|
* @return boolean true if autoincrement works, false otherwise
|
|
|
|
*/
|
|
|
|
protected function checkAutoincrement() {
|
|
|
|
$this->connection->executeUpdate('INSERT INTO ' . $this->tableName . ' ("text") VALUES ("test")');
|
|
|
|
$insertId = $this->connection->lastInsertId();
|
2020-03-26 11:30:18 +03:00
|
|
|
$this->connection->executeUpdate('DELETE FROM ' . $this->tableName . ' WHERE "someid" = ?', [$insertId]);
|
2015-03-25 20:31:49 +03:00
|
|
|
|
|
|
|
// insert again
|
|
|
|
$this->connection->executeUpdate('INSERT INTO ' . $this->tableName . ' ("text") VALUES ("test2")');
|
|
|
|
$newInsertId = $this->connection->lastInsertId();
|
|
|
|
|
|
|
|
return ($insertId !== $newInsertId);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testConvertIdColumn() {
|
|
|
|
$this->assertFalse($this->checkAutoincrement());
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IOutput | \PHPUnit\Framework\MockObject\MockObject $outputMock */
|
2016-04-22 16:35:39 +03:00
|
|
|
$outputMock = $this->getMockBuilder('\OCP\Migration\IOutput')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$this->repair->run($outputMock);
|
2015-03-25 20:31:49 +03:00
|
|
|
|
|
|
|
$this->assertTrue($this->checkAutoincrement());
|
|
|
|
}
|
|
|
|
}
|