Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only enforce schema limits for supported apps #10555

Merged
merged 3 commits into from
Jan 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 39 additions & 13 deletions lib/private/DB/MigrationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@

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 Doctrine\DBAL\Schema\Table;
use OC\App\InfoParser;
use OC\IntegrityCheck\Helpers\AppLocator;
use OC\Migration\SimpleOutput;
use OCP\AppFramework\App;
Expand All @@ -51,6 +52,8 @@ class MigrationService {
private $connection;
/** @var string */
private $appName;
/** @var bool */
private $checkOracle;

/**
* MigrationService constructor.
Expand All @@ -72,6 +75,7 @@ public function __construct($appName, IDBConnection $connection, IOutput $output
if ($appName === 'core') {
$this->migrationsPath = \OC::$SERVERROOT . '/core/Migrations';
$this->migrationsNamespace = 'OC\\Core\\Migrations';
$this->checkOracle = true;
} else {
if (null === $appLocator) {
$appLocator = new AppLocator();
Expand All @@ -80,6 +84,21 @@ public function __construct($appName, IDBConnection $connection, IOutput $output
$namespace = App::buildAppNamespace($appName);
$this->migrationsPath = "$appPath/lib/Migration";
$this->migrationsNamespace = $namespace . '\\Migration';

$infoParser = new InfoParser();
$info = $infoParser->parse($appPath . '/appinfo/info.xml');
if (!isset($info['dependencies']['database'])) {
$this->checkOracle = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so by default we check always check oracle?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah if the author did not define a db, you can install it on any. It a db is given the dependency check ensures it. So that mathes the logic here

} else {
$this->checkOracle = false;
foreach ($info['dependencies']['database'] as $database) {
if (\is_string($database) && $database === 'oci') {
$this->checkOracle = true;
} else if (\is_array($database) && isset($database['@value']) && $database['@value'] === 'oci') {
$this->checkOracle = true;
}
}
}
}
}

Expand Down Expand Up @@ -457,8 +476,10 @@ public function executeStep($version, $schemaOnly = false) {

if ($toSchema instanceof SchemaWrapper) {
$targetSchema = $toSchema->getWrappedSchema();
// TODO re-enable once stable14 is branched of: https://github.com/nextcloud/server/issues/10518
// $this->ensureOracleIdentifierLengthLimit($targetSchema, strlen($this->connection->getPrefix()));
if ($this->checkOracle) {
$sourceSchema = $this->connection->createSchema();
$this->ensureOracleIdentifierLengthLimit($sourceSchema, $targetSchema, strlen($this->connection->getPrefix()));
}
$this->connection->migrateToSchema($targetSchema);
$toSchema->performDropTableCalls();
}
Expand All @@ -472,34 +493,39 @@ public function executeStep($version, $schemaOnly = false) {
$this->markAsExecuted($version);
}

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

foreach ($schema->getTables() as $table) {
if (\strlen($table->getName()) - $prefixLength > 27) {
throw new \InvalidArgumentException('Table name "' . $table->getName() . '" is too long.');
foreach ($targetSchema->getTables() as $table) {
try {
$sourceTable = $sourceSchema->getTable($table->getName());
} catch (SchemaException $e) {
if (\strlen($table->getName()) - $prefixLength > 27) {
throw new \InvalidArgumentException('Table name "' . $table->getName() . '" is too long.');
}
$sourceTable = null;
}

foreach ($table->getColumns() as $thing) {
if (\strlen($thing->getName()) - $prefixLength > 27) {
if ((!$sourceTable instanceof Table || !$sourceTable->hasColumn($thing->getName())) && \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) {
if ((!$sourceTable instanceof Table || !$sourceTable->hasIndex($thing->getName())) && \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) {
if ((!$sourceTable instanceof Table || !$sourceTable->hasForeignKey($thing->getName())) && \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) {
if ($primaryKey instanceof Index && (!$sourceTable instanceof Table || !$sourceTable->hasPrimaryKey())) {
$indexName = strtolower($primaryKey->getName());
$isUsingDefaultName = $indexName === 'primary';

Expand Down Expand Up @@ -528,7 +554,7 @@ public function ensureOracleIdentifierLengthLimit(Schema $schema, int $prefixLen
}

foreach ($sequences as $sequence) {
if (\strlen($sequence->getName()) - $prefixLength > 27) {
if (!$sourceSchema->hasSequence($sequence->getName()) && \strlen($sequence->getName()) - $prefixLength > 27) {
throw new \InvalidArgumentException('Sequence name "' . $sequence->getName() . '" is too long.');
}
}
Expand Down
110 changes: 95 additions & 15 deletions tests/lib/DB/MigrationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\SchemaException;
use Doctrine\DBAL\Schema\Sequence;
use Doctrine\DBAL\Schema\Table;
use OC\DB\Connection;
Expand Down Expand Up @@ -102,13 +103,12 @@ public function testExecuteStepWithSchemaChange() {
->method('migrateToSchema');

$wrappedSchema = $this->createMock(Schema::class);
// TODO re-enable once stable14 is branched of: https://github.com/nextcloud/server/issues/10518
/*$wrappedSchema->expects($this->once())
$wrappedSchema->expects($this->once())
->method('getTables')
->willReturn([]);
$wrappedSchema->expects($this->once())
->method('getSequences')
->willReturn([]);*/
->willReturn([]);

$schemaResult = $this->createMock(SchemaWrapper::class);
$schemaResult->expects($this->once())
Expand Down Expand Up @@ -239,12 +239,12 @@ public function testEnsureOracleIdentifierLengthLimitValid() {
->willReturn(\str_repeat('a', 30));

$table = $this->createMock(Table::class);
$table->expects($this->once())
$table->expects($this->atLeastOnce())
->method('getName')
->willReturn(\str_repeat('a', 30));

$sequence = $this->createMock(Sequence::class);
$sequence->expects($this->once())
$sequence->expects($this->atLeastOnce())
->method('getName')
->willReturn(\str_repeat('a', 30));

Expand All @@ -269,7 +269,15 @@ public function testEnsureOracleIdentifierLengthLimitValid() {
->method('getSequences')
->willReturn([$sequence]);

self::invokePrivate($this->migrationService, 'ensureOracleIdentifierLengthLimit', [$schema, 3]);
$sourceSchema = $this->createMock(Schema::class);
$sourceSchema->expects($this->any())
->method('getTable')
->willThrowException(new SchemaException());
$sourceSchema->expects($this->any())
->method('hasSequence')
->willReturn(false);

self::invokePrivate($this->migrationService, 'ensureOracleIdentifierLengthLimit', [$sourceSchema, $schema, 3]);
}

public function testEnsureOracleIdentifierLengthLimitValidWithPrimaryKey() {
Expand Down Expand Up @@ -304,7 +312,15 @@ public function testEnsureOracleIdentifierLengthLimitValidWithPrimaryKey() {
->method('getSequences')
->willReturn([]);

self::invokePrivate($this->migrationService, 'ensureOracleIdentifierLengthLimit', [$schema, 3]);
$sourceSchema = $this->createMock(Schema::class);
$sourceSchema->expects($this->any())
->method('getTable')
->willThrowException(new SchemaException());
$sourceSchema->expects($this->any())
->method('hasSequence')
->willReturn(false);

self::invokePrivate($this->migrationService, 'ensureOracleIdentifierLengthLimit', [$sourceSchema, $schema, 3]);
}

public function testEnsureOracleIdentifierLengthLimitValidWithPrimaryKeyDefault() {
Expand Down Expand Up @@ -349,7 +365,15 @@ public function testEnsureOracleIdentifierLengthLimitValidWithPrimaryKeyDefault(
->method('getSequences')
->willReturn([]);

self::invokePrivate($this->migrationService, 'ensureOracleIdentifierLengthLimit', [$schema, 3]);
$sourceSchema = $this->createMock(Schema::class);
$sourceSchema->expects($this->any())
->method('getTable')
->willThrowException(new SchemaException());
$sourceSchema->expects($this->any())
->method('hasSequence')
->willReturn(false);

self::invokePrivate($this->migrationService, 'ensureOracleIdentifierLengthLimit', [$sourceSchema, $schema, 3]);
}

/**
Expand All @@ -366,7 +390,15 @@ public function testEnsureOracleIdentifierLengthLimitTooLongTableName() {
->method('getTables')
->willReturn([$table]);

self::invokePrivate($this->migrationService, 'ensureOracleIdentifierLengthLimit', [$schema, 3]);
$sourceSchema = $this->createMock(Schema::class);
$sourceSchema->expects($this->any())
->method('getTable')
->willThrowException(new SchemaException());
$sourceSchema->expects($this->any())
->method('hasSequence')
->willReturn(false);

self::invokePrivate($this->migrationService, 'ensureOracleIdentifierLengthLimit', [$sourceSchema, $schema, 3]);
}

/**
Expand Down Expand Up @@ -411,7 +443,15 @@ public function testEnsureOracleIdentifierLengthLimitTooLongPrimaryWithDefault()
->method('getTables')
->willReturn([$table]);

self::invokePrivate($this->migrationService, 'ensureOracleIdentifierLengthLimit', [$schema, 3]);
$sourceSchema = $this->createMock(Schema::class);
$sourceSchema->expects($this->any())
->method('getTable')
->willThrowException(new SchemaException());
$sourceSchema->expects($this->any())
->method('hasSequence')
->willReturn(false);

self::invokePrivate($this->migrationService, 'ensureOracleIdentifierLengthLimit', [$sourceSchema, $schema, 3]);
}

/**
Expand Down Expand Up @@ -446,7 +486,15 @@ public function testEnsureOracleIdentifierLengthLimitTooLongPrimaryWithName() {
->method('getTables')
->willReturn([$table]);

self::invokePrivate($this->migrationService, 'ensureOracleIdentifierLengthLimit', [$schema, 3]);
$sourceSchema = $this->createMock(Schema::class);
$sourceSchema->expects($this->any())
->method('getTable')
->willThrowException(new SchemaException());
$sourceSchema->expects($this->any())
->method('hasSequence')
->willReturn(false);

self::invokePrivate($this->migrationService, 'ensureOracleIdentifierLengthLimit', [$sourceSchema, $schema, 3]);
}

/**
Expand All @@ -472,7 +520,15 @@ public function testEnsureOracleIdentifierLengthLimitTooLongColumnName() {
->method('getTables')
->willReturn([$table]);

self::invokePrivate($this->migrationService, 'ensureOracleIdentifierLengthLimit', [$schema, 3]);
$sourceSchema = $this->createMock(Schema::class);
$sourceSchema->expects($this->any())
->method('getTable')
->willThrowException(new SchemaException());
$sourceSchema->expects($this->any())
->method('hasSequence')
->willReturn(false);

self::invokePrivate($this->migrationService, 'ensureOracleIdentifierLengthLimit', [$sourceSchema, $schema, 3]);
}

/**
Expand Down Expand Up @@ -501,7 +557,15 @@ public function testEnsureOracleIdentifierLengthLimitTooLongIndexName() {
->method('getTables')
->willReturn([$table]);

self::invokePrivate($this->migrationService, 'ensureOracleIdentifierLengthLimit', [$schema, 3]);
$sourceSchema = $this->createMock(Schema::class);
$sourceSchema->expects($this->any())
->method('getTable')
->willThrowException(new SchemaException());
$sourceSchema->expects($this->any())
->method('hasSequence')
->willReturn(false);

self::invokePrivate($this->migrationService, 'ensureOracleIdentifierLengthLimit', [$sourceSchema, $schema, 3]);
}

/**
Expand Down Expand Up @@ -533,7 +597,15 @@ public function testEnsureOracleIdentifierLengthLimitTooLongForeignKeyName() {
->method('getTables')
->willReturn([$table]);

self::invokePrivate($this->migrationService, 'ensureOracleIdentifierLengthLimit', [$schema, 3]);
$sourceSchema = $this->createMock(Schema::class);
$sourceSchema->expects($this->any())
->method('getTable')
->willThrowException(new SchemaException());
$sourceSchema->expects($this->any())
->method('hasSequence')
->willReturn(false);

self::invokePrivate($this->migrationService, 'ensureOracleIdentifierLengthLimit', [$sourceSchema, $schema, 3]);
}

/**
Expand All @@ -553,6 +625,14 @@ public function testEnsureOracleIdentifierLengthLimitTooLongSequenceName() {
->method('getSequences')
->willReturn([$sequence]);

self::invokePrivate($this->migrationService, 'ensureOracleIdentifierLengthLimit', [$schema, 3]);
$sourceSchema = $this->createMock(Schema::class);
$sourceSchema->expects($this->any())
->method('getTable')
->willThrowException(new SchemaException());
$sourceSchema->expects($this->any())
->method('hasSequence')
->willReturn(false);

self::invokePrivate($this->migrationService, 'ensureOracleIdentifierLengthLimit', [$sourceSchema, $schema, 3]);
}
}