123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735 |
- <?php
- namespace OC\DB;
- 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;
- use OCP\AppFramework\QueryException;
- use OCP\DB\ISchemaWrapper;
- use OCP\DB\Types;
- use OCP\IDBConnection;
- use OCP\Migration\IMigrationStep;
- use OCP\Migration\IOutput;
- use OCP\Server;
- use Psr\Log\LoggerInterface;
- class MigrationService {
- private bool $migrationTableCreated;
- private array $migrations;
- private string $migrationsPath;
- private string $migrationsNamespace;
- private IOutput $output;
- private LoggerInterface $logger;
- private Connection $connection;
- private string $appName;
- private bool $checkOracle;
-
- public function __construct(string $appName, Connection $connection, ?IOutput $output = null, ?AppLocator $appLocator = null, ?LoggerInterface $logger = null) {
- $this->appName = $appName;
- $this->connection = $connection;
- if ($logger === null) {
- $this->logger = Server::get(LoggerInterface::class);
- } else {
- $this->logger = $logger;
- }
- if ($output === null) {
- $this->output = new SimpleOutput($this->logger, $appName);
- } else {
- $this->output = $output;
- }
- if ($appName === 'core') {
- $this->migrationsPath = \OC::$SERVERROOT . '/core/Migrations';
- $this->migrationsNamespace = 'OC\\Core\\Migrations';
- $this->checkOracle = true;
- } else {
- if ($appLocator === null) {
- $appLocator = new AppLocator();
- }
- $appPath = $appLocator->getAppPath($appName);
- $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;
- } else {
- $this->checkOracle = false;
- foreach ($info['dependencies']['database'] as $database) {
- if (\is_string($database) && $database === 'oci') {
- $this->checkOracle = true;
- } elseif (\is_array($database) && isset($database['@value']) && $database['@value'] === 'oci') {
- $this->checkOracle = true;
- }
- }
- }
- }
- $this->migrationTableCreated = false;
- }
-
- public function getApp(): string {
- return $this->appName;
- }
-
- private function createMigrationTable(): bool {
- if ($this->migrationTableCreated) {
- return false;
- }
- if ($this->connection->tableExists('migrations') && \OC::$server->getConfig()->getAppValue('core', 'vendor', '') !== 'owncloud') {
- $this->migrationTableCreated = true;
- return false;
- }
- $schema = new SchemaWrapper($this->connection);
-
- try {
- $table = $schema->getTable('migrations');
- $columns = $table->getColumns();
- if (count($columns) === 2) {
- try {
- $column = $table->getColumn('app');
- $schemaMismatch = $column->getLength() !== 255;
- if (!$schemaMismatch) {
- $column = $table->getColumn('version');
- $schemaMismatch = $column->getLength() !== 255;
- }
- } catch (SchemaException $e) {
-
- $schemaMismatch = true;
- }
- if (!$schemaMismatch) {
-
- $this->migrationTableCreated = true;
- return false;
- }
- }
-
- $this->connection->dropTable('migrations');
-
- $schema = new SchemaWrapper($this->connection);
- } catch (SchemaException $e) {
-
- }
- $table = $schema->createTable('migrations');
- $table->addColumn('app', Types::STRING, ['length' => 255]);
- $table->addColumn('version', Types::STRING, ['length' => 255]);
- $table->setPrimaryKey(['app', 'version']);
- $this->connection->migrateToSchema($schema->getWrappedSchema());
- $this->migrationTableCreated = true;
- return true;
- }
-
- public function getMigratedVersions() {
- $this->createMigrationTable();
- $qb = $this->connection->getQueryBuilder();
- $qb->select('version')
- ->from('migrations')
- ->where($qb->expr()->eq('app', $qb->createNamedParameter($this->getApp())))
- ->orderBy('version');
- $result = $qb->executeQuery();
- $rows = $result->fetchAll(\PDO::FETCH_COLUMN);
- $result->closeCursor();
- usort($rows, $this->sortMigrations(...));
- return $rows;
- }
-
- public function getAvailableVersions(): array {
- $this->ensureMigrationsAreLoaded();
- $versions = array_map('strval', array_keys($this->migrations));
- usort($versions, $this->sortMigrations(...));
- return $versions;
- }
- protected function sortMigrations(string $a, string $b): int {
- preg_match('/(\d+)Date(\d+)/', basename($a), $matchA);
- preg_match('/(\d+)Date(\d+)/', basename($b), $matchB);
- if (!empty($matchA) && !empty($matchB)) {
- $versionA = (int)$matchA[1];
- $versionB = (int)$matchB[1];
- if ($versionA !== $versionB) {
- return ($versionA < $versionB) ? -1 : 1;
- }
- return ($matchA[2] < $matchB[2]) ? -1 : 1;
- }
- return (basename($a) < basename($b)) ? -1 : 1;
- }
-
- protected function findMigrations(): array {
- $directory = realpath($this->migrationsPath);
- if ($directory === false || !file_exists($directory) || !is_dir($directory)) {
- return [];
- }
- $iterator = new \RegexIterator(
- new \RecursiveIteratorIterator(
- new \RecursiveDirectoryIterator($directory, \FilesystemIterator::SKIP_DOTS),
- \RecursiveIteratorIterator::LEAVES_ONLY
- ),
- '#^.+\\/Version[^\\/]{1,255}\\.php$#i',
- \RegexIterator::GET_MATCH);
- $files = array_keys(iterator_to_array($iterator));
- usort($files, $this->sortMigrations(...));
- $migrations = [];
- foreach ($files as $file) {
- $className = basename($file, '.php');
- $version = (string)substr($className, 7);
- if ($version === '0') {
- throw new \InvalidArgumentException(
- "Cannot load a migrations with the name '$version' because it is a reserved number"
- );
- }
- $migrations[$version] = sprintf('%s\\%s', $this->migrationsNamespace, $className);
- }
- return $migrations;
- }
-
- private function getMigrationsToExecute($to) {
- $knownMigrations = $this->getMigratedVersions();
- $availableMigrations = $this->getAvailableVersions();
- $toBeExecuted = [];
- foreach ($availableMigrations as $v) {
- if ($to !== 'latest' && $v > $to) {
- continue;
- }
- if ($this->shallBeExecuted($v, $knownMigrations)) {
- $toBeExecuted[] = $v;
- }
- }
- return $toBeExecuted;
- }
-
- private function shallBeExecuted($m, $knownMigrations) {
- if (in_array($m, $knownMigrations)) {
- return false;
- }
- return true;
- }
-
- private function markAsExecuted($version) {
- $this->connection->insertIfNotExist('*PREFIX*migrations', [
- 'app' => $this->appName,
- 'version' => $version
- ]);
- }
-
- public function getMigrationsTableName() {
- return $this->connection->getPrefix() . 'migrations';
- }
-
- public function getMigrationsNamespace() {
- return $this->migrationsNamespace;
- }
-
- public function getMigrationsDirectory() {
- return $this->migrationsPath;
- }
-
- public function getMigration(string $alias) {
- switch ($alias) {
- case 'current':
- return $this->getCurrentVersion();
- case 'next':
- return $this->getRelativeVersion($this->getCurrentVersion(), 1);
- case 'prev':
- return $this->getRelativeVersion($this->getCurrentVersion(), -1);
- case 'latest':
- $this->ensureMigrationsAreLoaded();
- $migrations = $this->getAvailableVersions();
- return @end($migrations);
- }
- return '0';
- }
- private function getRelativeVersion(string $version, int $delta): ?string {
- $this->ensureMigrationsAreLoaded();
- $versions = $this->getAvailableVersions();
- array_unshift($versions, '0');
-
- $offset = array_search($version, $versions, true);
- if ($offset === false || !isset($versions[$offset + $delta])) {
-
- return null;
- }
- return (string)$versions[$offset + $delta];
- }
- private function getCurrentVersion(): string {
- $m = $this->getMigratedVersions();
- if (count($m) === 0) {
- return '0';
- }
- $migrations = array_values($m);
- return @end($migrations);
- }
-
- private function getClass(string $version): string {
- $this->ensureMigrationsAreLoaded();
- if (isset($this->migrations[$version])) {
- return $this->migrations[$version];
- }
- throw new \InvalidArgumentException("Version $version is unknown.");
- }
-
- public function setOutput(IOutput $output): void {
- $this->output = $output;
- }
-
- public function migrate(string $to = 'latest', bool $schemaOnly = false): void {
- if ($schemaOnly) {
- $this->output->debug('Migrating schema only');
- $this->migrateSchemaOnly($to);
- return;
- }
-
- $toBeExecuted = $this->getMigrationsToExecute($to);
- foreach ($toBeExecuted as $version) {
- try {
- $this->executeStep($version, $schemaOnly);
- } catch (\Exception $e) {
-
-
- throw new \Exception('Database error when running migration ' . $version . ' for app ' . $this->getApp() . PHP_EOL . $e->getMessage(), 0, $e);
- }
- }
- }
-
- public function migrateSchemaOnly(string $to = 'latest'): void {
-
- $toBeExecuted = $this->getMigrationsToExecute($to);
- if (empty($toBeExecuted)) {
- return;
- }
- $toSchema = null;
- foreach ($toBeExecuted as $version) {
- $this->output->debug('- Reading ' . $version);
- $instance = $this->createInstance($version);
- $toSchema = $instance->changeSchema($this->output, function () use ($toSchema): ISchemaWrapper {
- return $toSchema ?: new SchemaWrapper($this->connection);
- }, ['tablePrefix' => $this->connection->getPrefix()]) ?: $toSchema;
- }
- if ($toSchema instanceof SchemaWrapper) {
- $this->output->debug('- Checking target database schema');
- $targetSchema = $toSchema->getWrappedSchema();
- $this->ensureUniqueNamesConstraints($targetSchema, true);
- if ($this->checkOracle) {
- $beforeSchema = $this->connection->createSchema();
- $this->ensureOracleConstraints($beforeSchema, $targetSchema, strlen($this->connection->getPrefix()));
- }
- $this->output->debug('- Migrate database schema');
- $this->connection->migrateToSchema($targetSchema);
- $toSchema->performDropTableCalls();
- }
- $this->output->debug('- Mark migrations as executed');
- foreach ($toBeExecuted as $version) {
- $this->markAsExecuted($version);
- }
- }
-
- public function describeMigrationStep($to = 'latest') {
- $toBeExecuted = $this->getMigrationsToExecute($to);
- $description = [];
- foreach ($toBeExecuted as $version) {
- $migration = $this->createInstance($version);
- if ($migration->name()) {
- $description[$migration->name()] = $migration->description();
- }
- }
- return $description;
- }
-
- public function createInstance($version) {
- $class = $this->getClass($version);
- try {
- $s = \OCP\Server::get($class);
- if (!$s instanceof IMigrationStep) {
- throw new \InvalidArgumentException('Not a valid migration');
- }
- } catch (QueryException $e) {
- if (class_exists($class)) {
- $s = new $class();
- } else {
- throw new \InvalidArgumentException("Migration step '$class' is unknown");
- }
- }
- return $s;
- }
-
- public function executeStep($version, $schemaOnly = false) {
- $instance = $this->createInstance($version);
- if (!$schemaOnly) {
- $instance->preSchemaChange($this->output, function (): ISchemaWrapper {
- return new SchemaWrapper($this->connection);
- }, ['tablePrefix' => $this->connection->getPrefix()]);
- }
- $toSchema = $instance->changeSchema($this->output, function (): ISchemaWrapper {
- return new SchemaWrapper($this->connection);
- }, ['tablePrefix' => $this->connection->getPrefix()]);
- if ($toSchema instanceof SchemaWrapper) {
- $targetSchema = $toSchema->getWrappedSchema();
- $this->ensureUniqueNamesConstraints($targetSchema, $schemaOnly);
- if ($this->checkOracle) {
- $sourceSchema = $this->connection->createSchema();
- $this->ensureOracleConstraints($sourceSchema, $targetSchema, strlen($this->connection->getPrefix()));
- }
- $this->connection->migrateToSchema($targetSchema);
- $toSchema->performDropTableCalls();
- }
- if (!$schemaOnly) {
- $instance->postSchemaChange($this->output, function (): ISchemaWrapper {
- return new SchemaWrapper($this->connection);
- }, ['tablePrefix' => $this->connection->getPrefix()]);
- }
- $this->markAsExecuted($version);
- }
-
- public function ensureOracleConstraints(Schema $sourceSchema, Schema $targetSchema, int $prefixLength) {
- $sequences = $targetSchema->getSequences();
- 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 (!$sourceTable instanceof Table || !$sourceTable->hasColumn($thing->getName())) {
- if (\strlen($thing->getName()) > 30) {
- throw new \InvalidArgumentException('Column name "' . $table->getName() . '"."' . $thing->getName() . '" is too long.');
- }
- if ($thing->getNotnull() && $thing->getDefault() === ''
- && $sourceTable instanceof Table && !$sourceTable->hasColumn($thing->getName())) {
- throw new \InvalidArgumentException('Column "' . $table->getName() . '"."' . $thing->getName() . '" is NotNull, but has empty string or null as default.');
- }
- if ($thing->getNotnull() && $thing->getType()->getName() === Types::BOOLEAN) {
- throw new \InvalidArgumentException('Column "' . $table->getName() . '"."' . $thing->getName() . '" is type Bool and also NotNull, so it can not store "false".');
- }
- $sourceColumn = null;
- } else {
- $sourceColumn = $sourceTable->getColumn($thing->getName());
- }
-
-
- if (($sourceColumn === null || $sourceColumn->getLength() !== $thing->getLength() || $sourceColumn->getType()->getName() !== Types::STRING)
- && $thing->getLength() > 4000 && $thing->getType()->getName() === Types::STRING) {
- throw new \InvalidArgumentException('Column "' . $table->getName() . '"."' . $thing->getName() . '" is type String, but exceeding the 4.000 length limit.');
- }
- }
- foreach ($table->getIndexes() as $thing) {
- if ((!$sourceTable instanceof Table || !$sourceTable->hasIndex($thing->getName())) && \strlen($thing->getName()) > 30) {
- throw new \InvalidArgumentException('Index name "' . $table->getName() . '"."' . $thing->getName() . '" is too long.');
- }
- }
- foreach ($table->getForeignKeys() as $thing) {
- if ((!$sourceTable instanceof Table || !$sourceTable->hasForeignKey($thing->getName())) && \strlen($thing->getName()) > 30) {
- throw new \InvalidArgumentException('Foreign key name "' . $table->getName() . '"."' . $thing->getName() . '" is too long.');
- }
- }
- $primaryKey = $table->getPrimaryKey();
- if ($primaryKey instanceof Index && (!$sourceTable instanceof Table || !$sourceTable->hasPrimaryKey())) {
- $indexName = strtolower($primaryKey->getName());
- $isUsingDefaultName = $indexName === 'primary';
- if ($this->connection->getDatabaseProvider() === IDBConnection::PLATFORM_POSTGRES) {
- $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;
- });
- }
- } elseif ($this->connection->getDatabaseProvider() === IDBConnection::PLATFORM_ORACLE) {
- $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()) - $prefixLength >= 23) {
- throw new \InvalidArgumentException('Primary index name on "' . $table->getName() . '" is too long.');
- }
- } elseif (!$primaryKey instanceof Index && !$sourceTable instanceof Table) {
-
- $logger = \OC::$server->get(LoggerInterface::class);
- $logger->error('Table "' . $table->getName() . '" has no primary key and therefor will not behave sane in clustered setups. This will throw an exception and not be installable in a future version of Nextcloud.');
-
- }
- }
- foreach ($sequences as $sequence) {
- if (!$sourceSchema->hasSequence($sequence->getName()) && \strlen($sequence->getName()) > 30) {
- throw new \InvalidArgumentException('Sequence name "' . $sequence->getName() . '" is too long.');
- }
- }
- }
-
- public function ensureUniqueNamesConstraints(Schema $targetSchema, bool $isInstalling): void {
- $constraintNames = [];
- $sequences = $targetSchema->getSequences();
- foreach ($targetSchema->getTables() as $table) {
- foreach ($table->getIndexes() as $thing) {
- $indexName = strtolower($thing->getName());
- if ($indexName === 'primary' || $thing->isPrimary()) {
- continue;
- }
- if (isset($constraintNames[$thing->getName()])) {
- if ($isInstalling) {
- throw new \InvalidArgumentException('Index name "' . $thing->getName() . '" for table "' . $table->getName() . '" collides with the constraint on table "' . $constraintNames[$thing->getName()] . '".');
- }
- $this->logErrorOrWarning('Index name "' . $thing->getName() . '" for table "' . $table->getName() . '" collides with the constraint on table "' . $constraintNames[$thing->getName()] . '".');
- }
- $constraintNames[$thing->getName()] = $table->getName();
- }
- foreach ($table->getForeignKeys() as $thing) {
- if (isset($constraintNames[$thing->getName()])) {
- if ($isInstalling) {
- throw new \InvalidArgumentException('Foreign key name "' . $thing->getName() . '" for table "' . $table->getName() . '" collides with the constraint on table "' . $constraintNames[$thing->getName()] . '".');
- }
- $this->logErrorOrWarning('Foreign key name "' . $thing->getName() . '" for table "' . $table->getName() . '" collides with the constraint on table "' . $constraintNames[$thing->getName()] . '".');
- }
- $constraintNames[$thing->getName()] = $table->getName();
- }
- $primaryKey = $table->getPrimaryKey();
- if ($primaryKey instanceof Index) {
- $indexName = strtolower($primaryKey->getName());
- if ($indexName === 'primary') {
- continue;
- }
- if (isset($constraintNames[$indexName])) {
- if ($isInstalling) {
- throw new \InvalidArgumentException('Primary index name "' . $indexName . '" for table "' . $table->getName() . '" collides with the constraint on table "' . $constraintNames[$thing->getName()] . '".');
- }
- $this->logErrorOrWarning('Primary index name "' . $indexName . '" for table "' . $table->getName() . '" collides with the constraint on table "' . $constraintNames[$thing->getName()] . '".');
- }
- $constraintNames[$indexName] = $table->getName();
- }
- }
- foreach ($sequences as $sequence) {
- if (isset($constraintNames[$sequence->getName()])) {
- if ($isInstalling) {
- throw new \InvalidArgumentException('Sequence name "' . $sequence->getName() . '" for table "' . $table->getName() . '" collides with the constraint on table "' . $constraintNames[$thing->getName()] . '".');
- }
- $this->logErrorOrWarning('Sequence name "' . $sequence->getName() . '" for table "' . $table->getName() . '" collides with the constraint on table "' . $constraintNames[$thing->getName()] . '".');
- }
- $constraintNames[$sequence->getName()] = 'sequence';
- }
- }
- protected function logErrorOrWarning(string $log): void {
- if ($this->output instanceof SimpleOutput) {
- $this->output->warning($log);
- } else {
- $this->logger->error($log);
- }
- }
- private function ensureMigrationsAreLoaded() {
- if (empty($this->migrations)) {
- $this->migrations = $this->findMigrations();
- }
- }
- }
|