1
0

MigrationService.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com>
  4. * @copyright Copyright (c) 2017, ownCloud GmbH
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OC\DB;
  24. use Doctrine\DBAL\Platforms\OraclePlatform;
  25. use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
  26. use Doctrine\DBAL\Schema\Column;
  27. use Doctrine\DBAL\Schema\Index;
  28. use Doctrine\DBAL\Schema\Schema;
  29. use Doctrine\DBAL\Schema\SchemaException;
  30. use Doctrine\DBAL\Schema\Sequence;
  31. use OC\IntegrityCheck\Helpers\AppLocator;
  32. use OC\Migration\SimpleOutput;
  33. use OCP\AppFramework\App;
  34. use OCP\AppFramework\QueryException;
  35. use OCP\IDBConnection;
  36. use OCP\Migration\IMigrationStep;
  37. use OCP\Migration\IOutput;
  38. use Doctrine\DBAL\Types\Type;
  39. class MigrationService {
  40. /** @var boolean */
  41. private $migrationTableCreated;
  42. /** @var array */
  43. private $migrations;
  44. /** @var IOutput */
  45. private $output;
  46. /** @var Connection */
  47. private $connection;
  48. /** @var string */
  49. private $appName;
  50. /**
  51. * MigrationService constructor.
  52. *
  53. * @param $appName
  54. * @param IDBConnection $connection
  55. * @param AppLocator $appLocator
  56. * @param IOutput|null $output
  57. * @throws \Exception
  58. */
  59. public function __construct($appName, IDBConnection $connection, IOutput $output = null, AppLocator $appLocator = null) {
  60. $this->appName = $appName;
  61. $this->connection = $connection;
  62. $this->output = $output;
  63. if (null === $this->output) {
  64. $this->output = new SimpleOutput(\OC::$server->getLogger(), $appName);
  65. }
  66. if ($appName === 'core') {
  67. $this->migrationsPath = \OC::$SERVERROOT . '/core/Migrations';
  68. $this->migrationsNamespace = 'OC\\Core\\Migrations';
  69. } else {
  70. if (null === $appLocator) {
  71. $appLocator = new AppLocator();
  72. }
  73. $appPath = $appLocator->getAppPath($appName);
  74. $namespace = App::buildAppNamespace($appName);
  75. $this->migrationsPath = "$appPath/lib/Migration";
  76. $this->migrationsNamespace = $namespace . '\\Migration';
  77. }
  78. }
  79. /**
  80. * Returns the name of the app for which this migration is executed
  81. *
  82. * @return string
  83. */
  84. public function getApp() {
  85. return $this->appName;
  86. }
  87. /**
  88. * @return bool
  89. * @codeCoverageIgnore - this will implicitly tested on installation
  90. */
  91. private function createMigrationTable() {
  92. if ($this->migrationTableCreated) {
  93. return false;
  94. }
  95. $schema = new SchemaWrapper($this->connection);
  96. /**
  97. * We drop the table when it has different columns or the definition does not
  98. * match. E.g. ownCloud uses a length of 177 for app and 14 for version.
  99. */
  100. try {
  101. $table = $schema->getTable('migrations');
  102. $columns = $table->getColumns();
  103. if (count($columns) === 2) {
  104. try {
  105. $column = $table->getColumn('app');
  106. $schemaMismatch = $column->getLength() !== 255;
  107. if (!$schemaMismatch) {
  108. $column = $table->getColumn('version');
  109. $schemaMismatch = $column->getLength() !== 255;
  110. }
  111. } catch (SchemaException $e) {
  112. // One of the columns is missing
  113. $schemaMismatch = true;
  114. }
  115. if (!$schemaMismatch) {
  116. // Table exists and schema matches: return back!
  117. $this->migrationTableCreated = true;
  118. return false;
  119. }
  120. }
  121. // Drop the table, when it didn't match our expectations.
  122. $this->connection->dropTable('migrations');
  123. // Recreate the schema after the table was dropped.
  124. $schema = new SchemaWrapper($this->connection);
  125. } catch (SchemaException $e) {
  126. // Table not found, no need to panic, we will create it.
  127. }
  128. $table = $schema->createTable('migrations');
  129. $table->addColumn('app', Type::STRING, ['length' => 255]);
  130. $table->addColumn('version', Type::STRING, ['length' => 255]);
  131. $table->setPrimaryKey(['app', 'version']);
  132. $this->connection->migrateToSchema($schema->getWrappedSchema());
  133. $this->migrationTableCreated = true;
  134. return true;
  135. }
  136. /**
  137. * Returns all versions which have already been applied
  138. *
  139. * @return string[]
  140. * @codeCoverageIgnore - no need to test this
  141. */
  142. public function getMigratedVersions() {
  143. $this->createMigrationTable();
  144. $qb = $this->connection->getQueryBuilder();
  145. $qb->select('version')
  146. ->from('migrations')
  147. ->where($qb->expr()->eq('app', $qb->createNamedParameter($this->getApp())))
  148. ->orderBy('version');
  149. $result = $qb->execute();
  150. $rows = $result->fetchAll(\PDO::FETCH_COLUMN);
  151. $result->closeCursor();
  152. return $rows;
  153. }
  154. /**
  155. * Returns all versions which are available in the migration folder
  156. *
  157. * @return array
  158. */
  159. public function getAvailableVersions() {
  160. $this->ensureMigrationsAreLoaded();
  161. return array_map('strval', array_keys($this->migrations));
  162. }
  163. protected function findMigrations() {
  164. $directory = realpath($this->migrationsPath);
  165. if ($directory === false || !file_exists($directory) || !is_dir($directory)) {
  166. return [];
  167. }
  168. $iterator = new \RegexIterator(
  169. new \RecursiveIteratorIterator(
  170. new \RecursiveDirectoryIterator($directory, \FilesystemIterator::SKIP_DOTS),
  171. \RecursiveIteratorIterator::LEAVES_ONLY
  172. ),
  173. '#^.+\\/Version[^\\/]{1,255}\\.php$#i',
  174. \RegexIterator::GET_MATCH);
  175. $files = array_keys(iterator_to_array($iterator));
  176. uasort($files, function ($a, $b) {
  177. preg_match('/^Version(\d+)Date(\d+)\\.php$/', basename($a), $matchA);
  178. preg_match('/^Version(\d+)Date(\d+)\\.php$/', basename($b), $matchB);
  179. if (!empty($matchA) && !empty($matchB)) {
  180. if ($matchA[1] !== $matchB[1]) {
  181. return ($matchA[1] < $matchB[1]) ? -1 : 1;
  182. }
  183. return ($matchA[2] < $matchB[2]) ? -1 : 1;
  184. }
  185. return (basename($a) < basename($b)) ? -1 : 1;
  186. });
  187. $migrations = [];
  188. foreach ($files as $file) {
  189. $className = basename($file, '.php');
  190. $version = (string) substr($className, 7);
  191. if ($version === '0') {
  192. throw new \InvalidArgumentException(
  193. "Cannot load a migrations with the name '$version' because it is a reserved number"
  194. );
  195. }
  196. $migrations[$version] = sprintf('%s\\%s', $this->migrationsNamespace, $className);
  197. }
  198. return $migrations;
  199. }
  200. /**
  201. * @param string $to
  202. * @return string[]
  203. */
  204. private function getMigrationsToExecute($to) {
  205. $knownMigrations = $this->getMigratedVersions();
  206. $availableMigrations = $this->getAvailableVersions();
  207. $toBeExecuted = [];
  208. foreach ($availableMigrations as $v) {
  209. if ($to !== 'latest' && $v > $to) {
  210. continue;
  211. }
  212. if ($this->shallBeExecuted($v, $knownMigrations)) {
  213. $toBeExecuted[] = $v;
  214. }
  215. }
  216. return $toBeExecuted;
  217. }
  218. /**
  219. * @param string $m
  220. * @param string[] $knownMigrations
  221. * @return bool
  222. */
  223. private function shallBeExecuted($m, $knownMigrations) {
  224. if (in_array($m, $knownMigrations)) {
  225. return false;
  226. }
  227. return true;
  228. }
  229. /**
  230. * @param string $version
  231. */
  232. private function markAsExecuted($version) {
  233. $this->connection->insertIfNotExist('*PREFIX*migrations', [
  234. 'app' => $this->appName,
  235. 'version' => $version
  236. ]);
  237. }
  238. /**
  239. * Returns the name of the table which holds the already applied versions
  240. *
  241. * @return string
  242. */
  243. public function getMigrationsTableName() {
  244. return $this->connection->getPrefix() . 'migrations';
  245. }
  246. /**
  247. * Returns the namespace of the version classes
  248. *
  249. * @return string
  250. */
  251. public function getMigrationsNamespace() {
  252. return $this->migrationsNamespace;
  253. }
  254. /**
  255. * Returns the directory which holds the versions
  256. *
  257. * @return string
  258. */
  259. public function getMigrationsDirectory() {
  260. return $this->migrationsPath;
  261. }
  262. /**
  263. * Return the explicit version for the aliases; current, next, prev, latest
  264. *
  265. * @param string $alias
  266. * @return mixed|null|string
  267. */
  268. public function getMigration($alias) {
  269. switch($alias) {
  270. case 'current':
  271. return $this->getCurrentVersion();
  272. case 'next':
  273. return $this->getRelativeVersion($this->getCurrentVersion(), 1);
  274. case 'prev':
  275. return $this->getRelativeVersion($this->getCurrentVersion(), -1);
  276. case 'latest':
  277. $this->ensureMigrationsAreLoaded();
  278. $migrations = $this->getAvailableVersions();
  279. return @end($migrations);
  280. }
  281. return '0';
  282. }
  283. /**
  284. * @param string $version
  285. * @param int $delta
  286. * @return null|string
  287. */
  288. private function getRelativeVersion($version, $delta) {
  289. $this->ensureMigrationsAreLoaded();
  290. $versions = $this->getAvailableVersions();
  291. array_unshift($versions, 0);
  292. $offset = array_search($version, $versions, true);
  293. if ($offset === false || !isset($versions[$offset + $delta])) {
  294. // Unknown version or delta out of bounds.
  295. return null;
  296. }
  297. return (string) $versions[$offset + $delta];
  298. }
  299. /**
  300. * @return string
  301. */
  302. private function getCurrentVersion() {
  303. $m = $this->getMigratedVersions();
  304. if (count($m) === 0) {
  305. return '0';
  306. }
  307. $migrations = array_values($m);
  308. return @end($migrations);
  309. }
  310. /**
  311. * @param string $version
  312. * @return string
  313. * @throws \InvalidArgumentException
  314. */
  315. private function getClass($version) {
  316. $this->ensureMigrationsAreLoaded();
  317. if (isset($this->migrations[$version])) {
  318. return $this->migrations[$version];
  319. }
  320. throw new \InvalidArgumentException("Version $version is unknown.");
  321. }
  322. /**
  323. * Allows to set an IOutput implementation which is used for logging progress and messages
  324. *
  325. * @param IOutput $output
  326. */
  327. public function setOutput(IOutput $output) {
  328. $this->output = $output;
  329. }
  330. /**
  331. * Applies all not yet applied versions up to $to
  332. *
  333. * @param string $to
  334. * @param bool $schemaOnly
  335. * @throws \InvalidArgumentException
  336. */
  337. public function migrate($to = 'latest', $schemaOnly = false) {
  338. // read known migrations
  339. $toBeExecuted = $this->getMigrationsToExecute($to);
  340. foreach ($toBeExecuted as $version) {
  341. $this->executeStep($version, $schemaOnly);
  342. }
  343. }
  344. /**
  345. * Get the human readable descriptions for the migration steps to run
  346. *
  347. * @param string $to
  348. * @return string[] [$name => $description]
  349. */
  350. public function describeMigrationStep($to = 'latest') {
  351. $toBeExecuted = $this->getMigrationsToExecute($to);
  352. $description = [];
  353. foreach ($toBeExecuted as $version) {
  354. $migration = $this->createInstance($version);
  355. if ($migration->name()) {
  356. $description[$migration->name()] = $migration->description();
  357. }
  358. }
  359. return $description;
  360. }
  361. /**
  362. * @param string $version
  363. * @return IMigrationStep
  364. * @throws \InvalidArgumentException
  365. */
  366. protected function createInstance($version) {
  367. $class = $this->getClass($version);
  368. try {
  369. $s = \OC::$server->query($class);
  370. if (!$s instanceof IMigrationStep) {
  371. throw new \InvalidArgumentException('Not a valid migration');
  372. }
  373. } catch (QueryException $e) {
  374. if (class_exists($class)) {
  375. $s = new $class();
  376. } else {
  377. throw new \InvalidArgumentException("Migration step '$class' is unknown");
  378. }
  379. }
  380. return $s;
  381. }
  382. /**
  383. * Executes one explicit version
  384. *
  385. * @param string $version
  386. * @param bool $schemaOnly
  387. * @throws \InvalidArgumentException
  388. */
  389. public function executeStep($version, $schemaOnly = false) {
  390. $instance = $this->createInstance($version);
  391. if (!$schemaOnly) {
  392. $instance->preSchemaChange($this->output, function() {
  393. return new SchemaWrapper($this->connection);
  394. }, ['tablePrefix' => $this->connection->getPrefix()]);
  395. }
  396. $toSchema = $instance->changeSchema($this->output, function() {
  397. return new SchemaWrapper($this->connection);
  398. }, ['tablePrefix' => $this->connection->getPrefix()]);
  399. if ($toSchema instanceof SchemaWrapper) {
  400. $targetSchema = $toSchema->getWrappedSchema();
  401. // TODO re-enable once stable14 is branched of: https://github.com/nextcloud/server/issues/10518
  402. // $this->ensureOracleIdentifierLengthLimit($targetSchema, strlen($this->connection->getPrefix()));
  403. $this->connection->migrateToSchema($targetSchema);
  404. $toSchema->performDropTableCalls();
  405. }
  406. if (!$schemaOnly) {
  407. $instance->postSchemaChange($this->output, function() {
  408. return new SchemaWrapper($this->connection);
  409. }, ['tablePrefix' => $this->connection->getPrefix()]);
  410. }
  411. $this->markAsExecuted($version);
  412. }
  413. public function ensureOracleIdentifierLengthLimit(Schema $schema, int $prefixLength) {
  414. $sequences = $schema->getSequences();
  415. foreach ($schema->getTables() as $table) {
  416. if (\strlen($table->getName()) - $prefixLength > 27) {
  417. throw new \InvalidArgumentException('Table name "' . $table->getName() . '" is too long.');
  418. }
  419. foreach ($table->getColumns() as $thing) {
  420. if (\strlen($thing->getName()) - $prefixLength > 27) {
  421. throw new \InvalidArgumentException('Column name "' . $table->getName() . '"."' . $thing->getName() . '" is too long.');
  422. }
  423. }
  424. foreach ($table->getIndexes() as $thing) {
  425. if (\strlen($thing->getName()) - $prefixLength > 27) {
  426. throw new \InvalidArgumentException('Index name "' . $table->getName() . '"."' . $thing->getName() . '" is too long.');
  427. }
  428. }
  429. foreach ($table->getForeignKeys() as $thing) {
  430. if (\strlen($thing->getName()) - $prefixLength > 27) {
  431. throw new \InvalidArgumentException('Foreign key name "' . $table->getName() . '"."' . $thing->getName() . '" is too long.');
  432. }
  433. }
  434. $primaryKey = $table->getPrimaryKey();
  435. if ($primaryKey instanceof Index) {
  436. $indexName = strtolower($primaryKey->getName());
  437. $isUsingDefaultName = $indexName === 'primary';
  438. if ($this->connection->getDatabasePlatform() instanceof PostgreSqlPlatform) {
  439. $defaultName = $table->getName() . '_pkey';
  440. $isUsingDefaultName = strtolower($defaultName) === $indexName;
  441. if ($isUsingDefaultName) {
  442. $sequenceName = $table->getName() . '_' . implode('_', $primaryKey->getColumns()) . '_seq';
  443. $sequences = array_filter($sequences, function(Sequence $sequence) use ($sequenceName) {
  444. return $sequence->getName() !== $sequenceName;
  445. });
  446. }
  447. } else if ($this->connection->getDatabasePlatform() instanceof OraclePlatform) {
  448. $defaultName = $table->getName() . '_seq';
  449. $isUsingDefaultName = strtolower($defaultName) === $indexName;
  450. }
  451. if (!$isUsingDefaultName && \strlen($indexName) - $prefixLength > 27) {
  452. throw new \InvalidArgumentException('Primary index name on "' . $table->getName() . '" is too long.');
  453. }
  454. if ($isUsingDefaultName && \strlen($table->getName()) - $prefixLength > 23) {
  455. throw new \InvalidArgumentException('Primary index name on "' . $table->getName() . '" is too long.');
  456. }
  457. }
  458. }
  459. foreach ($sequences as $sequence) {
  460. if (\strlen($sequence->getName()) - $prefixLength > 27) {
  461. throw new \InvalidArgumentException('Sequence name "' . $sequence->getName() . '" is too long.');
  462. }
  463. }
  464. }
  465. private function ensureMigrationsAreLoaded() {
  466. if (empty($this->migrations)) {
  467. $this->migrations = $this->findMigrations();
  468. }
  469. }
  470. }