diff options
author | Joas Schilling <coding@schilljs.com> | 2018-07-19 10:28:52 +0200 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2018-07-27 14:45:21 +0200 |
commit | 960961148ed2bea4818ca1abf8484ef3bf238f64 (patch) | |
tree | 9d498d10a6b4b3bf038b3b468745702df6f20878 | |
parent | 14682dfa7c1f846e3094b4f6bcd558681b8f5b9f (diff) | |
download | nextcloud-server-960961148ed2bea4818ca1abf8484ef3bf238f64.tar.gz nextcloud-server-960961148ed2bea4818ca1abf8484ef3bf238f64.zip |
Fix calculation of default name
Signed-off-by: Joas Schilling <coding@schilljs.com>
-rw-r--r-- | lib/private/DB/MigrationService.php | 26 | ||||
-rw-r--r-- | tests/lib/DB/MigrationsTest.php | 63 |
2 files changed, 83 insertions, 6 deletions
diff --git a/lib/private/DB/MigrationService.php b/lib/private/DB/MigrationService.php index b2ec3c13ba1..99b8575cd97 100644 --- a/lib/private/DB/MigrationService.php +++ b/lib/private/DB/MigrationService.php @@ -23,6 +23,10 @@ namespace OC\DB; +use Doctrine\DBAL\Platforms\OraclePlatform; +use Doctrine\DBAL\Platforms\PostgreSqlPlatform; +use Doctrine\DBAL\Schema\Column; +use Doctrine\DBAL\Schema\Index; use Doctrine\DBAL\Schema\Schema; use Doctrine\DBAL\Schema\SchemaException; use OC\IntegrityCheck\Helpers\AppLocator; @@ -490,9 +494,25 @@ class MigrationService { } } - $thing = $table->getPrimaryKey(); - if ($thing && (\strlen($table->getName()) > 26 || \strlen($thing->getName()) > 30)) { - throw new \InvalidArgumentException('Primary index name on "' . $table->getName() . '" is too long.'); + $primaryKey = $table->getPrimaryKey(); + if ($primaryKey instanceof Index) { + $indexName = strtolower($primaryKey->getName()); + $isUsingDefaultName = $indexName === 'primary'; + + if ($this->connection->getDatabasePlatform() instanceof PostgreSqlPlatform) { + $defaultName = $table->getName() . '_' . implode('_', $primaryKey->getColumns()) . '_seq'; + $isUsingDefaultName = strtolower($defaultName) === $indexName; + } else if ($this->connection->getDatabasePlatform() instanceof OraclePlatform) { + $defaultName = $table->getName() . '_seq'; + $isUsingDefaultName = strtolower($defaultName) === $indexName; + } + + if (!$isUsingDefaultName && \strlen($indexName) > 30) { + throw new \InvalidArgumentException('Primary index name on "' . $table->getName() . '" is too long.'); + } + if ($isUsingDefaultName && \strlen($table->getName()) > 26) { + throw new \InvalidArgumentException('Primary index name on "' . $table->getName() . '" is too long.'); + } } } diff --git a/tests/lib/DB/MigrationsTest.php b/tests/lib/DB/MigrationsTest.php index 35e1d9ce391..95656e3feda 100644 --- a/tests/lib/DB/MigrationsTest.php +++ b/tests/lib/DB/MigrationsTest.php @@ -10,6 +10,8 @@ namespace Test\DB; +use Doctrine\DBAL\Platforms\OraclePlatform; +use Doctrine\DBAL\Platforms\PostgreSqlPlatform; use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\ForeignKeyConstraint; use Doctrine\DBAL\Schema\Index; @@ -271,12 +273,57 @@ class MigrationsTest extends \Test\TestCase { public function testEnsureOracleIdentifierLengthLimitValidWithPrimaryKey() { $index = $this->createMock(Index::class); - $index->expects($this->once()) + $index->expects($this->any()) ->method('getName') ->willReturn(\str_repeat('a', 30)); $table = $this->createMock(Table::class); - $table->expects($this->exactly(2)) + $table->expects($this->any()) + ->method('getName') + ->willReturn(\str_repeat('a', 26)); + + $table->expects($this->once()) + ->method('getColumns') + ->willReturn([]); + $table->expects($this->once()) + ->method('getIndexes') + ->willReturn([]); + $table->expects($this->once()) + ->method('getForeignKeys') + ->willReturn([]); + $table->expects($this->once()) + ->method('getPrimaryKey') + ->willReturn($index); + + $schema = $this->createMock(Schema::class); + $schema->expects($this->once()) + ->method('getTables') + ->willReturn([$table]); + $schema->expects($this->once()) + ->method('getSequences') + ->willReturn([]); + + self::invokePrivate($this->migrationService, 'ensureOracleIdentifierLengthLimit', [$schema]); + } + + public function testEnsureOracleIdentifierLengthLimitValidWithPrimaryKeyDefault() { + $defaultName = 'PRIMARY'; + if ($this->db->getDatabasePlatform() instanceof PostgreSqlPlatform) { + $defaultName = \str_repeat('a', 26) . '_' . \str_repeat('b', 30) . '_seq'; + } else if ($this->db->getDatabasePlatform() instanceof OraclePlatform) { + $defaultName = \str_repeat('a', 26) . '_seq'; + } + + $index = $this->createMock(Index::class); + $index->expects($this->any()) + ->method('getName') + ->willReturn($defaultName); + $index->expects($this->any()) + ->method('getColumns') + ->willReturn([\str_repeat('b', 30)]); + + $table = $this->createMock(Table::class); + $table->expects($this->any()) ->method('getName') ->willReturn(\str_repeat('a', 26)); @@ -325,10 +372,20 @@ class MigrationsTest extends \Test\TestCase { * @expectedException \InvalidArgumentException */ public function testEnsureOracleIdentifierLengthLimitTooLongPrimaryWithDefault() { + $defaultName = 'PRIMARY'; + if ($this->db->getDatabasePlatform() instanceof PostgreSqlPlatform) { + $defaultName = \str_repeat('a', 27) . '_' . \str_repeat('b', 30) . '_seq'; + } else if ($this->db->getDatabasePlatform() instanceof OraclePlatform) { + $defaultName = \str_repeat('a', 27) . '_seq'; + } + $index = $this->createMock(Index::class); $index->expects($this->any()) ->method('getName') - ->willReturn(\str_repeat('a', 30)); + ->willReturn($defaultName); + $index->expects($this->any()) + ->method('getColumns') + ->willReturn([\str_repeat('b', 30)]); $table = $this->createMock(Table::class); $table->expects($this->any()) |