@@ -47,583 +47,583 @@  | 
                                                    ||
| 47 | 47 | |
| 48 | 48 |  class MigrationService { | 
                                                        
| 49 | 49 | |
| 50 | - /** @var boolean */  | 
                                                        |
| 51 | - private $migrationTableCreated;  | 
                                                        |
| 52 | - /** @var array */  | 
                                                        |
| 53 | - private $migrations;  | 
                                                        |
| 54 | - /** @var IOutput */  | 
                                                        |
| 55 | - private $output;  | 
                                                        |
| 56 | - /** @var Connection */  | 
                                                        |
| 57 | - private $connection;  | 
                                                        |
| 58 | - /** @var string */  | 
                                                        |
| 59 | - private $appName;  | 
                                                        |
| 60 | - /** @var bool */  | 
                                                        |
| 61 | - private $checkOracle;  | 
                                                        |
| 62 | -  | 
                                                        |
| 63 | - /**  | 
                                                        |
| 64 | - * MigrationService constructor.  | 
                                                        |
| 65 | - *  | 
                                                        |
| 66 | - * @param $appName  | 
                                                        |
| 67 | - * @param IDBConnection $connection  | 
                                                        |
| 68 | - * @param AppLocator $appLocator  | 
                                                        |
| 69 | - * @param IOutput|null $output  | 
                                                        |
| 70 | - * @throws \Exception  | 
                                                        |
| 71 | - */  | 
                                                        |
| 72 | -	public function __construct($appName, IDBConnection $connection, IOutput $output = null, AppLocator $appLocator = null) { | 
                                                        |
| 73 | - $this->appName = $appName;  | 
                                                        |
| 74 | - $this->connection = $connection;  | 
                                                        |
| 75 | - $this->output = $output;  | 
                                                        |
| 76 | -		if (null === $this->output) { | 
                                                        |
| 77 | - $this->output = new SimpleOutput(\OC::$server->getLogger(), $appName);  | 
                                                        |
| 78 | - }  | 
                                                        |
| 79 | -  | 
                                                        |
| 80 | -		if ($appName === 'core') { | 
                                                        |
| 81 | - $this->migrationsPath = \OC::$SERVERROOT . '/core/Migrations';  | 
                                                        |
| 82 | - $this->migrationsNamespace = 'OC\\Core\\Migrations';  | 
                                                        |
| 83 | - $this->checkOracle = true;  | 
                                                        |
| 84 | -		} else { | 
                                                        |
| 85 | -			if (null === $appLocator) { | 
                                                        |
| 86 | - $appLocator = new AppLocator();  | 
                                                        |
| 87 | - }  | 
                                                        |
| 88 | - $appPath = $appLocator->getAppPath($appName);  | 
                                                        |
| 89 | - $namespace = App::buildAppNamespace($appName);  | 
                                                        |
| 90 | - $this->migrationsPath = "$appPath/lib/Migration";  | 
                                                        |
| 91 | - $this->migrationsNamespace = $namespace . '\\Migration';  | 
                                                        |
| 92 | -  | 
                                                        |
| 93 | - $infoParser = new InfoParser();  | 
                                                        |
| 94 | - $info = $infoParser->parse($appPath . '/appinfo/info.xml');  | 
                                                        |
| 95 | -			if (!isset($info['dependencies']['database'])) { | 
                                                        |
| 96 | - $this->checkOracle = true;  | 
                                                        |
| 97 | -			} else { | 
                                                        |
| 98 | - $this->checkOracle = false;  | 
                                                        |
| 99 | -				foreach ($info['dependencies']['database'] as $database) { | 
                                                        |
| 100 | -					if (\is_string($database) && $database === 'oci') { | 
                                                        |
| 101 | - $this->checkOracle = true;  | 
                                                        |
| 102 | -					} elseif (\is_array($database) && isset($database['@value']) && $database['@value'] === 'oci') { | 
                                                        |
| 103 | - $this->checkOracle = true;  | 
                                                        |
| 104 | - }  | 
                                                        |
| 105 | - }  | 
                                                        |
| 106 | - }  | 
                                                        |
| 107 | - }  | 
                                                        |
| 108 | - }  | 
                                                        |
| 109 | -  | 
                                                        |
| 110 | - /**  | 
                                                        |
| 111 | - * Returns the name of the app for which this migration is executed  | 
                                                        |
| 112 | - *  | 
                                                        |
| 113 | - * @return string  | 
                                                        |
| 114 | - */  | 
                                                        |
| 115 | -	public function getApp() { | 
                                                        |
| 116 | - return $this->appName;  | 
                                                        |
| 117 | - }  | 
                                                        |
| 118 | -  | 
                                                        |
| 119 | - /**  | 
                                                        |
| 120 | - * @return bool  | 
                                                        |
| 121 | - * @codeCoverageIgnore - this will implicitly tested on installation  | 
                                                        |
| 122 | - */  | 
                                                        |
| 123 | -	private function createMigrationTable() { | 
                                                        |
| 124 | -		if ($this->migrationTableCreated) { | 
                                                        |
| 125 | - return false;  | 
                                                        |
| 126 | - }  | 
                                                        |
| 127 | -  | 
                                                        |
| 128 | -		if ($this->connection->tableExists('migrations')) { | 
                                                        |
| 129 | - $this->migrationTableCreated = true;  | 
                                                        |
| 130 | - return false;  | 
                                                        |
| 131 | - }  | 
                                                        |
| 132 | -  | 
                                                        |
| 133 | - $schema = new SchemaWrapper($this->connection);  | 
                                                        |
| 134 | -  | 
                                                        |
| 135 | - /**  | 
                                                        |
| 136 | - * We drop the table when it has different columns or the definition does not  | 
                                                        |
| 137 | - * match. E.g. ownCloud uses a length of 177 for app and 14 for version.  | 
                                                        |
| 138 | - */  | 
                                                        |
| 139 | -		try { | 
                                                        |
| 140 | -			$table = $schema->getTable('migrations'); | 
                                                        |
| 141 | - $columns = $table->getColumns();  | 
                                                        |
| 142 | -  | 
                                                        |
| 143 | -			if (count($columns) === 2) { | 
                                                        |
| 144 | -				try { | 
                                                        |
| 145 | -					$column = $table->getColumn('app'); | 
                                                        |
| 146 | - $schemaMismatch = $column->getLength() !== 255;  | 
                                                        |
| 147 | -  | 
                                                        |
| 148 | -					if (!$schemaMismatch) { | 
                                                        |
| 149 | -						$column = $table->getColumn('version'); | 
                                                        |
| 150 | - $schemaMismatch = $column->getLength() !== 255;  | 
                                                        |
| 151 | - }  | 
                                                        |
| 152 | -				} catch (SchemaException $e) { | 
                                                        |
| 153 | - // One of the columns is missing  | 
                                                        |
| 154 | - $schemaMismatch = true;  | 
                                                        |
| 155 | - }  | 
                                                        |
| 156 | -  | 
                                                        |
| 157 | -				if (!$schemaMismatch) { | 
                                                        |
| 158 | - // Table exists and schema matches: return back!  | 
                                                        |
| 159 | - $this->migrationTableCreated = true;  | 
                                                        |
| 160 | - return false;  | 
                                                        |
| 161 | - }  | 
                                                        |
| 162 | - }  | 
                                                        |
| 163 | -  | 
                                                        |
| 164 | - // Drop the table, when it didn't match our expectations.  | 
                                                        |
| 165 | -			$this->connection->dropTable('migrations'); | 
                                                        |
| 166 | -  | 
                                                        |
| 167 | - // Recreate the schema after the table was dropped.  | 
                                                        |
| 168 | - $schema = new SchemaWrapper($this->connection);  | 
                                                        |
| 169 | -		} catch (SchemaException $e) { | 
                                                        |
| 170 | - // Table not found, no need to panic, we will create it.  | 
                                                        |
| 171 | - }  | 
                                                        |
| 172 | -  | 
                                                        |
| 173 | -		$table = $schema->createTable('migrations'); | 
                                                        |
| 174 | -		$table->addColumn('app', Types::STRING, ['length' => 255]); | 
                                                        |
| 175 | -		$table->addColumn('version', Types::STRING, ['length' => 255]); | 
                                                        |
| 176 | - $table->setPrimaryKey(['app', 'version']);  | 
                                                        |
| 177 | -  | 
                                                        |
| 178 | - $this->connection->migrateToSchema($schema->getWrappedSchema());  | 
                                                        |
| 179 | -  | 
                                                        |
| 180 | - $this->migrationTableCreated = true;  | 
                                                        |
| 181 | -  | 
                                                        |
| 182 | - return true;  | 
                                                        |
| 183 | - }  | 
                                                        |
| 184 | -  | 
                                                        |
| 185 | - /**  | 
                                                        |
| 186 | - * Returns all versions which have already been applied  | 
                                                        |
| 187 | - *  | 
                                                        |
| 188 | - * @return string[]  | 
                                                        |
| 189 | - * @codeCoverageIgnore - no need to test this  | 
                                                        |
| 190 | - */  | 
                                                        |
| 191 | -	public function getMigratedVersions() { | 
                                                        |
| 192 | - $this->createMigrationTable();  | 
                                                        |
| 193 | - $qb = $this->connection->getQueryBuilder();  | 
                                                        |
| 194 | -  | 
                                                        |
| 195 | -		$qb->select('version') | 
                                                        |
| 196 | -			->from('migrations') | 
                                                        |
| 197 | -			->where($qb->expr()->eq('app', $qb->createNamedParameter($this->getApp()))) | 
                                                        |
| 198 | -			->orderBy('version'); | 
                                                        |
| 199 | -  | 
                                                        |
| 200 | - $result = $qb->execute();  | 
                                                        |
| 201 | - $rows = $result->fetchAll(\PDO::FETCH_COLUMN);  | 
                                                        |
| 202 | - $result->closeCursor();  | 
                                                        |
| 203 | -  | 
                                                        |
| 204 | - return $rows;  | 
                                                        |
| 205 | - }  | 
                                                        |
| 206 | -  | 
                                                        |
| 207 | - /**  | 
                                                        |
| 208 | - * Returns all versions which are available in the migration folder  | 
                                                        |
| 209 | - *  | 
                                                        |
| 210 | - * @return array  | 
                                                        |
| 211 | - */  | 
                                                        |
| 212 | -	public function getAvailableVersions() { | 
                                                        |
| 213 | - $this->ensureMigrationsAreLoaded();  | 
                                                        |
| 214 | -		return array_map('strval', array_keys($this->migrations)); | 
                                                        |
| 215 | - }  | 
                                                        |
| 216 | -  | 
                                                        |
| 217 | -	protected function findMigrations() { | 
                                                        |
| 218 | - $directory = realpath($this->migrationsPath);  | 
                                                        |
| 219 | -		if ($directory === false || !file_exists($directory) || !is_dir($directory)) { | 
                                                        |
| 220 | - return [];  | 
                                                        |
| 221 | - }  | 
                                                        |
| 222 | -  | 
                                                        |
| 223 | - $iterator = new \RegexIterator(  | 
                                                        |
| 224 | - new \RecursiveIteratorIterator(  | 
                                                        |
| 225 | - new \RecursiveDirectoryIterator($directory, \FilesystemIterator::SKIP_DOTS),  | 
                                                        |
| 226 | - \RecursiveIteratorIterator::LEAVES_ONLY  | 
                                                        |
| 227 | - ),  | 
                                                        |
| 228 | -			'#^.+\\/Version[^\\/]{1,255}\\.php$#i', | 
                                                        |
| 229 | - \RegexIterator::GET_MATCH);  | 
                                                        |
| 230 | -  | 
                                                        |
| 231 | - $files = array_keys(iterator_to_array($iterator));  | 
                                                        |
| 232 | -		uasort($files, function ($a, $b) { | 
                                                        |
| 233 | -			preg_match('/^Version(\d+)Date(\d+)\\.php$/', basename($a), $matchA); | 
                                                        |
| 234 | -			preg_match('/^Version(\d+)Date(\d+)\\.php$/', basename($b), $matchB); | 
                                                        |
| 235 | -			if (!empty($matchA) && !empty($matchB)) { | 
                                                        |
| 236 | -				if ($matchA[1] !== $matchB[1]) { | 
                                                        |
| 237 | - return ($matchA[1] < $matchB[1]) ? -1 : 1;  | 
                                                        |
| 238 | - }  | 
                                                        |
| 239 | - return ($matchA[2] < $matchB[2]) ? -1 : 1;  | 
                                                        |
| 240 | - }  | 
                                                        |
| 241 | - return (basename($a) < basename($b)) ? -1 : 1;  | 
                                                        |
| 242 | - });  | 
                                                        |
| 243 | -  | 
                                                        |
| 244 | - $migrations = [];  | 
                                                        |
| 245 | -  | 
                                                        |
| 246 | -		foreach ($files as $file) { | 
                                                        |
| 247 | - $className = basename($file, '.php');  | 
                                                        |
| 248 | - $version = (string) substr($className, 7);  | 
                                                        |
| 249 | -			if ($version === '0') { | 
                                                        |
| 250 | - throw new \InvalidArgumentException(  | 
                                                        |
| 251 | - "Cannot load a migrations with the name '$version' because it is a reserved number"  | 
                                                        |
| 252 | - );  | 
                                                        |
| 253 | - }  | 
                                                        |
| 254 | -			$migrations[$version] = sprintf('%s\\%s', $this->migrationsNamespace, $className); | 
                                                        |
| 255 | - }  | 
                                                        |
| 256 | -  | 
                                                        |
| 257 | - return $migrations;  | 
                                                        |
| 258 | - }  | 
                                                        |
| 259 | -  | 
                                                        |
| 260 | - /**  | 
                                                        |
| 261 | - * @param string $to  | 
                                                        |
| 262 | - * @return string[]  | 
                                                        |
| 263 | - */  | 
                                                        |
| 264 | -	private function getMigrationsToExecute($to) { | 
                                                        |
| 265 | - $knownMigrations = $this->getMigratedVersions();  | 
                                                        |
| 266 | - $availableMigrations = $this->getAvailableVersions();  | 
                                                        |
| 267 | -  | 
                                                        |
| 268 | - $toBeExecuted = [];  | 
                                                        |
| 269 | -		foreach ($availableMigrations as $v) { | 
                                                        |
| 270 | -			if ($to !== 'latest' && $v > $to) { | 
                                                        |
| 271 | - continue;  | 
                                                        |
| 272 | - }  | 
                                                        |
| 273 | -			if ($this->shallBeExecuted($v, $knownMigrations)) { | 
                                                        |
| 274 | - $toBeExecuted[] = $v;  | 
                                                        |
| 275 | - }  | 
                                                        |
| 276 | - }  | 
                                                        |
| 277 | -  | 
                                                        |
| 278 | - return $toBeExecuted;  | 
                                                        |
| 279 | - }  | 
                                                        |
| 280 | -  | 
                                                        |
| 281 | - /**  | 
                                                        |
| 282 | - * @param string $m  | 
                                                        |
| 283 | - * @param string[] $knownMigrations  | 
                                                        |
| 284 | - * @return bool  | 
                                                        |
| 285 | - */  | 
                                                        |
| 286 | -	private function shallBeExecuted($m, $knownMigrations) { | 
                                                        |
| 287 | -		if (in_array($m, $knownMigrations)) { | 
                                                        |
| 288 | - return false;  | 
                                                        |
| 289 | - }  | 
                                                        |
| 290 | -  | 
                                                        |
| 291 | - return true;  | 
                                                        |
| 292 | - }  | 
                                                        |
| 293 | -  | 
                                                        |
| 294 | - /**  | 
                                                        |
| 295 | - * @param string $version  | 
                                                        |
| 296 | - */  | 
                                                        |
| 297 | -	private function markAsExecuted($version) { | 
                                                        |
| 298 | -		$this->connection->insertIfNotExist('*PREFIX*migrations', [ | 
                                                        |
| 299 | - 'app' => $this->appName,  | 
                                                        |
| 300 | - 'version' => $version  | 
                                                        |
| 301 | - ]);  | 
                                                        |
| 302 | - }  | 
                                                        |
| 303 | -  | 
                                                        |
| 304 | - /**  | 
                                                        |
| 305 | - * Returns the name of the table which holds the already applied versions  | 
                                                        |
| 306 | - *  | 
                                                        |
| 307 | - * @return string  | 
                                                        |
| 308 | - */  | 
                                                        |
| 309 | -	public function getMigrationsTableName() { | 
                                                        |
| 310 | - return $this->connection->getPrefix() . 'migrations';  | 
                                                        |
| 311 | - }  | 
                                                        |
| 312 | -  | 
                                                        |
| 313 | - /**  | 
                                                        |
| 314 | - * Returns the namespace of the version classes  | 
                                                        |
| 315 | - *  | 
                                                        |
| 316 | - * @return string  | 
                                                        |
| 317 | - */  | 
                                                        |
| 318 | -	public function getMigrationsNamespace() { | 
                                                        |
| 319 | - return $this->migrationsNamespace;  | 
                                                        |
| 320 | - }  | 
                                                        |
| 321 | -  | 
                                                        |
| 322 | - /**  | 
                                                        |
| 323 | - * Returns the directory which holds the versions  | 
                                                        |
| 324 | - *  | 
                                                        |
| 325 | - * @return string  | 
                                                        |
| 326 | - */  | 
                                                        |
| 327 | -	public function getMigrationsDirectory() { | 
                                                        |
| 328 | - return $this->migrationsPath;  | 
                                                        |
| 329 | - }  | 
                                                        |
| 330 | -  | 
                                                        |
| 331 | - /**  | 
                                                        |
| 332 | - * Return the explicit version for the aliases; current, next, prev, latest  | 
                                                        |
| 333 | - *  | 
                                                        |
| 334 | - * @param string $alias  | 
                                                        |
| 335 | - * @return mixed|null|string  | 
                                                        |
| 336 | - */  | 
                                                        |
| 337 | -	public function getMigration($alias) { | 
                                                        |
| 338 | -		switch ($alias) { | 
                                                        |
| 339 | - case 'current':  | 
                                                        |
| 340 | - return $this->getCurrentVersion();  | 
                                                        |
| 341 | - case 'next':  | 
                                                        |
| 342 | - return $this->getRelativeVersion($this->getCurrentVersion(), 1);  | 
                                                        |
| 343 | - case 'prev':  | 
                                                        |
| 344 | - return $this->getRelativeVersion($this->getCurrentVersion(), -1);  | 
                                                        |
| 345 | - case 'latest':  | 
                                                        |
| 346 | - $this->ensureMigrationsAreLoaded();  | 
                                                        |
| 347 | -  | 
                                                        |
| 348 | - $migrations = $this->getAvailableVersions();  | 
                                                        |
| 349 | - return @end($migrations);  | 
                                                        |
| 350 | - }  | 
                                                        |
| 351 | - return '0';  | 
                                                        |
| 352 | - }  | 
                                                        |
| 353 | -  | 
                                                        |
| 354 | - /**  | 
                                                        |
| 355 | - * @param string $version  | 
                                                        |
| 356 | - * @param int $delta  | 
                                                        |
| 357 | - * @return null|string  | 
                                                        |
| 358 | - */  | 
                                                        |
| 359 | -	private function getRelativeVersion($version, $delta) { | 
                                                        |
| 360 | - $this->ensureMigrationsAreLoaded();  | 
                                                        |
| 361 | -  | 
                                                        |
| 362 | - $versions = $this->getAvailableVersions();  | 
                                                        |
| 363 | - array_unshift($versions, 0);  | 
                                                        |
| 364 | - $offset = array_search($version, $versions, true);  | 
                                                        |
| 365 | -		if ($offset === false || !isset($versions[$offset + $delta])) { | 
                                                        |
| 366 | - // Unknown version or delta out of bounds.  | 
                                                        |
| 367 | - return null;  | 
                                                        |
| 368 | - }  | 
                                                        |
| 369 | -  | 
                                                        |
| 370 | - return (string) $versions[$offset + $delta];  | 
                                                        |
| 371 | - }  | 
                                                        |
| 372 | -  | 
                                                        |
| 373 | - /**  | 
                                                        |
| 374 | - * @return string  | 
                                                        |
| 375 | - */  | 
                                                        |
| 376 | -	private function getCurrentVersion() { | 
                                                        |
| 377 | - $m = $this->getMigratedVersions();  | 
                                                        |
| 378 | -		if (count($m) === 0) { | 
                                                        |
| 379 | - return '0';  | 
                                                        |
| 380 | - }  | 
                                                        |
| 381 | - $migrations = array_values($m);  | 
                                                        |
| 382 | - return @end($migrations);  | 
                                                        |
| 383 | - }  | 
                                                        |
| 384 | -  | 
                                                        |
| 385 | - /**  | 
                                                        |
| 386 | - * @param string $version  | 
                                                        |
| 387 | - * @return string  | 
                                                        |
| 388 | - * @throws \InvalidArgumentException  | 
                                                        |
| 389 | - */  | 
                                                        |
| 390 | -	private function getClass($version) { | 
                                                        |
| 391 | - $this->ensureMigrationsAreLoaded();  | 
                                                        |
| 392 | -  | 
                                                        |
| 393 | -		if (isset($this->migrations[$version])) { | 
                                                        |
| 394 | - return $this->migrations[$version];  | 
                                                        |
| 395 | - }  | 
                                                        |
| 396 | -  | 
                                                        |
| 397 | -		throw new \InvalidArgumentException("Version $version is unknown."); | 
                                                        |
| 398 | - }  | 
                                                        |
| 399 | -  | 
                                                        |
| 400 | - /**  | 
                                                        |
| 401 | - * Allows to set an IOutput implementation which is used for logging progress and messages  | 
                                                        |
| 402 | - *  | 
                                                        |
| 403 | - * @param IOutput $output  | 
                                                        |
| 404 | - */  | 
                                                        |
| 405 | -	public function setOutput(IOutput $output) { | 
                                                        |
| 406 | - $this->output = $output;  | 
                                                        |
| 407 | - }  | 
                                                        |
| 408 | -  | 
                                                        |
| 409 | - /**  | 
                                                        |
| 410 | - * Applies all not yet applied versions up to $to  | 
                                                        |
| 411 | - *  | 
                                                        |
| 412 | - * @param string $to  | 
                                                        |
| 413 | - * @param bool $schemaOnly  | 
                                                        |
| 414 | - * @throws \InvalidArgumentException  | 
                                                        |
| 415 | - */  | 
                                                        |
| 416 | -	public function migrate($to = 'latest', $schemaOnly = false) { | 
                                                        |
| 417 | -		if ($schemaOnly) { | 
                                                        |
| 418 | - $this->migrateSchemaOnly($to);  | 
                                                        |
| 419 | - return;  | 
                                                        |
| 420 | - }  | 
                                                        |
| 421 | -  | 
                                                        |
| 422 | - // read known migrations  | 
                                                        |
| 423 | - $toBeExecuted = $this->getMigrationsToExecute($to);  | 
                                                        |
| 424 | -		foreach ($toBeExecuted as $version) { | 
                                                        |
| 425 | -			try { | 
                                                        |
| 426 | - $this->executeStep($version, $schemaOnly);  | 
                                                        |
| 427 | -			} catch (DriverException $e) { | 
                                                        |
| 428 | - // The exception itself does not contain the name of the migration,  | 
                                                        |
| 429 | - // so we wrap it here, to make debugging easier.  | 
                                                        |
| 430 | -				throw new \Exception('Database error when running migration ' . $to . ' for app ' . $this->getApp(), 0, $e); | 
                                                        |
| 431 | - }  | 
                                                        |
| 432 | - }  | 
                                                        |
| 433 | - }  | 
                                                        |
| 434 | -  | 
                                                        |
| 435 | - /**  | 
                                                        |
| 436 | - * Applies all not yet applied versions up to $to  | 
                                                        |
| 437 | - *  | 
                                                        |
| 438 | - * @param string $to  | 
                                                        |
| 439 | - * @throws \InvalidArgumentException  | 
                                                        |
| 440 | - */  | 
                                                        |
| 441 | -	public function migrateSchemaOnly($to = 'latest') { | 
                                                        |
| 442 | - // read known migrations  | 
                                                        |
| 443 | - $toBeExecuted = $this->getMigrationsToExecute($to);  | 
                                                        |
| 444 | -  | 
                                                        |
| 445 | -		if (empty($toBeExecuted)) { | 
                                                        |
| 446 | - return;  | 
                                                        |
| 447 | - }  | 
                                                        |
| 448 | -  | 
                                                        |
| 449 | - $toSchema = null;  | 
                                                        |
| 450 | -		foreach ($toBeExecuted as $version) { | 
                                                        |
| 451 | - $instance = $this->createInstance($version);  | 
                                                        |
| 452 | -  | 
                                                        |
| 453 | -			$toSchema = $instance->changeSchema($this->output, function () use ($toSchema) { | 
                                                        |
| 454 | - return $toSchema ?: new SchemaWrapper($this->connection);  | 
                                                        |
| 455 | - }, ['tablePrefix' => $this->connection->getPrefix()]) ?: $toSchema;  | 
                                                        |
| 456 | -  | 
                                                        |
| 457 | - $this->markAsExecuted($version);  | 
                                                        |
| 458 | - }  | 
                                                        |
| 459 | -  | 
                                                        |
| 460 | -		if ($toSchema instanceof SchemaWrapper) { | 
                                                        |
| 461 | - $targetSchema = $toSchema->getWrappedSchema();  | 
                                                        |
| 462 | -			if ($this->checkOracle) { | 
                                                        |
| 463 | - $beforeSchema = $this->connection->createSchema();  | 
                                                        |
| 464 | - $this->ensureOracleIdentifierLengthLimit($beforeSchema, $targetSchema, strlen($this->connection->getPrefix()));  | 
                                                        |
| 465 | - }  | 
                                                        |
| 466 | - $this->connection->migrateToSchema($targetSchema);  | 
                                                        |
| 467 | - $toSchema->performDropTableCalls();  | 
                                                        |
| 468 | - }  | 
                                                        |
| 469 | - }  | 
                                                        |
| 470 | -  | 
                                                        |
| 471 | - /**  | 
                                                        |
| 472 | - * Get the human readable descriptions for the migration steps to run  | 
                                                        |
| 473 | - *  | 
                                                        |
| 474 | - * @param string $to  | 
                                                        |
| 475 | - * @return string[] [$name => $description]  | 
                                                        |
| 476 | - */  | 
                                                        |
| 477 | -	public function describeMigrationStep($to = 'latest') { | 
                                                        |
| 478 | - $toBeExecuted = $this->getMigrationsToExecute($to);  | 
                                                        |
| 479 | - $description = [];  | 
                                                        |
| 480 | -		foreach ($toBeExecuted as $version) { | 
                                                        |
| 481 | - $migration = $this->createInstance($version);  | 
                                                        |
| 482 | -			if ($migration->name()) { | 
                                                        |
| 483 | - $description[$migration->name()] = $migration->description();  | 
                                                        |
| 484 | - }  | 
                                                        |
| 485 | - }  | 
                                                        |
| 486 | - return $description;  | 
                                                        |
| 487 | - }  | 
                                                        |
| 488 | -  | 
                                                        |
| 489 | - /**  | 
                                                        |
| 490 | - * @param string $version  | 
                                                        |
| 491 | - * @return IMigrationStep  | 
                                                        |
| 492 | - * @throws \InvalidArgumentException  | 
                                                        |
| 493 | - */  | 
                                                        |
| 494 | -	protected function createInstance($version) { | 
                                                        |
| 495 | - $class = $this->getClass($version);  | 
                                                        |
| 496 | -		try { | 
                                                        |
| 497 | - $s = \OC::$server->query($class);  | 
                                                        |
| 498 | -  | 
                                                        |
| 499 | -			if (!$s instanceof IMigrationStep) { | 
                                                        |
| 500 | -				throw new \InvalidArgumentException('Not a valid migration'); | 
                                                        |
| 501 | - }  | 
                                                        |
| 502 | -		} catch (QueryException $e) { | 
                                                        |
| 503 | -			if (class_exists($class)) { | 
                                                        |
| 504 | - $s = new $class();  | 
                                                        |
| 505 | -			} else { | 
                                                        |
| 506 | -				throw new \InvalidArgumentException("Migration step '$class' is unknown"); | 
                                                        |
| 507 | - }  | 
                                                        |
| 508 | - }  | 
                                                        |
| 509 | -  | 
                                                        |
| 510 | - return $s;  | 
                                                        |
| 511 | - }  | 
                                                        |
| 512 | -  | 
                                                        |
| 513 | - /**  | 
                                                        |
| 514 | - * Executes one explicit version  | 
                                                        |
| 515 | - *  | 
                                                        |
| 516 | - * @param string $version  | 
                                                        |
| 517 | - * @param bool $schemaOnly  | 
                                                        |
| 518 | - * @throws \InvalidArgumentException  | 
                                                        |
| 519 | - */  | 
                                                        |
| 520 | -	public function executeStep($version, $schemaOnly = false) { | 
                                                        |
| 521 | - $instance = $this->createInstance($version);  | 
                                                        |
| 522 | -  | 
                                                        |
| 523 | -		if (!$schemaOnly) { | 
                                                        |
| 524 | -			$instance->preSchemaChange($this->output, function () { | 
                                                        |
| 525 | - return new SchemaWrapper($this->connection);  | 
                                                        |
| 526 | - }, ['tablePrefix' => $this->connection->getPrefix()]);  | 
                                                        |
| 527 | - }  | 
                                                        |
| 528 | -  | 
                                                        |
| 529 | -		$toSchema = $instance->changeSchema($this->output, function () { | 
                                                        |
| 530 | - return new SchemaWrapper($this->connection);  | 
                                                        |
| 531 | - }, ['tablePrefix' => $this->connection->getPrefix()]);  | 
                                                        |
| 532 | -  | 
                                                        |
| 533 | -		if ($toSchema instanceof SchemaWrapper) { | 
                                                        |
| 534 | - $targetSchema = $toSchema->getWrappedSchema();  | 
                                                        |
| 535 | -			if ($this->checkOracle) { | 
                                                        |
| 536 | - $sourceSchema = $this->connection->createSchema();  | 
                                                        |
| 537 | - $this->ensureOracleIdentifierLengthLimit($sourceSchema, $targetSchema, strlen($this->connection->getPrefix()));  | 
                                                        |
| 538 | - }  | 
                                                        |
| 539 | - $this->connection->migrateToSchema($targetSchema);  | 
                                                        |
| 540 | - $toSchema->performDropTableCalls();  | 
                                                        |
| 541 | - }  | 
                                                        |
| 542 | -  | 
                                                        |
| 543 | -		if (!$schemaOnly) { | 
                                                        |
| 544 | -			$instance->postSchemaChange($this->output, function () { | 
                                                        |
| 545 | - return new SchemaWrapper($this->connection);  | 
                                                        |
| 546 | - }, ['tablePrefix' => $this->connection->getPrefix()]);  | 
                                                        |
| 547 | - }  | 
                                                        |
| 548 | -  | 
                                                        |
| 549 | - $this->markAsExecuted($version);  | 
                                                        |
| 550 | - }  | 
                                                        |
| 551 | -  | 
                                                        |
| 552 | -	public function ensureOracleIdentifierLengthLimit(Schema $sourceSchema, Schema $targetSchema, int $prefixLength) { | 
                                                        |
| 553 | - $sequences = $targetSchema->getSequences();  | 
                                                        |
| 554 | -  | 
                                                        |
| 555 | -		foreach ($targetSchema->getTables() as $table) { | 
                                                        |
| 556 | -			try { | 
                                                        |
| 557 | - $sourceTable = $sourceSchema->getTable($table->getName());  | 
                                                        |
| 558 | -			} catch (SchemaException $e) { | 
                                                        |
| 559 | -				if (\strlen($table->getName()) - $prefixLength > 27) { | 
                                                        |
| 560 | -					throw new \InvalidArgumentException('Table name "'  . $table->getName() . '" is too long.'); | 
                                                        |
| 561 | - }  | 
                                                        |
| 562 | - $sourceTable = null;  | 
                                                        |
| 563 | - }  | 
                                                        |
| 564 | -  | 
                                                        |
| 565 | -			foreach ($table->getColumns() as $thing) { | 
                                                        |
| 566 | -				if ((!$sourceTable instanceof Table || !$sourceTable->hasColumn($thing->getName())) && \strlen($thing->getName()) > 30) { | 
                                                        |
| 567 | -					throw new \InvalidArgumentException('Column name "'  . $table->getName() . '"."' . $thing->getName() . '" is too long.'); | 
                                                        |
| 568 | - }  | 
                                                        |
| 569 | -  | 
                                                        |
| 570 | - if ($thing->getNotnull() && $thing->getDefault() === ''  | 
                                                        |
| 571 | -					&& $sourceTable instanceof Table && !$sourceTable->hasColumn($thing->getName())) { | 
                                                        |
| 572 | -					throw new \InvalidArgumentException('Column name "'  . $table->getName() . '"."' . $thing->getName() . '" is NotNull, but has empty string or null as default.'); | 
                                                        |
| 573 | - }  | 
                                                        |
| 574 | - }  | 
                                                        |
| 575 | -  | 
                                                        |
| 576 | -			foreach ($table->getIndexes() as $thing) { | 
                                                        |
| 577 | -				if ((!$sourceTable instanceof Table || !$sourceTable->hasIndex($thing->getName())) && \strlen($thing->getName()) > 30) { | 
                                                        |
| 578 | -					throw new \InvalidArgumentException('Index name "'  . $table->getName() . '"."' . $thing->getName() . '" is too long.'); | 
                                                        |
| 579 | - }  | 
                                                        |
| 580 | - }  | 
                                                        |
| 581 | -  | 
                                                        |
| 582 | -			foreach ($table->getForeignKeys() as $thing) { | 
                                                        |
| 583 | -				if ((!$sourceTable instanceof Table || !$sourceTable->hasForeignKey($thing->getName())) && \strlen($thing->getName()) > 30) { | 
                                                        |
| 584 | -					throw new \InvalidArgumentException('Foreign key name "'  . $table->getName() . '"."' . $thing->getName() . '" is too long.'); | 
                                                        |
| 585 | - }  | 
                                                        |
| 586 | - }  | 
                                                        |
| 587 | -  | 
                                                        |
| 588 | - $primaryKey = $table->getPrimaryKey();  | 
                                                        |
| 589 | -			if ($primaryKey instanceof Index && (!$sourceTable instanceof Table || !$sourceTable->hasPrimaryKey())) { | 
                                                        |
| 590 | - $indexName = strtolower($primaryKey->getName());  | 
                                                        |
| 591 | - $isUsingDefaultName = $indexName === 'primary';  | 
                                                        |
| 592 | -  | 
                                                        |
| 593 | -				if ($this->connection->getDatabasePlatform() instanceof PostgreSqlPlatform) { | 
                                                        |
| 594 | - $defaultName = $table->getName() . '_pkey';  | 
                                                        |
| 595 | - $isUsingDefaultName = strtolower($defaultName) === $indexName;  | 
                                                        |
| 596 | -  | 
                                                        |
| 597 | -					if ($isUsingDefaultName) { | 
                                                        |
| 598 | -						$sequenceName = $table->getName() . '_' . implode('_', $primaryKey->getColumns()) . '_seq'; | 
                                                        |
| 599 | -						$sequences = array_filter($sequences, function (Sequence $sequence) use ($sequenceName) { | 
                                                        |
| 600 | - return $sequence->getName() !== $sequenceName;  | 
                                                        |
| 601 | - });  | 
                                                        |
| 602 | - }  | 
                                                        |
| 603 | -				} elseif ($this->connection->getDatabasePlatform() instanceof OraclePlatform) { | 
                                                        |
| 604 | - $defaultName = $table->getName() . '_seq';  | 
                                                        |
| 605 | - $isUsingDefaultName = strtolower($defaultName) === $indexName;  | 
                                                        |
| 606 | - }  | 
                                                        |
| 607 | -  | 
                                                        |
| 608 | -				if (!$isUsingDefaultName && \strlen($indexName) > 30) { | 
                                                        |
| 609 | -					throw new \InvalidArgumentException('Primary index name  on "'  . $table->getName() . '" is too long.'); | 
                                                        |
| 610 | - }  | 
                                                        |
| 611 | -				if ($isUsingDefaultName && \strlen($table->getName()) - $prefixLength >= 23) { | 
                                                        |
| 612 | -					throw new \InvalidArgumentException('Primary index name  on "'  . $table->getName() . '" is too long.'); | 
                                                        |
| 613 | - }  | 
                                                        |
| 614 | - }  | 
                                                        |
| 615 | - }  | 
                                                        |
| 616 | -  | 
                                                        |
| 617 | -		foreach ($sequences as $sequence) { | 
                                                        |
| 618 | -			if (!$sourceSchema->hasSequence($sequence->getName()) && \strlen($sequence->getName()) > 30) { | 
                                                        |
| 619 | -				throw new \InvalidArgumentException('Sequence name "'  . $sequence->getName() . '" is too long.'); | 
                                                        |
| 620 | - }  | 
                                                        |
| 621 | - }  | 
                                                        |
| 622 | - }  | 
                                                        |
| 623 | -  | 
                                                        |
| 624 | -	private function ensureMigrationsAreLoaded() { | 
                                                        |
| 625 | -		if (empty($this->migrations)) { | 
                                                        |
| 626 | - $this->migrations = $this->findMigrations();  | 
                                                        |
| 627 | - }  | 
                                                        |
| 628 | - }  | 
                                                        |
| 50 | + /** @var boolean */  | 
                                                        |
| 51 | + private $migrationTableCreated;  | 
                                                        |
| 52 | + /** @var array */  | 
                                                        |
| 53 | + private $migrations;  | 
                                                        |
| 54 | + /** @var IOutput */  | 
                                                        |
| 55 | + private $output;  | 
                                                        |
| 56 | + /** @var Connection */  | 
                                                        |
| 57 | + private $connection;  | 
                                                        |
| 58 | + /** @var string */  | 
                                                        |
| 59 | + private $appName;  | 
                                                        |
| 60 | + /** @var bool */  | 
                                                        |
| 61 | + private $checkOracle;  | 
                                                        |
| 62 | +  | 
                                                        |
| 63 | + /**  | 
                                                        |
| 64 | + * MigrationService constructor.  | 
                                                        |
| 65 | + *  | 
                                                        |
| 66 | + * @param $appName  | 
                                                        |
| 67 | + * @param IDBConnection $connection  | 
                                                        |
| 68 | + * @param AppLocator $appLocator  | 
                                                        |
| 69 | + * @param IOutput|null $output  | 
                                                        |
| 70 | + * @throws \Exception  | 
                                                        |
| 71 | + */  | 
                                                        |
| 72 | +    public function __construct($appName, IDBConnection $connection, IOutput $output = null, AppLocator $appLocator = null) { | 
                                                        |
| 73 | + $this->appName = $appName;  | 
                                                        |
| 74 | + $this->connection = $connection;  | 
                                                        |
| 75 | + $this->output = $output;  | 
                                                        |
| 76 | +        if (null === $this->output) { | 
                                                        |
| 77 | + $this->output = new SimpleOutput(\OC::$server->getLogger(), $appName);  | 
                                                        |
| 78 | + }  | 
                                                        |
| 79 | +  | 
                                                        |
| 80 | +        if ($appName === 'core') { | 
                                                        |
| 81 | + $this->migrationsPath = \OC::$SERVERROOT . '/core/Migrations';  | 
                                                        |
| 82 | + $this->migrationsNamespace = 'OC\\Core\\Migrations';  | 
                                                        |
| 83 | + $this->checkOracle = true;  | 
                                                        |
| 84 | +        } else { | 
                                                        |
| 85 | +            if (null === $appLocator) { | 
                                                        |
| 86 | + $appLocator = new AppLocator();  | 
                                                        |
| 87 | + }  | 
                                                        |
| 88 | + $appPath = $appLocator->getAppPath($appName);  | 
                                                        |
| 89 | + $namespace = App::buildAppNamespace($appName);  | 
                                                        |
| 90 | + $this->migrationsPath = "$appPath/lib/Migration";  | 
                                                        |
| 91 | + $this->migrationsNamespace = $namespace . '\\Migration';  | 
                                                        |
| 92 | +  | 
                                                        |
| 93 | + $infoParser = new InfoParser();  | 
                                                        |
| 94 | + $info = $infoParser->parse($appPath . '/appinfo/info.xml');  | 
                                                        |
| 95 | +            if (!isset($info['dependencies']['database'])) { | 
                                                        |
| 96 | + $this->checkOracle = true;  | 
                                                        |
| 97 | +            } else { | 
                                                        |
| 98 | + $this->checkOracle = false;  | 
                                                        |
| 99 | +                foreach ($info['dependencies']['database'] as $database) { | 
                                                        |
| 100 | +                    if (\is_string($database) && $database === 'oci') { | 
                                                        |
| 101 | + $this->checkOracle = true;  | 
                                                        |
| 102 | +                    } elseif (\is_array($database) && isset($database['@value']) && $database['@value'] === 'oci') { | 
                                                        |
| 103 | + $this->checkOracle = true;  | 
                                                        |
| 104 | + }  | 
                                                        |
| 105 | + }  | 
                                                        |
| 106 | + }  | 
                                                        |
| 107 | + }  | 
                                                        |
| 108 | + }  | 
                                                        |
| 109 | +  | 
                                                        |
| 110 | + /**  | 
                                                        |
| 111 | + * Returns the name of the app for which this migration is executed  | 
                                                        |
| 112 | + *  | 
                                                        |
| 113 | + * @return string  | 
                                                        |
| 114 | + */  | 
                                                        |
| 115 | +    public function getApp() { | 
                                                        |
| 116 | + return $this->appName;  | 
                                                        |
| 117 | + }  | 
                                                        |
| 118 | +  | 
                                                        |
| 119 | + /**  | 
                                                        |
| 120 | + * @return bool  | 
                                                        |
| 121 | + * @codeCoverageIgnore - this will implicitly tested on installation  | 
                                                        |
| 122 | + */  | 
                                                        |
| 123 | +    private function createMigrationTable() { | 
                                                        |
| 124 | +        if ($this->migrationTableCreated) { | 
                                                        |
| 125 | + return false;  | 
                                                        |
| 126 | + }  | 
                                                        |
| 127 | +  | 
                                                        |
| 128 | +        if ($this->connection->tableExists('migrations')) { | 
                                                        |
| 129 | + $this->migrationTableCreated = true;  | 
                                                        |
| 130 | + return false;  | 
                                                        |
| 131 | + }  | 
                                                        |
| 132 | +  | 
                                                        |
| 133 | + $schema = new SchemaWrapper($this->connection);  | 
                                                        |
| 134 | +  | 
                                                        |
| 135 | + /**  | 
                                                        |
| 136 | + * We drop the table when it has different columns or the definition does not  | 
                                                        |
| 137 | + * match. E.g. ownCloud uses a length of 177 for app and 14 for version.  | 
                                                        |
| 138 | + */  | 
                                                        |
| 139 | +        try { | 
                                                        |
| 140 | +            $table = $schema->getTable('migrations'); | 
                                                        |
| 141 | + $columns = $table->getColumns();  | 
                                                        |
| 142 | +  | 
                                                        |
| 143 | +            if (count($columns) === 2) { | 
                                                        |
| 144 | +                try { | 
                                                        |
| 145 | +                    $column = $table->getColumn('app'); | 
                                                        |
| 146 | + $schemaMismatch = $column->getLength() !== 255;  | 
                                                        |
| 147 | +  | 
                                                        |
| 148 | +                    if (!$schemaMismatch) { | 
                                                        |
| 149 | +                        $column = $table->getColumn('version'); | 
                                                        |
| 150 | + $schemaMismatch = $column->getLength() !== 255;  | 
                                                        |
| 151 | + }  | 
                                                        |
| 152 | +                } catch (SchemaException $e) { | 
                                                        |
| 153 | + // One of the columns is missing  | 
                                                        |
| 154 | + $schemaMismatch = true;  | 
                                                        |
| 155 | + }  | 
                                                        |
| 156 | +  | 
                                                        |
| 157 | +                if (!$schemaMismatch) { | 
                                                        |
| 158 | + // Table exists and schema matches: return back!  | 
                                                        |
| 159 | + $this->migrationTableCreated = true;  | 
                                                        |
| 160 | + return false;  | 
                                                        |
| 161 | + }  | 
                                                        |
| 162 | + }  | 
                                                        |
| 163 | +  | 
                                                        |
| 164 | + // Drop the table, when it didn't match our expectations.  | 
                                                        |
| 165 | +            $this->connection->dropTable('migrations'); | 
                                                        |
| 166 | +  | 
                                                        |
| 167 | + // Recreate the schema after the table was dropped.  | 
                                                        |
| 168 | + $schema = new SchemaWrapper($this->connection);  | 
                                                        |
| 169 | +        } catch (SchemaException $e) { | 
                                                        |
| 170 | + // Table not found, no need to panic, we will create it.  | 
                                                        |
| 171 | + }  | 
                                                        |
| 172 | +  | 
                                                        |
| 173 | +        $table = $schema->createTable('migrations'); | 
                                                        |
| 174 | +        $table->addColumn('app', Types::STRING, ['length' => 255]); | 
                                                        |
| 175 | +        $table->addColumn('version', Types::STRING, ['length' => 255]); | 
                                                        |
| 176 | + $table->setPrimaryKey(['app', 'version']);  | 
                                                        |
| 177 | +  | 
                                                        |
| 178 | + $this->connection->migrateToSchema($schema->getWrappedSchema());  | 
                                                        |
| 179 | +  | 
                                                        |
| 180 | + $this->migrationTableCreated = true;  | 
                                                        |
| 181 | +  | 
                                                        |
| 182 | + return true;  | 
                                                        |
| 183 | + }  | 
                                                        |
| 184 | +  | 
                                                        |
| 185 | + /**  | 
                                                        |
| 186 | + * Returns all versions which have already been applied  | 
                                                        |
| 187 | + *  | 
                                                        |
| 188 | + * @return string[]  | 
                                                        |
| 189 | + * @codeCoverageIgnore - no need to test this  | 
                                                        |
| 190 | + */  | 
                                                        |
| 191 | +    public function getMigratedVersions() { | 
                                                        |
| 192 | + $this->createMigrationTable();  | 
                                                        |
| 193 | + $qb = $this->connection->getQueryBuilder();  | 
                                                        |
| 194 | +  | 
                                                        |
| 195 | +        $qb->select('version') | 
                                                        |
| 196 | +            ->from('migrations') | 
                                                        |
| 197 | +            ->where($qb->expr()->eq('app', $qb->createNamedParameter($this->getApp()))) | 
                                                        |
| 198 | +            ->orderBy('version'); | 
                                                        |
| 199 | +  | 
                                                        |
| 200 | + $result = $qb->execute();  | 
                                                        |
| 201 | + $rows = $result->fetchAll(\PDO::FETCH_COLUMN);  | 
                                                        |
| 202 | + $result->closeCursor();  | 
                                                        |
| 203 | +  | 
                                                        |
| 204 | + return $rows;  | 
                                                        |
| 205 | + }  | 
                                                        |
| 206 | +  | 
                                                        |
| 207 | + /**  | 
                                                        |
| 208 | + * Returns all versions which are available in the migration folder  | 
                                                        |
| 209 | + *  | 
                                                        |
| 210 | + * @return array  | 
                                                        |
| 211 | + */  | 
                                                        |
| 212 | +    public function getAvailableVersions() { | 
                                                        |
| 213 | + $this->ensureMigrationsAreLoaded();  | 
                                                        |
| 214 | +        return array_map('strval', array_keys($this->migrations)); | 
                                                        |
| 215 | + }  | 
                                                        |
| 216 | +  | 
                                                        |
| 217 | +    protected function findMigrations() { | 
                                                        |
| 218 | + $directory = realpath($this->migrationsPath);  | 
                                                        |
| 219 | +        if ($directory === false || !file_exists($directory) || !is_dir($directory)) { | 
                                                        |
| 220 | + return [];  | 
                                                        |
| 221 | + }  | 
                                                        |
| 222 | +  | 
                                                        |
| 223 | + $iterator = new \RegexIterator(  | 
                                                        |
| 224 | + new \RecursiveIteratorIterator(  | 
                                                        |
| 225 | + new \RecursiveDirectoryIterator($directory, \FilesystemIterator::SKIP_DOTS),  | 
                                                        |
| 226 | + \RecursiveIteratorIterator::LEAVES_ONLY  | 
                                                        |
| 227 | + ),  | 
                                                        |
| 228 | +            '#^.+\\/Version[^\\/]{1,255}\\.php$#i', | 
                                                        |
| 229 | + \RegexIterator::GET_MATCH);  | 
                                                        |
| 230 | +  | 
                                                        |
| 231 | + $files = array_keys(iterator_to_array($iterator));  | 
                                                        |
| 232 | +        uasort($files, function ($a, $b) { | 
                                                        |
| 233 | +            preg_match('/^Version(\d+)Date(\d+)\\.php$/', basename($a), $matchA); | 
                                                        |
| 234 | +            preg_match('/^Version(\d+)Date(\d+)\\.php$/', basename($b), $matchB); | 
                                                        |
| 235 | +            if (!empty($matchA) && !empty($matchB)) { | 
                                                        |
| 236 | +                if ($matchA[1] !== $matchB[1]) { | 
                                                        |
| 237 | + return ($matchA[1] < $matchB[1]) ? -1 : 1;  | 
                                                        |
| 238 | + }  | 
                                                        |
| 239 | + return ($matchA[2] < $matchB[2]) ? -1 : 1;  | 
                                                        |
| 240 | + }  | 
                                                        |
| 241 | + return (basename($a) < basename($b)) ? -1 : 1;  | 
                                                        |
| 242 | + });  | 
                                                        |
| 243 | +  | 
                                                        |
| 244 | + $migrations = [];  | 
                                                        |
| 245 | +  | 
                                                        |
| 246 | +        foreach ($files as $file) { | 
                                                        |
| 247 | + $className = basename($file, '.php');  | 
                                                        |
| 248 | + $version = (string) substr($className, 7);  | 
                                                        |
| 249 | +            if ($version === '0') { | 
                                                        |
| 250 | + throw new \InvalidArgumentException(  | 
                                                        |
| 251 | + "Cannot load a migrations with the name '$version' because it is a reserved number"  | 
                                                        |
| 252 | + );  | 
                                                        |
| 253 | + }  | 
                                                        |
| 254 | +            $migrations[$version] = sprintf('%s\\%s', $this->migrationsNamespace, $className); | 
                                                        |
| 255 | + }  | 
                                                        |
| 256 | +  | 
                                                        |
| 257 | + return $migrations;  | 
                                                        |
| 258 | + }  | 
                                                        |
| 259 | +  | 
                                                        |
| 260 | + /**  | 
                                                        |
| 261 | + * @param string $to  | 
                                                        |
| 262 | + * @return string[]  | 
                                                        |
| 263 | + */  | 
                                                        |
| 264 | +    private function getMigrationsToExecute($to) { | 
                                                        |
| 265 | + $knownMigrations = $this->getMigratedVersions();  | 
                                                        |
| 266 | + $availableMigrations = $this->getAvailableVersions();  | 
                                                        |
| 267 | +  | 
                                                        |
| 268 | + $toBeExecuted = [];  | 
                                                        |
| 269 | +        foreach ($availableMigrations as $v) { | 
                                                        |
| 270 | +            if ($to !== 'latest' && $v > $to) { | 
                                                        |
| 271 | + continue;  | 
                                                        |
| 272 | + }  | 
                                                        |
| 273 | +            if ($this->shallBeExecuted($v, $knownMigrations)) { | 
                                                        |
| 274 | + $toBeExecuted[] = $v;  | 
                                                        |
| 275 | + }  | 
                                                        |
| 276 | + }  | 
                                                        |
| 277 | +  | 
                                                        |
| 278 | + return $toBeExecuted;  | 
                                                        |
| 279 | + }  | 
                                                        |
| 280 | +  | 
                                                        |
| 281 | + /**  | 
                                                        |
| 282 | + * @param string $m  | 
                                                        |
| 283 | + * @param string[] $knownMigrations  | 
                                                        |
| 284 | + * @return bool  | 
                                                        |
| 285 | + */  | 
                                                        |
| 286 | +    private function shallBeExecuted($m, $knownMigrations) { | 
                                                        |
| 287 | +        if (in_array($m, $knownMigrations)) { | 
                                                        |
| 288 | + return false;  | 
                                                        |
| 289 | + }  | 
                                                        |
| 290 | +  | 
                                                        |
| 291 | + return true;  | 
                                                        |
| 292 | + }  | 
                                                        |
| 293 | +  | 
                                                        |
| 294 | + /**  | 
                                                        |
| 295 | + * @param string $version  | 
                                                        |
| 296 | + */  | 
                                                        |
| 297 | +    private function markAsExecuted($version) { | 
                                                        |
| 298 | +        $this->connection->insertIfNotExist('*PREFIX*migrations', [ | 
                                                        |
| 299 | + 'app' => $this->appName,  | 
                                                        |
| 300 | + 'version' => $version  | 
                                                        |
| 301 | + ]);  | 
                                                        |
| 302 | + }  | 
                                                        |
| 303 | +  | 
                                                        |
| 304 | + /**  | 
                                                        |
| 305 | + * Returns the name of the table which holds the already applied versions  | 
                                                        |
| 306 | + *  | 
                                                        |
| 307 | + * @return string  | 
                                                        |
| 308 | + */  | 
                                                        |
| 309 | +    public function getMigrationsTableName() { | 
                                                        |
| 310 | + return $this->connection->getPrefix() . 'migrations';  | 
                                                        |
| 311 | + }  | 
                                                        |
| 312 | +  | 
                                                        |
| 313 | + /**  | 
                                                        |
| 314 | + * Returns the namespace of the version classes  | 
                                                        |
| 315 | + *  | 
                                                        |
| 316 | + * @return string  | 
                                                        |
| 317 | + */  | 
                                                        |
| 318 | +    public function getMigrationsNamespace() { | 
                                                        |
| 319 | + return $this->migrationsNamespace;  | 
                                                        |
| 320 | + }  | 
                                                        |
| 321 | +  | 
                                                        |
| 322 | + /**  | 
                                                        |
| 323 | + * Returns the directory which holds the versions  | 
                                                        |
| 324 | + *  | 
                                                        |
| 325 | + * @return string  | 
                                                        |
| 326 | + */  | 
                                                        |
| 327 | +    public function getMigrationsDirectory() { | 
                                                        |
| 328 | + return $this->migrationsPath;  | 
                                                        |
| 329 | + }  | 
                                                        |
| 330 | +  | 
                                                        |
| 331 | + /**  | 
                                                        |
| 332 | + * Return the explicit version for the aliases; current, next, prev, latest  | 
                                                        |
| 333 | + *  | 
                                                        |
| 334 | + * @param string $alias  | 
                                                        |
| 335 | + * @return mixed|null|string  | 
                                                        |
| 336 | + */  | 
                                                        |
| 337 | +    public function getMigration($alias) { | 
                                                        |
| 338 | +        switch ($alias) { | 
                                                        |
| 339 | + case 'current':  | 
                                                        |
| 340 | + return $this->getCurrentVersion();  | 
                                                        |
| 341 | + case 'next':  | 
                                                        |
| 342 | + return $this->getRelativeVersion($this->getCurrentVersion(), 1);  | 
                                                        |
| 343 | + case 'prev':  | 
                                                        |
| 344 | + return $this->getRelativeVersion($this->getCurrentVersion(), -1);  | 
                                                        |
| 345 | + case 'latest':  | 
                                                        |
| 346 | + $this->ensureMigrationsAreLoaded();  | 
                                                        |
| 347 | +  | 
                                                        |
| 348 | + $migrations = $this->getAvailableVersions();  | 
                                                        |
| 349 | + return @end($migrations);  | 
                                                        |
| 350 | + }  | 
                                                        |
| 351 | + return '0';  | 
                                                        |
| 352 | + }  | 
                                                        |
| 353 | +  | 
                                                        |
| 354 | + /**  | 
                                                        |
| 355 | + * @param string $version  | 
                                                        |
| 356 | + * @param int $delta  | 
                                                        |
| 357 | + * @return null|string  | 
                                                        |
| 358 | + */  | 
                                                        |
| 359 | +    private function getRelativeVersion($version, $delta) { | 
                                                        |
| 360 | + $this->ensureMigrationsAreLoaded();  | 
                                                        |
| 361 | +  | 
                                                        |
| 362 | + $versions = $this->getAvailableVersions();  | 
                                                        |
| 363 | + array_unshift($versions, 0);  | 
                                                        |
| 364 | + $offset = array_search($version, $versions, true);  | 
                                                        |
| 365 | +        if ($offset === false || !isset($versions[$offset + $delta])) { | 
                                                        |
| 366 | + // Unknown version or delta out of bounds.  | 
                                                        |
| 367 | + return null;  | 
                                                        |
| 368 | + }  | 
                                                        |
| 369 | +  | 
                                                        |
| 370 | + return (string) $versions[$offset + $delta];  | 
                                                        |
| 371 | + }  | 
                                                        |
| 372 | +  | 
                                                        |
| 373 | + /**  | 
                                                        |
| 374 | + * @return string  | 
                                                        |
| 375 | + */  | 
                                                        |
| 376 | +    private function getCurrentVersion() { | 
                                                        |
| 377 | + $m = $this->getMigratedVersions();  | 
                                                        |
| 378 | +        if (count($m) === 0) { | 
                                                        |
| 379 | + return '0';  | 
                                                        |
| 380 | + }  | 
                                                        |
| 381 | + $migrations = array_values($m);  | 
                                                        |
| 382 | + return @end($migrations);  | 
                                                        |
| 383 | + }  | 
                                                        |
| 384 | +  | 
                                                        |
| 385 | + /**  | 
                                                        |
| 386 | + * @param string $version  | 
                                                        |
| 387 | + * @return string  | 
                                                        |
| 388 | + * @throws \InvalidArgumentException  | 
                                                        |
| 389 | + */  | 
                                                        |
| 390 | +    private function getClass($version) { | 
                                                        |
| 391 | + $this->ensureMigrationsAreLoaded();  | 
                                                        |
| 392 | +  | 
                                                        |
| 393 | +        if (isset($this->migrations[$version])) { | 
                                                        |
| 394 | + return $this->migrations[$version];  | 
                                                        |
| 395 | + }  | 
                                                        |
| 396 | +  | 
                                                        |
| 397 | +        throw new \InvalidArgumentException("Version $version is unknown."); | 
                                                        |
| 398 | + }  | 
                                                        |
| 399 | +  | 
                                                        |
| 400 | + /**  | 
                                                        |
| 401 | + * Allows to set an IOutput implementation which is used for logging progress and messages  | 
                                                        |
| 402 | + *  | 
                                                        |
| 403 | + * @param IOutput $output  | 
                                                        |
| 404 | + */  | 
                                                        |
| 405 | +    public function setOutput(IOutput $output) { | 
                                                        |
| 406 | + $this->output = $output;  | 
                                                        |
| 407 | + }  | 
                                                        |
| 408 | +  | 
                                                        |
| 409 | + /**  | 
                                                        |
| 410 | + * Applies all not yet applied versions up to $to  | 
                                                        |
| 411 | + *  | 
                                                        |
| 412 | + * @param string $to  | 
                                                        |
| 413 | + * @param bool $schemaOnly  | 
                                                        |
| 414 | + * @throws \InvalidArgumentException  | 
                                                        |
| 415 | + */  | 
                                                        |
| 416 | +    public function migrate($to = 'latest', $schemaOnly = false) { | 
                                                        |
| 417 | +        if ($schemaOnly) { | 
                                                        |
| 418 | + $this->migrateSchemaOnly($to);  | 
                                                        |
| 419 | + return;  | 
                                                        |
| 420 | + }  | 
                                                        |
| 421 | +  | 
                                                        |
| 422 | + // read known migrations  | 
                                                        |
| 423 | + $toBeExecuted = $this->getMigrationsToExecute($to);  | 
                                                        |
| 424 | +        foreach ($toBeExecuted as $version) { | 
                                                        |
| 425 | +            try { | 
                                                        |
| 426 | + $this->executeStep($version, $schemaOnly);  | 
                                                        |
| 427 | +            } catch (DriverException $e) { | 
                                                        |
| 428 | + // The exception itself does not contain the name of the migration,  | 
                                                        |
| 429 | + // so we wrap it here, to make debugging easier.  | 
                                                        |
| 430 | +                throw new \Exception('Database error when running migration ' . $to . ' for app ' . $this->getApp(), 0, $e); | 
                                                        |
| 431 | + }  | 
                                                        |
| 432 | + }  | 
                                                        |
| 433 | + }  | 
                                                        |
| 434 | +  | 
                                                        |
| 435 | + /**  | 
                                                        |
| 436 | + * Applies all not yet applied versions up to $to  | 
                                                        |
| 437 | + *  | 
                                                        |
| 438 | + * @param string $to  | 
                                                        |
| 439 | + * @throws \InvalidArgumentException  | 
                                                        |
| 440 | + */  | 
                                                        |
| 441 | +    public function migrateSchemaOnly($to = 'latest') { | 
                                                        |
| 442 | + // read known migrations  | 
                                                        |
| 443 | + $toBeExecuted = $this->getMigrationsToExecute($to);  | 
                                                        |
| 444 | +  | 
                                                        |
| 445 | +        if (empty($toBeExecuted)) { | 
                                                        |
| 446 | + return;  | 
                                                        |
| 447 | + }  | 
                                                        |
| 448 | +  | 
                                                        |
| 449 | + $toSchema = null;  | 
                                                        |
| 450 | +        foreach ($toBeExecuted as $version) { | 
                                                        |
| 451 | + $instance = $this->createInstance($version);  | 
                                                        |
| 452 | +  | 
                                                        |
| 453 | +            $toSchema = $instance->changeSchema($this->output, function () use ($toSchema) { | 
                                                        |
| 454 | + return $toSchema ?: new SchemaWrapper($this->connection);  | 
                                                        |
| 455 | + }, ['tablePrefix' => $this->connection->getPrefix()]) ?: $toSchema;  | 
                                                        |
| 456 | +  | 
                                                        |
| 457 | + $this->markAsExecuted($version);  | 
                                                        |
| 458 | + }  | 
                                                        |
| 459 | +  | 
                                                        |
| 460 | +        if ($toSchema instanceof SchemaWrapper) { | 
                                                        |
| 461 | + $targetSchema = $toSchema->getWrappedSchema();  | 
                                                        |
| 462 | +            if ($this->checkOracle) { | 
                                                        |
| 463 | + $beforeSchema = $this->connection->createSchema();  | 
                                                        |
| 464 | + $this->ensureOracleIdentifierLengthLimit($beforeSchema, $targetSchema, strlen($this->connection->getPrefix()));  | 
                                                        |
| 465 | + }  | 
                                                        |
| 466 | + $this->connection->migrateToSchema($targetSchema);  | 
                                                        |
| 467 | + $toSchema->performDropTableCalls();  | 
                                                        |
| 468 | + }  | 
                                                        |
| 469 | + }  | 
                                                        |
| 470 | +  | 
                                                        |
| 471 | + /**  | 
                                                        |
| 472 | + * Get the human readable descriptions for the migration steps to run  | 
                                                        |
| 473 | + *  | 
                                                        |
| 474 | + * @param string $to  | 
                                                        |
| 475 | + * @return string[] [$name => $description]  | 
                                                        |
| 476 | + */  | 
                                                        |
| 477 | +    public function describeMigrationStep($to = 'latest') { | 
                                                        |
| 478 | + $toBeExecuted = $this->getMigrationsToExecute($to);  | 
                                                        |
| 479 | + $description = [];  | 
                                                        |
| 480 | +        foreach ($toBeExecuted as $version) { | 
                                                        |
| 481 | + $migration = $this->createInstance($version);  | 
                                                        |
| 482 | +            if ($migration->name()) { | 
                                                        |
| 483 | + $description[$migration->name()] = $migration->description();  | 
                                                        |
| 484 | + }  | 
                                                        |
| 485 | + }  | 
                                                        |
| 486 | + return $description;  | 
                                                        |
| 487 | + }  | 
                                                        |
| 488 | +  | 
                                                        |
| 489 | + /**  | 
                                                        |
| 490 | + * @param string $version  | 
                                                        |
| 491 | + * @return IMigrationStep  | 
                                                        |
| 492 | + * @throws \InvalidArgumentException  | 
                                                        |
| 493 | + */  | 
                                                        |
| 494 | +    protected function createInstance($version) { | 
                                                        |
| 495 | + $class = $this->getClass($version);  | 
                                                        |
| 496 | +        try { | 
                                                        |
| 497 | + $s = \OC::$server->query($class);  | 
                                                        |
| 498 | +  | 
                                                        |
| 499 | +            if (!$s instanceof IMigrationStep) { | 
                                                        |
| 500 | +                throw new \InvalidArgumentException('Not a valid migration'); | 
                                                        |
| 501 | + }  | 
                                                        |
| 502 | +        } catch (QueryException $e) { | 
                                                        |
| 503 | +            if (class_exists($class)) { | 
                                                        |
| 504 | + $s = new $class();  | 
                                                        |
| 505 | +            } else { | 
                                                        |
| 506 | +                throw new \InvalidArgumentException("Migration step '$class' is unknown"); | 
                                                        |
| 507 | + }  | 
                                                        |
| 508 | + }  | 
                                                        |
| 509 | +  | 
                                                        |
| 510 | + return $s;  | 
                                                        |
| 511 | + }  | 
                                                        |
| 512 | +  | 
                                                        |
| 513 | + /**  | 
                                                        |
| 514 | + * Executes one explicit version  | 
                                                        |
| 515 | + *  | 
                                                        |
| 516 | + * @param string $version  | 
                                                        |
| 517 | + * @param bool $schemaOnly  | 
                                                        |
| 518 | + * @throws \InvalidArgumentException  | 
                                                        |
| 519 | + */  | 
                                                        |
| 520 | +    public function executeStep($version, $schemaOnly = false) { | 
                                                        |
| 521 | + $instance = $this->createInstance($version);  | 
                                                        |
| 522 | +  | 
                                                        |
| 523 | +        if (!$schemaOnly) { | 
                                                        |
| 524 | +            $instance->preSchemaChange($this->output, function () { | 
                                                        |
| 525 | + return new SchemaWrapper($this->connection);  | 
                                                        |
| 526 | + }, ['tablePrefix' => $this->connection->getPrefix()]);  | 
                                                        |
| 527 | + }  | 
                                                        |
| 528 | +  | 
                                                        |
| 529 | +        $toSchema = $instance->changeSchema($this->output, function () { | 
                                                        |
| 530 | + return new SchemaWrapper($this->connection);  | 
                                                        |
| 531 | + }, ['tablePrefix' => $this->connection->getPrefix()]);  | 
                                                        |
| 532 | +  | 
                                                        |
| 533 | +        if ($toSchema instanceof SchemaWrapper) { | 
                                                        |
| 534 | + $targetSchema = $toSchema->getWrappedSchema();  | 
                                                        |
| 535 | +            if ($this->checkOracle) { | 
                                                        |
| 536 | + $sourceSchema = $this->connection->createSchema();  | 
                                                        |
| 537 | + $this->ensureOracleIdentifierLengthLimit($sourceSchema, $targetSchema, strlen($this->connection->getPrefix()));  | 
                                                        |
| 538 | + }  | 
                                                        |
| 539 | + $this->connection->migrateToSchema($targetSchema);  | 
                                                        |
| 540 | + $toSchema->performDropTableCalls();  | 
                                                        |
| 541 | + }  | 
                                                        |
| 542 | +  | 
                                                        |
| 543 | +        if (!$schemaOnly) { | 
                                                        |
| 544 | +            $instance->postSchemaChange($this->output, function () { | 
                                                        |
| 545 | + return new SchemaWrapper($this->connection);  | 
                                                        |
| 546 | + }, ['tablePrefix' => $this->connection->getPrefix()]);  | 
                                                        |
| 547 | + }  | 
                                                        |
| 548 | +  | 
                                                        |
| 549 | + $this->markAsExecuted($version);  | 
                                                        |
| 550 | + }  | 
                                                        |
| 551 | +  | 
                                                        |
| 552 | +    public function ensureOracleIdentifierLengthLimit(Schema $sourceSchema, Schema $targetSchema, int $prefixLength) { | 
                                                        |
| 553 | + $sequences = $targetSchema->getSequences();  | 
                                                        |
| 554 | +  | 
                                                        |
| 555 | +        foreach ($targetSchema->getTables() as $table) { | 
                                                        |
| 556 | +            try { | 
                                                        |
| 557 | + $sourceTable = $sourceSchema->getTable($table->getName());  | 
                                                        |
| 558 | +            } catch (SchemaException $e) { | 
                                                        |
| 559 | +                if (\strlen($table->getName()) - $prefixLength > 27) { | 
                                                        |
| 560 | +                    throw new \InvalidArgumentException('Table name "'  . $table->getName() . '" is too long.'); | 
                                                        |
| 561 | + }  | 
                                                        |
| 562 | + $sourceTable = null;  | 
                                                        |
| 563 | + }  | 
                                                        |
| 564 | +  | 
                                                        |
| 565 | +            foreach ($table->getColumns() as $thing) { | 
                                                        |
| 566 | +                if ((!$sourceTable instanceof Table || !$sourceTable->hasColumn($thing->getName())) && \strlen($thing->getName()) > 30) { | 
                                                        |
| 567 | +                    throw new \InvalidArgumentException('Column name "'  . $table->getName() . '"."' . $thing->getName() . '" is too long.'); | 
                                                        |
| 568 | + }  | 
                                                        |
| 569 | +  | 
                                                        |
| 570 | + if ($thing->getNotnull() && $thing->getDefault() === ''  | 
                                                        |
| 571 | +                    && $sourceTable instanceof Table && !$sourceTable->hasColumn($thing->getName())) { | 
                                                        |
| 572 | +                    throw new \InvalidArgumentException('Column name "'  . $table->getName() . '"."' . $thing->getName() . '" is NotNull, but has empty string or null as default.'); | 
                                                        |
| 573 | + }  | 
                                                        |
| 574 | + }  | 
                                                        |
| 575 | +  | 
                                                        |
| 576 | +            foreach ($table->getIndexes() as $thing) { | 
                                                        |
| 577 | +                if ((!$sourceTable instanceof Table || !$sourceTable->hasIndex($thing->getName())) && \strlen($thing->getName()) > 30) { | 
                                                        |
| 578 | +                    throw new \InvalidArgumentException('Index name "'  . $table->getName() . '"."' . $thing->getName() . '" is too long.'); | 
                                                        |
| 579 | + }  | 
                                                        |
| 580 | + }  | 
                                                        |
| 581 | +  | 
                                                        |
| 582 | +            foreach ($table->getForeignKeys() as $thing) { | 
                                                        |
| 583 | +                if ((!$sourceTable instanceof Table || !$sourceTable->hasForeignKey($thing->getName())) && \strlen($thing->getName()) > 30) { | 
                                                        |
| 584 | +                    throw new \InvalidArgumentException('Foreign key name "'  . $table->getName() . '"."' . $thing->getName() . '" is too long.'); | 
                                                        |
| 585 | + }  | 
                                                        |
| 586 | + }  | 
                                                        |
| 587 | +  | 
                                                        |
| 588 | + $primaryKey = $table->getPrimaryKey();  | 
                                                        |
| 589 | +            if ($primaryKey instanceof Index && (!$sourceTable instanceof Table || !$sourceTable->hasPrimaryKey())) { | 
                                                        |
| 590 | + $indexName = strtolower($primaryKey->getName());  | 
                                                        |
| 591 | + $isUsingDefaultName = $indexName === 'primary';  | 
                                                        |
| 592 | +  | 
                                                        |
| 593 | +                if ($this->connection->getDatabasePlatform() instanceof PostgreSqlPlatform) { | 
                                                        |
| 594 | + $defaultName = $table->getName() . '_pkey';  | 
                                                        |
| 595 | + $isUsingDefaultName = strtolower($defaultName) === $indexName;  | 
                                                        |
| 596 | +  | 
                                                        |
| 597 | +                    if ($isUsingDefaultName) { | 
                                                        |
| 598 | +                        $sequenceName = $table->getName() . '_' . implode('_', $primaryKey->getColumns()) . '_seq'; | 
                                                        |
| 599 | +                        $sequences = array_filter($sequences, function (Sequence $sequence) use ($sequenceName) { | 
                                                        |
| 600 | + return $sequence->getName() !== $sequenceName;  | 
                                                        |
| 601 | + });  | 
                                                        |
| 602 | + }  | 
                                                        |
| 603 | +                } elseif ($this->connection->getDatabasePlatform() instanceof OraclePlatform) { | 
                                                        |
| 604 | + $defaultName = $table->getName() . '_seq';  | 
                                                        |
| 605 | + $isUsingDefaultName = strtolower($defaultName) === $indexName;  | 
                                                        |
| 606 | + }  | 
                                                        |
| 607 | +  | 
                                                        |
| 608 | +                if (!$isUsingDefaultName && \strlen($indexName) > 30) { | 
                                                        |
| 609 | +                    throw new \InvalidArgumentException('Primary index name  on "'  . $table->getName() . '" is too long.'); | 
                                                        |
| 610 | + }  | 
                                                        |
| 611 | +                if ($isUsingDefaultName && \strlen($table->getName()) - $prefixLength >= 23) { | 
                                                        |
| 612 | +                    throw new \InvalidArgumentException('Primary index name  on "'  . $table->getName() . '" is too long.'); | 
                                                        |
| 613 | + }  | 
                                                        |
| 614 | + }  | 
                                                        |
| 615 | + }  | 
                                                        |
| 616 | +  | 
                                                        |
| 617 | +        foreach ($sequences as $sequence) { | 
                                                        |
| 618 | +            if (!$sourceSchema->hasSequence($sequence->getName()) && \strlen($sequence->getName()) > 30) { | 
                                                        |
| 619 | +                throw new \InvalidArgumentException('Sequence name "'  . $sequence->getName() . '" is too long.'); | 
                                                        |
| 620 | + }  | 
                                                        |
| 621 | + }  | 
                                                        |
| 622 | + }  | 
                                                        |
| 623 | +  | 
                                                        |
| 624 | +    private function ensureMigrationsAreLoaded() { | 
                                                        |
| 625 | +        if (empty($this->migrations)) { | 
                                                        |
| 626 | + $this->migrations = $this->findMigrations();  | 
                                                        |
| 627 | + }  | 
                                                        |
| 628 | + }  | 
                                                        |
| 629 | 629 | }  | 
                                                        
