GenerateCommand.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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\Core\Command\Db\Migrations;
  24. use OC\DB\MigrationService;
  25. use OC\Migration\ConsoleOutput;
  26. use OCP\App\IAppManager;
  27. use OCP\IDBConnection;
  28. use Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionAwareInterface;
  29. use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
  30. use Symfony\Component\Console\Command\Command;
  31. use Symfony\Component\Console\Exception\RuntimeException;
  32. use Symfony\Component\Console\Input\InputArgument;
  33. use Symfony\Component\Console\Input\InputInterface;
  34. use Symfony\Component\Console\Output\OutputInterface;
  35. class GenerateCommand extends Command implements CompletionAwareInterface {
  36. protected static $_templateSimple =
  37. '<?php
  38. declare(strict_types=1);
  39. namespace {{namespace}};
  40. use Closure;
  41. use OCP\DB\ISchemaWrapper;
  42. use OCP\Migration\SimpleMigrationStep;
  43. use OCP\Migration\IOutput;
  44. /**
  45. * Auto-generated migration step: Please modify to your needs!
  46. */
  47. class {{classname}} extends SimpleMigrationStep {
  48. /**
  49. * @param IOutput $output
  50. * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
  51. * @param array $options
  52. */
  53. public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options) {
  54. }
  55. /**
  56. * @param IOutput $output
  57. * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
  58. * @param array $options
  59. * @return null|ISchemaWrapper
  60. */
  61. public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
  62. {{schemabody}}
  63. }
  64. /**
  65. * @param IOutput $output
  66. * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
  67. * @param array $options
  68. */
  69. public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) {
  70. }
  71. }
  72. ';
  73. /** @var IDBConnection */
  74. protected $connection;
  75. /** @var IAppManager */
  76. protected $appManager;
  77. /**
  78. * @param IDBConnection $connection
  79. * @param IAppManager $appManager
  80. */
  81. public function __construct(IDBConnection $connection, IAppManager $appManager) {
  82. $this->connection = $connection;
  83. $this->appManager = $appManager;
  84. parent::__construct();
  85. }
  86. protected function configure() {
  87. $this
  88. ->setName('migrations:generate')
  89. ->addArgument('app', InputArgument::REQUIRED, 'Name of the app this migration command shall work on')
  90. ->addArgument('version', InputArgument::REQUIRED, 'Major version of this app, to allow versions on parallel development branches')
  91. ;
  92. parent::configure();
  93. }
  94. public function execute(InputInterface $input, OutputInterface $output) {
  95. $appName = $input->getArgument('app');
  96. $version = $input->getArgument('version');
  97. if (!preg_match('/^\d{1,16}$/',$version)) {
  98. $output->writeln('<error>The given version is invalid. Only 0-9 are allowed (max. 16 digits)</error>');
  99. return 1;
  100. }
  101. $ms = new MigrationService($appName, $this->connection, new ConsoleOutput($output));
  102. $date = date('YmdHis');
  103. $path = $this->generateMigration($ms, 'Version' . $version . 'Date' . $date);
  104. $output->writeln("New migration class has been generated to <info>$path</info>");
  105. return 0;
  106. }
  107. /**
  108. * @param string $optionName
  109. * @param CompletionContext $context
  110. * @return string[]
  111. */
  112. public function completeOptionValues($optionName, CompletionContext $context) {
  113. return [];
  114. }
  115. /**
  116. * @param string $argumentName
  117. * @param CompletionContext $context
  118. * @return string[]
  119. */
  120. public function completeArgumentValues($argumentName, CompletionContext $context) {
  121. if ($argumentName === 'app') {
  122. $allApps = \OC_App::getAllApps();
  123. return array_diff($allApps, \OC_App::getEnabledApps(true, true));
  124. }
  125. if ($argumentName === 'version') {
  126. $appName = $context->getWordAtIndex($context->getWordIndex() - 1);
  127. $version = explode('.', $this->appManager->getAppVersion($appName));
  128. return [$version[0] . sprintf('%1$03d', $version[1])];
  129. }
  130. return [];
  131. }
  132. /**
  133. * @param MigrationService $ms
  134. * @param string $className
  135. * @param string $schemaBody
  136. * @return string
  137. */
  138. protected function generateMigration(MigrationService $ms, $className, $schemaBody = '') {
  139. if ($schemaBody === '') {
  140. $schemaBody = "\t\t" . 'return null;';
  141. }
  142. $placeHolders = [
  143. '{{namespace}}',
  144. '{{classname}}',
  145. '{{schemabody}}',
  146. ];
  147. $replacements = [
  148. $ms->getMigrationsNamespace(),
  149. $className,
  150. $schemaBody,
  151. ];
  152. $code = str_replace($placeHolders, $replacements, self::$_templateSimple);
  153. $dir = $ms->getMigrationsDirectory();
  154. $this->ensureMigrationDirExists($dir);
  155. $path = $dir . '/' . $className . '.php';
  156. if (file_put_contents($path, $code) === false) {
  157. throw new RuntimeException('Failed to generate new migration step.');
  158. }
  159. return $path;
  160. }
  161. protected function ensureMigrationDirExists($directory) {
  162. if (file_exists($directory) && is_dir($directory)) {
  163. return;
  164. }
  165. if (file_exists($directory)) {
  166. throw new \RuntimeException("Could not create folder \"$directory\"");
  167. }
  168. $this->ensureMigrationDirExists(dirname($directory));
  169. if (!@mkdir($directory) && !is_dir($directory)) {
  170. throw new \RuntimeException("Could not create folder \"$directory\"");
  171. }
  172. }
  173. }