You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GenerateCommand.php 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com>
  4. * @copyright Copyright (c) 2017, ownCloud GmbH
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OC\Core\Command\Db\Migrations;
  26. use OC\DB\Connection;
  27. use OC\DB\MigrationService;
  28. use OC\Migration\ConsoleOutput;
  29. use OCP\App\IAppManager;
  30. use Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionAwareInterface;
  31. use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
  32. use Symfony\Component\Console\Command\Command;
  33. use Symfony\Component\Console\Exception\RuntimeException;
  34. use Symfony\Component\Console\Input\InputArgument;
  35. use Symfony\Component\Console\Input\InputInterface;
  36. use Symfony\Component\Console\Output\OutputInterface;
  37. class GenerateCommand extends Command implements CompletionAwareInterface {
  38. protected static $_templateSimple =
  39. '<?php
  40. declare(strict_types=1);
  41. namespace {{namespace}};
  42. use Closure;
  43. use OCP\DB\ISchemaWrapper;
  44. use OCP\Migration\IOutput;
  45. use OCP\Migration\SimpleMigrationStep;
  46. /**
  47. * Auto-generated migration step: Please modify to your needs!
  48. */
  49. class {{classname}} extends SimpleMigrationStep {
  50. /**
  51. * @param IOutput $output
  52. * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
  53. * @param array $options
  54. */
  55. public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
  56. }
  57. /**
  58. * @param IOutput $output
  59. * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
  60. * @param array $options
  61. * @return null|ISchemaWrapper
  62. */
  63. public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
  64. {{schemabody}}
  65. }
  66. /**
  67. * @param IOutput $output
  68. * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
  69. * @param array $options
  70. */
  71. public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
  72. }
  73. }
  74. ';
  75. /** @var Connection */
  76. protected $connection;
  77. /** @var IAppManager */
  78. protected $appManager;
  79. /**
  80. * @param Connection $connection
  81. * @param IAppManager $appManager
  82. */
  83. public function __construct(Connection $connection, IAppManager $appManager) {
  84. $this->connection = $connection;
  85. $this->appManager = $appManager;
  86. parent::__construct();
  87. }
  88. protected function configure() {
  89. $this
  90. ->setName('migrations:generate')
  91. ->addArgument('app', InputArgument::REQUIRED, 'Name of the app this migration command shall work on')
  92. ->addArgument('version', InputArgument::REQUIRED, 'Major version of this app, to allow versions on parallel development branches')
  93. ;
  94. parent::configure();
  95. }
  96. public function execute(InputInterface $input, OutputInterface $output): int {
  97. $appName = $input->getArgument('app');
  98. $version = $input->getArgument('version');
  99. if (!preg_match('/^\d{1,16}$/', $version)) {
  100. $output->writeln('<error>The given version is invalid. Only 0-9 are allowed (max. 16 digits)</error>');
  101. return 1;
  102. }
  103. $ms = new MigrationService($appName, $this->connection, new ConsoleOutput($output));
  104. $date = date('YmdHis');
  105. $path = $this->generateMigration($ms, 'Version' . $version . 'Date' . $date);
  106. $output->writeln("New migration class has been generated to <info>$path</info>");
  107. return 0;
  108. }
  109. /**
  110. * @param string $optionName
  111. * @param CompletionContext $context
  112. * @return string[]
  113. */
  114. public function completeOptionValues($optionName, CompletionContext $context) {
  115. return [];
  116. }
  117. /**
  118. * @param string $argumentName
  119. * @param CompletionContext $context
  120. * @return string[]
  121. */
  122. public function completeArgumentValues($argumentName, CompletionContext $context) {
  123. if ($argumentName === 'app') {
  124. $allApps = \OC_App::getAllApps();
  125. return array_diff($allApps, \OC_App::getEnabledApps(true, true));
  126. }
  127. if ($argumentName === 'version') {
  128. $appName = $context->getWordAtIndex($context->getWordIndex() - 1);
  129. $version = explode('.', $this->appManager->getAppVersion($appName));
  130. return [$version[0] . sprintf('%1$03d', $version[1])];
  131. }
  132. return [];
  133. }
  134. /**
  135. * @param MigrationService $ms
  136. * @param string $className
  137. * @param string $schemaBody
  138. * @return string
  139. */
  140. protected function generateMigration(MigrationService $ms, $className, $schemaBody = '') {
  141. if ($schemaBody === '') {
  142. $schemaBody = "\t\t" . 'return null;';
  143. }
  144. $placeHolders = [
  145. '{{namespace}}',
  146. '{{classname}}',
  147. '{{schemabody}}',
  148. ];
  149. $replacements = [
  150. $ms->getMigrationsNamespace(),
  151. $className,
  152. $schemaBody,
  153. ];
  154. $code = str_replace($placeHolders, $replacements, self::$_templateSimple);
  155. $dir = $ms->getMigrationsDirectory();
  156. $this->ensureMigrationDirExists($dir);
  157. $path = $dir . '/' . $className . '.php';
  158. if (file_put_contents($path, $code) === false) {
  159. throw new RuntimeException('Failed to generate new migration step.');
  160. }
  161. return $path;
  162. }
  163. protected function ensureMigrationDirExists($directory) {
  164. if (file_exists($directory) && is_dir($directory)) {
  165. return;
  166. }
  167. if (file_exists($directory)) {
  168. throw new \RuntimeException("Could not create folder \"$directory\"");
  169. }
  170. $this->ensureMigrationDirExists(dirname($directory));
  171. if (!@mkdir($directory) && !is_dir($directory)) {
  172. throw new \RuntimeException("Could not create folder \"$directory\"");
  173. }
  174. }
  175. }