@@ -78,7 +78,7 @@ discard block  | 
                                                    ||
| 78 | 78 | }  | 
                                                        
| 79 | 79 | |
| 80 | 80 |  		if ($appName === 'core') { | 
                                                        
| 81 | - $this->migrationsPath = \OC::$SERVERROOT . '/core/Migrations';  | 
                                                        |
| 81 | + $this->migrationsPath = \OC::$SERVERROOT.'/core/Migrations';  | 
                                                        |
| 82 | 82 | $this->migrationsNamespace = 'OC\\Core\\Migrations';  | 
                                                        
| 83 | 83 | $this->checkOracle = true;  | 
                                                        
| 84 | 84 |  		} else { | 
                                                        
@@ -88,10 +88,10 @@ discard block  | 
                                                    ||
| 88 | 88 | $appPath = $appLocator->getAppPath($appName);  | 
                                                        
| 89 | 89 | $namespace = App::buildAppNamespace($appName);  | 
                                                        
| 90 | 90 | $this->migrationsPath = "$appPath/lib/Migration";  | 
                                                        
| 91 | - $this->migrationsNamespace = $namespace . '\\Migration';  | 
                                                        |
| 91 | + $this->migrationsNamespace = $namespace.'\\Migration';  | 
                                                        |
| 92 | 92 | |
| 93 | 93 | $infoParser = new InfoParser();  | 
                                                        
| 94 | - $info = $infoParser->parse($appPath . '/appinfo/info.xml');  | 
                                                        |
| 94 | + $info = $infoParser->parse($appPath.'/appinfo/info.xml');  | 
                                                        |
| 95 | 95 |  			if (!isset($info['dependencies']['database'])) { | 
                                                        
| 96 | 96 | $this->checkOracle = true;  | 
                                                        
| 97 | 97 |  			} else { | 
                                                        
@@ -229,7 +229,7 @@ discard block  | 
                                                    ||
| 229 | 229 | \RegexIterator::GET_MATCH);  | 
                                                        
| 230 | 230 | |
| 231 | 231 | $files = array_keys(iterator_to_array($iterator));  | 
                                                        
| 232 | -		uasort($files, function ($a, $b) { | 
                                                        |
| 232 | +		uasort($files, function($a, $b) { | 
                                                        |
| 233 | 233 |  			preg_match('/^Version(\d+)Date(\d+)\\.php$/', basename($a), $matchA); | 
                                                        
| 234 | 234 |  			preg_match('/^Version(\d+)Date(\d+)\\.php$/', basename($b), $matchB); | 
                                                        
| 235 | 235 |  			if (!empty($matchA) && !empty($matchB)) { | 
                                                        
@@ -307,7 +307,7 @@ discard block  | 
                                                    ||
| 307 | 307 | * @return string  | 
                                                        
| 308 | 308 | */  | 
                                                        
| 309 | 309 |  	public function getMigrationsTableName() { | 
                                                        
| 310 | - return $this->connection->getPrefix() . 'migrations';  | 
                                                        |
| 310 | + return $this->connection->getPrefix().'migrations';  | 
                                                        |
| 311 | 311 | }  | 
                                                        
| 312 | 312 | |
| 313 | 313 | /**  | 
                                                        
@@ -427,7 +427,7 @@ discard block  | 
                                                    ||
| 427 | 427 |  			} catch (DriverException $e) { | 
                                                        
| 428 | 428 | // The exception itself does not contain the name of the migration,  | 
                                                        
| 429 | 429 | // so we wrap it here, to make debugging easier.  | 
                                                        
| 430 | -				throw new \Exception('Database error when running migration ' . $to . ' for app ' . $this->getApp(), 0, $e); | 
                                                        |
| 430 | +				throw new \Exception('Database error when running migration '.$to.' for app '.$this->getApp(), 0, $e); | 
                                                        |
| 431 | 431 | }  | 
                                                        
| 432 | 432 | }  | 
                                                        
| 433 | 433 | }  | 
                                                        
@@ -450,7 +450,7 @@ discard block  | 
                                                    ||
| 450 | 450 |  		foreach ($toBeExecuted as $version) { | 
                                                        
| 451 | 451 | $instance = $this->createInstance($version);  | 
                                                        
| 452 | 452 | |
| 453 | -			$toSchema = $instance->changeSchema($this->output, function () use ($toSchema) { | 
                                                        |
| 453 | +			$toSchema = $instance->changeSchema($this->output, function() use ($toSchema) { | 
                                                        |
| 454 | 454 | return $toSchema ?: new SchemaWrapper($this->connection);  | 
                                                        
| 455 | 455 | }, ['tablePrefix' => $this->connection->getPrefix()]) ?: $toSchema;  | 
                                                        
| 456 | 456 | |
@@ -521,12 +521,12 @@ discard block  | 
                                                    ||
| 521 | 521 | $instance = $this->createInstance($version);  | 
                                                        
| 522 | 522 | |
| 523 | 523 |  		if (!$schemaOnly) { | 
                                                        
| 524 | -			$instance->preSchemaChange($this->output, function () { | 
                                                        |
| 524 | +			$instance->preSchemaChange($this->output, function() { | 
                                                        |
| 525 | 525 | return new SchemaWrapper($this->connection);  | 
                                                        
| 526 | 526 | }, ['tablePrefix' => $this->connection->getPrefix()]);  | 
                                                        
| 527 | 527 | }  | 
                                                        
| 528 | 528 | |
| 529 | -		$toSchema = $instance->changeSchema($this->output, function () { | 
                                                        |
| 529 | +		$toSchema = $instance->changeSchema($this->output, function() { | 
                                                        |
| 530 | 530 | return new SchemaWrapper($this->connection);  | 
                                                        
| 531 | 531 | }, ['tablePrefix' => $this->connection->getPrefix()]);  | 
                                                        
| 532 | 532 | |
@@ -541,7 +541,7 @@ discard block  | 
                                                    ||
| 541 | 541 | }  | 
                                                        
| 542 | 542 | |
| 543 | 543 |  		if (!$schemaOnly) { | 
                                                        
| 544 | -			$instance->postSchemaChange($this->output, function () { | 
                                                        |
| 544 | +			$instance->postSchemaChange($this->output, function() { | 
                                                        |
| 545 | 545 | return new SchemaWrapper($this->connection);  | 
                                                        
| 546 | 546 | }, ['tablePrefix' => $this->connection->getPrefix()]);  | 
                                                        
| 547 | 547 | }  | 
                                                        
@@ -557,31 +557,31 @@ discard block  | 
                                                    ||
| 557 | 557 | $sourceTable = $sourceSchema->getTable($table->getName());  | 
                                                        
| 558 | 558 |  			} catch (SchemaException $e) { | 
                                                        
| 559 | 559 |  				if (\strlen($table->getName()) - $prefixLength > 27) { | 
                                                        
| 560 | -					throw new \InvalidArgumentException('Table name "'  . $table->getName() . '" is too long.'); | 
                                                        |
| 560 | +					throw new \InvalidArgumentException('Table name "'.$table->getName().'" is too long.'); | 
                                                        |
| 561 | 561 | }  | 
                                                        
| 562 | 562 | $sourceTable = null;  | 
                                                        
| 563 | 563 | }  | 
                                                        
| 564 | 564 | |
| 565 | 565 |  			foreach ($table->getColumns() as $thing) { | 
                                                        
| 566 | 566 |  				if ((!$sourceTable instanceof Table || !$sourceTable->hasColumn($thing->getName())) && \strlen($thing->getName()) > 30) { | 
                                                        
| 567 | -					throw new \InvalidArgumentException('Column name "'  . $table->getName() . '"."' . $thing->getName() . '" is too long.'); | 
                                                        |
| 567 | +					throw new \InvalidArgumentException('Column name "'.$table->getName().'"."'.$thing->getName().'" is too long.'); | 
                                                        |
| 568 | 568 | }  | 
                                                        
| 569 | 569 | |
| 570 | 570 | if ($thing->getNotnull() && $thing->getDefault() === ''  | 
                                                        
| 571 | 571 |  					&& $sourceTable instanceof Table && !$sourceTable->hasColumn($thing->getName())) { | 
                                                        
| 572 | -					throw new \InvalidArgumentException('Column name "'  . $table->getName() . '"."' . $thing->getName() . '" is NotNull, but has empty string or null as default.'); | 
                                                        |
| 572 | +					throw new \InvalidArgumentException('Column name "'.$table->getName().'"."'.$thing->getName().'" is NotNull, but has empty string or null as default.'); | 
                                                        |
| 573 | 573 | }  | 
                                                        
| 574 | 574 | }  | 
                                                        
| 575 | 575 | |
| 576 | 576 |  			foreach ($table->getIndexes() as $thing) { | 
                                                        
| 577 | 577 |  				if ((!$sourceTable instanceof Table || !$sourceTable->hasIndex($thing->getName())) && \strlen($thing->getName()) > 30) { | 
                                                        
| 578 | -					throw new \InvalidArgumentException('Index name "'  . $table->getName() . '"."' . $thing->getName() . '" is too long.'); | 
                                                        |
| 578 | +					throw new \InvalidArgumentException('Index name "'.$table->getName().'"."'.$thing->getName().'" is too long.'); | 
                                                        |
| 579 | 579 | }  | 
                                                        
| 580 | 580 | }  | 
                                                        
| 581 | 581 | |
| 582 | 582 |  			foreach ($table->getForeignKeys() as $thing) { | 
                                                        
| 583 | 583 |  				if ((!$sourceTable instanceof Table || !$sourceTable->hasForeignKey($thing->getName())) && \strlen($thing->getName()) > 30) { | 
                                                        
| 584 | -					throw new \InvalidArgumentException('Foreign key name "'  . $table->getName() . '"."' . $thing->getName() . '" is too long.'); | 
                                                        |
| 584 | +					throw new \InvalidArgumentException('Foreign key name "'.$table->getName().'"."'.$thing->getName().'" is too long.'); | 
                                                        |
| 585 | 585 | }  | 
                                                        
| 586 | 586 | }  | 
                                                        
| 587 | 587 | |
@@ -591,32 +591,32 @@ discard block  | 
                                                    ||
| 591 | 591 | $isUsingDefaultName = $indexName === 'primary';  | 
                                                        
| 592 | 592 | |
| 593 | 593 |  				if ($this->connection->getDatabasePlatform() instanceof PostgreSqlPlatform) { | 
                                                        
| 594 | - $defaultName = $table->getName() . '_pkey';  | 
                                                        |
| 594 | + $defaultName = $table->getName().'_pkey';  | 
                                                        |
| 595 | 595 | $isUsingDefaultName = strtolower($defaultName) === $indexName;  | 
                                                        
| 596 | 596 | |
| 597 | 597 |  					if ($isUsingDefaultName) { | 
                                                        
| 598 | -						$sequenceName = $table->getName() . '_' . implode('_', $primaryKey->getColumns()) . '_seq'; | 
                                                        |
| 599 | -						$sequences = array_filter($sequences, function (Sequence $sequence) use ($sequenceName) { | 
                                                        |
| 598 | +						$sequenceName = $table->getName().'_'.implode('_', $primaryKey->getColumns()).'_seq'; | 
                                                        |
| 599 | +						$sequences = array_filter($sequences, function(Sequence $sequence) use ($sequenceName) { | 
                                                        |
| 600 | 600 | return $sequence->getName() !== $sequenceName;  | 
                                                        
| 601 | 601 | });  | 
                                                        
| 602 | 602 | }  | 
                                                        
| 603 | 603 |  				} elseif ($this->connection->getDatabasePlatform() instanceof OraclePlatform) { | 
                                                        
| 604 | - $defaultName = $table->getName() . '_seq';  | 
                                                        |
| 604 | + $defaultName = $table->getName().'_seq';  | 
                                                        |
| 605 | 605 | $isUsingDefaultName = strtolower($defaultName) === $indexName;  | 
                                                        
| 606 | 606 | }  | 
                                                        
| 607 | 607 | |
| 608 | 608 |  				if (!$isUsingDefaultName && \strlen($indexName) > 30) { | 
                                                        
| 609 | -					throw new \InvalidArgumentException('Primary index name  on "'  . $table->getName() . '" is too long.'); | 
                                                        |
| 609 | +					throw new \InvalidArgumentException('Primary index name  on "'.$table->getName().'" is too long.'); | 
                                                        |
| 610 | 610 | }  | 
                                                        
| 611 | 611 |  				if ($isUsingDefaultName && \strlen($table->getName()) - $prefixLength >= 23) { | 
                                                        
| 612 | -					throw new \InvalidArgumentException('Primary index name  on "'  . $table->getName() . '" is too long.'); | 
                                                        |
| 612 | +					throw new \InvalidArgumentException('Primary index name  on "'.$table->getName().'" is too long.'); | 
                                                        |
| 613 | 613 | }  | 
                                                        
| 614 | 614 | }  | 
                                                        
| 615 | 615 | }  | 
                                                        
| 616 | 616 | |
| 617 | 617 |  		foreach ($sequences as $sequence) { | 
                                                        
| 618 | 618 |  			if (!$sourceSchema->hasSequence($sequence->getName()) && \strlen($sequence->getName()) > 30) { | 
                                                        
| 619 | -				throw new \InvalidArgumentException('Sequence name "'  . $sequence->getName() . '" is too long.'); | 
                                                        |
| 619 | +				throw new \InvalidArgumentException('Sequence name "'.$sequence->getName().'" is too long.'); | 
                                                        |
| 620 | 620 | }  | 
                                                        
| 621 | 621 | }  | 
                                                        
| 622 | 622 | }  |