Skip to content

Commit

Permalink
Merge pull request #10221 from nextcloud/bugfix/noid/prevent-too-long…
Browse files Browse the repository at this point in the history
…-identifiers

Prevent too long identifier names
  • Loading branch information
rullzer authored Jul 30, 2018
2 parents 168d6f7 + ef5074a commit f208ce9
Show file tree
Hide file tree
Showing 2 changed files with 421 additions and 2 deletions.
72 changes: 71 additions & 1 deletion lib/private/DB/MigrationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@

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 Doctrine\DBAL\Schema\Sequence;
use OC\IntegrityCheck\Helpers\AppLocator;
use OC\Migration\SimpleOutput;
use OCP\AppFramework\App;
Expand Down Expand Up @@ -450,7 +456,9 @@ public function executeStep($version, $schemaOnly = false) {
}, ['tablePrefix' => $this->connection->getPrefix()]);

if ($toSchema instanceof SchemaWrapper) {
$this->connection->migrateToSchema($toSchema->getWrappedSchema());
$targetSchema = $toSchema->getWrappedSchema();
$this->ensureOracleIdentifierLengthLimit($targetSchema, strlen($this->connection->getPrefix()));
$this->connection->migrateToSchema($targetSchema);
$toSchema->performDropTableCalls();
}

Expand All @@ -463,6 +471,68 @@ public function executeStep($version, $schemaOnly = false) {
$this->markAsExecuted($version);
}

public function ensureOracleIdentifierLengthLimit(Schema $schema, int $prefixLength) {
$sequences = $schema->getSequences();

foreach ($schema->getTables() as $table) {
if (\strlen($table->getName()) - $prefixLength > 27) {
throw new \InvalidArgumentException('Table name "' . $table->getName() . '" is too long.');
}

foreach ($table->getColumns() as $thing) {
if (\strlen($thing->getName()) - $prefixLength > 27) {
throw new \InvalidArgumentException('Column name "' . $table->getName() . '"."' . $thing->getName() . '" is too long.');
}
}

foreach ($table->getIndexes() as $thing) {
if (\strlen($thing->getName()) - $prefixLength > 27) {
throw new \InvalidArgumentException('Index name "' . $table->getName() . '"."' . $thing->getName() . '" is too long.');
}
}

foreach ($table->getForeignKeys() as $thing) {
if (\strlen($thing->getName()) - $prefixLength > 27) {
throw new \InvalidArgumentException('Foreign key name "' . $table->getName() . '"."' . $thing->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() . '_pkey';
$isUsingDefaultName = strtolower($defaultName) === $indexName;

if ($isUsingDefaultName) {
$sequenceName = $table->getName() . '_' . implode('_', $primaryKey->getColumns()) . '_seq';
$sequences = array_filter($sequences, function(Sequence $sequence) use ($sequenceName) {
return $sequence->getName() !== $sequenceName;
});
}
} else if ($this->connection->getDatabasePlatform() instanceof OraclePlatform) {
$defaultName = $table->getName() . '_seq';
$isUsingDefaultName = strtolower($defaultName) === $indexName;
}

if (!$isUsingDefaultName && \strlen($indexName) - $prefixLength > 27) {
throw new \InvalidArgumentException('Primary index name on "' . $table->getName() . '" is too long.');
}
if ($isUsingDefaultName && \strlen($table->getName()) - $prefixLength > 23) {
throw new \InvalidArgumentException('Primary index name on "' . $table->getName() . '" is too long.');
}
}
}

foreach ($sequences as $sequence) {
if (\strlen($sequence->getName()) - $prefixLength > 27) {
throw new \InvalidArgumentException('Sequence name "' . $sequence->getName() . '" is too long.');
}
}
}

private function ensureMigrationsAreLoaded() {
if (empty($this->migrations)) {
$this->migrations = $this->findMigrations();
Expand Down
Loading

0 comments on commit f208ce9

Please sign in to comment.