@@ -36,170 +36,170 @@ |
||
| 36 | 36 | |
| 37 | 37 | class GenerateFromSchemaFileCommand extends GenerateCommand { |
| 38 | 38 | |
| 39 | - /** @var IConfig */ |
|
| 40 | - protected $config; |
|
| 39 | + /** @var IConfig */ |
|
| 40 | + protected $config; |
|
| 41 | 41 | |
| 42 | - /** @var IAppManager */ |
|
| 43 | - protected $appManager; |
|
| 42 | + /** @var IAppManager */ |
|
| 43 | + protected $appManager; |
|
| 44 | 44 | |
| 45 | - public function __construct(IConfig $config, IAppManager $appManager, IDBConnection $connection) { |
|
| 46 | - parent::__construct($connection); |
|
| 47 | - $this->config = $config; |
|
| 48 | - $this->appManager = $appManager; |
|
| 49 | - } |
|
| 45 | + public function __construct(IConfig $config, IAppManager $appManager, IDBConnection $connection) { |
|
| 46 | + parent::__construct($connection); |
|
| 47 | + $this->config = $config; |
|
| 48 | + $this->appManager = $appManager; |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | 51 | |
| 52 | - protected function configure() { |
|
| 53 | - parent::configure(); |
|
| 52 | + protected function configure() { |
|
| 53 | + parent::configure(); |
|
| 54 | 54 | |
| 55 | - $this->setName('migrations:generate-from-schema'); |
|
| 56 | - } |
|
| 55 | + $this->setName('migrations:generate-from-schema'); |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - public function execute(InputInterface $input, OutputInterface $output) { |
|
| 59 | - $appName = $input->getArgument('app'); |
|
| 60 | - $version = $input->getArgument('version'); |
|
| 58 | + public function execute(InputInterface $input, OutputInterface $output) { |
|
| 59 | + $appName = $input->getArgument('app'); |
|
| 60 | + $version = $input->getArgument('version'); |
|
| 61 | 61 | |
| 62 | - if (!preg_match('/^\d{1,16}$/',$version)) { |
|
| 63 | - $output->writeln('<error>The given version is invalid. Only 0-9 are allowed (max. 16 digits)</error>'); |
|
| 64 | - return 1; |
|
| 65 | - } |
|
| 62 | + if (!preg_match('/^\d{1,16}$/',$version)) { |
|
| 63 | + $output->writeln('<error>The given version is invalid. Only 0-9 are allowed (max. 16 digits)</error>'); |
|
| 64 | + return 1; |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - $schemaFile = $this->appManager->getAppPath($appName) . '/appinfo/database.xml'; |
|
| 68 | - if (!file_exists($schemaFile)) { |
|
| 69 | - $output->writeln('<error>App ' . $appName . ' does not have a database.xml file</error>'); |
|
| 70 | - return 2; |
|
| 71 | - } |
|
| 67 | + $schemaFile = $this->appManager->getAppPath($appName) . '/appinfo/database.xml'; |
|
| 68 | + if (!file_exists($schemaFile)) { |
|
| 69 | + $output->writeln('<error>App ' . $appName . ' does not have a database.xml file</error>'); |
|
| 70 | + return 2; |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - $reader = new MDB2SchemaReader($this->config, $this->connection->getDatabasePlatform()); |
|
| 74 | - $schema = new Schema(); |
|
| 75 | - $reader->loadSchemaFromFile($schemaFile, $schema); |
|
| 73 | + $reader = new MDB2SchemaReader($this->config, $this->connection->getDatabasePlatform()); |
|
| 74 | + $schema = new Schema(); |
|
| 75 | + $reader->loadSchemaFromFile($schemaFile, $schema); |
|
| 76 | 76 | |
| 77 | - $schemaBody = $this->schemaToMigration($schema); |
|
| 77 | + $schemaBody = $this->schemaToMigration($schema); |
|
| 78 | 78 | |
| 79 | - $ms = new MigrationService($appName, $this->connection, new ConsoleOutput($output)); |
|
| 79 | + $ms = new MigrationService($appName, $this->connection, new ConsoleOutput($output)); |
|
| 80 | 80 | |
| 81 | - $date = date('YmdHis'); |
|
| 82 | - $path = $this->generateMigration($ms, 'Version' . $version . 'Date' . $date, $schemaBody); |
|
| 81 | + $date = date('YmdHis'); |
|
| 82 | + $path = $this->generateMigration($ms, 'Version' . $version . 'Date' . $date, $schemaBody); |
|
| 83 | 83 | |
| 84 | - $output->writeln("New migration class has been generated to <info>$path</info>"); |
|
| 85 | - return 0; |
|
| 86 | - } |
|
| 84 | + $output->writeln("New migration class has been generated to <info>$path</info>"); |
|
| 85 | + return 0; |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | - /** |
|
| 89 | - * @param Schema $schema |
|
| 90 | - * @return string |
|
| 91 | - */ |
|
| 92 | - protected function schemaToMigration(Schema $schema) { |
|
| 93 | - $content = <<<'EOT' |
|
| 88 | + /** |
|
| 89 | + * @param Schema $schema |
|
| 90 | + * @return string |
|
| 91 | + */ |
|
| 92 | + protected function schemaToMigration(Schema $schema) { |
|
| 93 | + $content = <<<'EOT' |
|
| 94 | 94 | /** @var SchemaWrapper $schema */ |
| 95 | 95 | $schema = $schemaClosure(); |
| 96 | 96 | |
| 97 | 97 | EOT; |
| 98 | 98 | |
| 99 | - foreach ($schema->getTables() as $table) { |
|
| 100 | - $content .= str_replace('{{table-name}}', substr($table->getName(), 3), <<<'EOT' |
|
| 99 | + foreach ($schema->getTables() as $table) { |
|
| 100 | + $content .= str_replace('{{table-name}}', substr($table->getName(), 3), <<<'EOT' |
|
| 101 | 101 | |
| 102 | 102 | if (!$schema->hasTable('{{table-name}}')) { |
| 103 | 103 | $table = $schema->createTable('{{table-name}}'); |
| 104 | 104 | |
| 105 | 105 | EOT |
| 106 | - ); |
|
| 106 | + ); |
|
| 107 | 107 | |
| 108 | - foreach ($table->getColumns() as $column) { |
|
| 109 | - $content .= str_replace(['{{name}}', '{{type}}'], [$column->getName(), $column->getType()->getName()], <<<'EOT' |
|
| 108 | + foreach ($table->getColumns() as $column) { |
|
| 109 | + $content .= str_replace(['{{name}}', '{{type}}'], [$column->getName(), $column->getType()->getName()], <<<'EOT' |
|
| 110 | 110 | $table->addColumn('{{name}}', '{{type}}', [ |
| 111 | 111 | |
| 112 | 112 | EOT |
| 113 | - ); |
|
| 114 | - if ($column->getAutoincrement()) { |
|
| 115 | - $content .= <<<'EOT' |
|
| 113 | + ); |
|
| 114 | + if ($column->getAutoincrement()) { |
|
| 115 | + $content .= <<<'EOT' |
|
| 116 | 116 | 'autoincrement' => true, |
| 117 | 117 | |
| 118 | 118 | EOT; |
| 119 | - } |
|
| 120 | - $content .= str_replace('{{notnull}}', $column->getNotnull() ? 'true' : 'false', <<<'EOT' |
|
| 119 | + } |
|
| 120 | + $content .= str_replace('{{notnull}}', $column->getNotnull() ? 'true' : 'false', <<<'EOT' |
|
| 121 | 121 | 'notnull' => {{notnull}}, |
| 122 | 122 | |
| 123 | 123 | EOT |
| 124 | - ); |
|
| 125 | - if ($column->getLength() !== null) { |
|
| 126 | - $content .= str_replace('{{length}}', $column->getLength(), <<<'EOT' |
|
| 124 | + ); |
|
| 125 | + if ($column->getLength() !== null) { |
|
| 126 | + $content .= str_replace('{{length}}', $column->getLength(), <<<'EOT' |
|
| 127 | 127 | 'length' => {{length}}, |
| 128 | 128 | |
| 129 | 129 | EOT |
| 130 | - ); |
|
| 131 | - } |
|
| 132 | - $default = $column->getDefault(); |
|
| 133 | - if ($default !== null) { |
|
| 134 | - $default = is_numeric($default) ? $default : "'$default'"; |
|
| 135 | - $content .= str_replace('{{default}}', $default, <<<'EOT' |
|
| 130 | + ); |
|
| 131 | + } |
|
| 132 | + $default = $column->getDefault(); |
|
| 133 | + if ($default !== null) { |
|
| 134 | + $default = is_numeric($default) ? $default : "'$default'"; |
|
| 135 | + $content .= str_replace('{{default}}', $default, <<<'EOT' |
|
| 136 | 136 | 'default' => {{default}}, |
| 137 | 137 | |
| 138 | 138 | EOT |
| 139 | - ); |
|
| 140 | - } |
|
| 141 | - if ($column->getUnsigned()) { |
|
| 142 | - $content .= <<<'EOT' |
|
| 139 | + ); |
|
| 140 | + } |
|
| 141 | + if ($column->getUnsigned()) { |
|
| 142 | + $content .= <<<'EOT' |
|
| 143 | 143 | 'unsigned' => true, |
| 144 | 144 | |
| 145 | 145 | EOT; |
| 146 | - } |
|
| 146 | + } |
|
| 147 | 147 | |
| 148 | - $content .= <<<'EOT' |
|
| 148 | + $content .= <<<'EOT' |
|
| 149 | 149 | ]); |
| 150 | 150 | |
| 151 | 151 | EOT; |
| 152 | - } |
|
| 152 | + } |
|
| 153 | 153 | |
| 154 | - $content .= <<<'EOT' |
|
| 154 | + $content .= <<<'EOT' |
|
| 155 | 155 | |
| 156 | 156 | EOT; |
| 157 | 157 | |
| 158 | - $primaryKey = $table->getPrimaryKey(); |
|
| 159 | - if ($primaryKey !== null) { |
|
| 160 | - $content .= str_replace('{{columns}}', implode('\', \'', $primaryKey->getUnquotedColumns()), <<<'EOT' |
|
| 158 | + $primaryKey = $table->getPrimaryKey(); |
|
| 159 | + if ($primaryKey !== null) { |
|
| 160 | + $content .= str_replace('{{columns}}', implode('\', \'', $primaryKey->getUnquotedColumns()), <<<'EOT' |
|
| 161 | 161 | $table->setPrimaryKey(['{{columns}}']); |
| 162 | 162 | |
| 163 | 163 | EOT |
| 164 | - ); |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - foreach ($table->getIndexes() as $index) { |
|
| 168 | - if ($index->isPrimary()) { |
|
| 169 | - continue; |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - if ($index->isUnique()) { |
|
| 173 | - $content .= str_replace( |
|
| 174 | - ['{{columns}}', '{{name}}'], |
|
| 175 | - [implode('\', \'', $index->getUnquotedColumns()), $index->getName()], |
|
| 176 | - <<<'EOT' |
|
| 164 | + ); |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + foreach ($table->getIndexes() as $index) { |
|
| 168 | + if ($index->isPrimary()) { |
|
| 169 | + continue; |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + if ($index->isUnique()) { |
|
| 173 | + $content .= str_replace( |
|
| 174 | + ['{{columns}}', '{{name}}'], |
|
| 175 | + [implode('\', \'', $index->getUnquotedColumns()), $index->getName()], |
|
| 176 | + <<<'EOT' |
|
| 177 | 177 | $table->addUniqueIndex(['{{columns}}'], '{{name}}'); |
| 178 | 178 | |
| 179 | 179 | EOT |
| 180 | - ); |
|
| 181 | - } else { |
|
| 182 | - $content .= str_replace( |
|
| 183 | - ['{{columns}}', '{{name}}'], |
|
| 184 | - [implode('\', \'', $index->getUnquotedColumns()), $index->getName()], |
|
| 185 | - <<<'EOT' |
|
| 180 | + ); |
|
| 181 | + } else { |
|
| 182 | + $content .= str_replace( |
|
| 183 | + ['{{columns}}', '{{name}}'], |
|
| 184 | + [implode('\', \'', $index->getUnquotedColumns()), $index->getName()], |
|
| 185 | + <<<'EOT' |
|
| 186 | 186 | $table->addIndex(['{{columns}}'], '{{name}}'); |
| 187 | 187 | |
| 188 | 188 | EOT |
| 189 | - ); |
|
| 190 | - } |
|
| 191 | - } |
|
| 189 | + ); |
|
| 190 | + } |
|
| 191 | + } |
|
| 192 | 192 | |
| 193 | - $content .= <<<'EOT' |
|
| 193 | + $content .= <<<'EOT' |
|
| 194 | 194 | } |
| 195 | 195 | |
| 196 | 196 | EOT; |
| 197 | - } |
|
| 197 | + } |
|
| 198 | 198 | |
| 199 | - $content .= <<<'EOT' |
|
| 199 | + $content .= <<<'EOT' |
|
| 200 | 200 | return $schema; |
| 201 | 201 | EOT; |
| 202 | 202 | |
| 203 | - return $content; |
|
| 204 | - } |
|
| 203 | + return $content; |
|
| 204 | + } |
|
| 205 | 205 | } |
@@ -35,8 +35,8 @@ discard block |
||
| 35 | 35 | |
| 36 | 36 | class GenerateCommand extends Command { |
| 37 | 37 | |
| 38 | - protected static $_templateSimple = |
|
| 39 | - '<?php |
|
| 38 | + protected static $_templateSimple = |
|
| 39 | + '<?php |
|
| 40 | 40 | namespace {{namespace}}; |
| 41 | 41 | |
| 42 | 42 | use OC\DB\SchemaWrapper; |
@@ -79,94 +79,94 @@ discard block |
||
| 79 | 79 | } |
| 80 | 80 | '; |
| 81 | 81 | |
| 82 | - /** @var IDBConnection */ |
|
| 83 | - protected $connection; |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * @param IDBConnection $connection |
|
| 87 | - */ |
|
| 88 | - public function __construct(IDBConnection $connection) { |
|
| 89 | - $this->connection = $connection; |
|
| 90 | - |
|
| 91 | - parent::__construct(); |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - protected function configure() { |
|
| 95 | - $this |
|
| 96 | - ->setName('migrations:generate') |
|
| 97 | - ->addArgument('app', InputArgument::REQUIRED, 'Name of the app this migration command shall work on') |
|
| 98 | - ->addArgument('version', InputArgument::REQUIRED, 'Major version of this app, to allow versions on parallel development branches') |
|
| 99 | - ; |
|
| 100 | - |
|
| 101 | - parent::configure(); |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - public function execute(InputInterface $input, OutputInterface $output) { |
|
| 105 | - $appName = $input->getArgument('app'); |
|
| 106 | - $version = $input->getArgument('version'); |
|
| 107 | - |
|
| 108 | - if (!preg_match('/^\d{1,16}$/',$version)) { |
|
| 109 | - $output->writeln('<error>The given version is invalid. Only 0-9 are allowed (max. 16 digits)</error>'); |
|
| 110 | - return 1; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - $ms = new MigrationService($appName, $this->connection, new ConsoleOutput($output)); |
|
| 114 | - |
|
| 115 | - $date = date('YmdHis'); |
|
| 116 | - $path = $this->generateMigration($ms, 'Version' . $version . 'Date' . $date); |
|
| 117 | - |
|
| 118 | - $output->writeln("New migration class has been generated to <info>$path</info>"); |
|
| 119 | - return 0; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * @param MigrationService $ms |
|
| 124 | - * @param string $className |
|
| 125 | - * @param string $schemaBody |
|
| 126 | - * @return string |
|
| 127 | - */ |
|
| 128 | - protected function generateMigration(MigrationService $ms, $className, $schemaBody = '') { |
|
| 129 | - if ($schemaBody === '') { |
|
| 130 | - $schemaBody = "\t\t" . 'return null;'; |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - |
|
| 134 | - $placeHolders = [ |
|
| 135 | - '{{namespace}}', |
|
| 136 | - '{{classname}}', |
|
| 137 | - '{{schemabody}}', |
|
| 138 | - ]; |
|
| 139 | - $replacements = [ |
|
| 140 | - $ms->getMigrationsNamespace(), |
|
| 141 | - $className, |
|
| 142 | - $schemaBody, |
|
| 143 | - ]; |
|
| 144 | - $code = str_replace($placeHolders, $replacements, self::$_templateSimple); |
|
| 145 | - $dir = $ms->getMigrationsDirectory(); |
|
| 146 | - |
|
| 147 | - $this->ensureMigrationDirExists($dir); |
|
| 148 | - $path = $dir . '/' . $className . '.php'; |
|
| 149 | - |
|
| 150 | - if (file_put_contents($path, $code) === false) { |
|
| 151 | - throw new RuntimeException('Failed to generate new migration step.'); |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - return $path; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - protected function ensureMigrationDirExists($directory) { |
|
| 158 | - if (file_exists($directory) && is_dir($directory)) { |
|
| 159 | - return; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - if (file_exists($directory)) { |
|
| 163 | - throw new \RuntimeException("Could not create folder \"$directory\""); |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - $this->ensureMigrationDirExists(dirname($directory)); |
|
| 167 | - |
|
| 168 | - if (!@mkdir($directory) && !is_dir($directory)) { |
|
| 169 | - throw new \RuntimeException("Could not create folder \"$directory\""); |
|
| 170 | - } |
|
| 171 | - } |
|
| 82 | + /** @var IDBConnection */ |
|
| 83 | + protected $connection; |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * @param IDBConnection $connection |
|
| 87 | + */ |
|
| 88 | + public function __construct(IDBConnection $connection) { |
|
| 89 | + $this->connection = $connection; |
|
| 90 | + |
|
| 91 | + parent::__construct(); |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + protected function configure() { |
|
| 95 | + $this |
|
| 96 | + ->setName('migrations:generate') |
|
| 97 | + ->addArgument('app', InputArgument::REQUIRED, 'Name of the app this migration command shall work on') |
|
| 98 | + ->addArgument('version', InputArgument::REQUIRED, 'Major version of this app, to allow versions on parallel development branches') |
|
| 99 | + ; |
|
| 100 | + |
|
| 101 | + parent::configure(); |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + public function execute(InputInterface $input, OutputInterface $output) { |
|
| 105 | + $appName = $input->getArgument('app'); |
|
| 106 | + $version = $input->getArgument('version'); |
|
| 107 | + |
|
| 108 | + if (!preg_match('/^\d{1,16}$/',$version)) { |
|
| 109 | + $output->writeln('<error>The given version is invalid. Only 0-9 are allowed (max. 16 digits)</error>'); |
|
| 110 | + return 1; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + $ms = new MigrationService($appName, $this->connection, new ConsoleOutput($output)); |
|
| 114 | + |
|
| 115 | + $date = date('YmdHis'); |
|
| 116 | + $path = $this->generateMigration($ms, 'Version' . $version . 'Date' . $date); |
|
| 117 | + |
|
| 118 | + $output->writeln("New migration class has been generated to <info>$path</info>"); |
|
| 119 | + return 0; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * @param MigrationService $ms |
|
| 124 | + * @param string $className |
|
| 125 | + * @param string $schemaBody |
|
| 126 | + * @return string |
|
| 127 | + */ |
|
| 128 | + protected function generateMigration(MigrationService $ms, $className, $schemaBody = '') { |
|
| 129 | + if ($schemaBody === '') { |
|
| 130 | + $schemaBody = "\t\t" . 'return null;'; |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + |
|
| 134 | + $placeHolders = [ |
|
| 135 | + '{{namespace}}', |
|
| 136 | + '{{classname}}', |
|
| 137 | + '{{schemabody}}', |
|
| 138 | + ]; |
|
| 139 | + $replacements = [ |
|
| 140 | + $ms->getMigrationsNamespace(), |
|
| 141 | + $className, |
|
| 142 | + $schemaBody, |
|
| 143 | + ]; |
|
| 144 | + $code = str_replace($placeHolders, $replacements, self::$_templateSimple); |
|
| 145 | + $dir = $ms->getMigrationsDirectory(); |
|
| 146 | + |
|
| 147 | + $this->ensureMigrationDirExists($dir); |
|
| 148 | + $path = $dir . '/' . $className . '.php'; |
|
| 149 | + |
|
| 150 | + if (file_put_contents($path, $code) === false) { |
|
| 151 | + throw new RuntimeException('Failed to generate new migration step.'); |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + return $path; |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + protected function ensureMigrationDirExists($directory) { |
|
| 158 | + if (file_exists($directory) && is_dir($directory)) { |
|
| 159 | + return; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + if (file_exists($directory)) { |
|
| 163 | + throw new \RuntimeException("Could not create folder \"$directory\""); |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + $this->ensureMigrationDirExists(dirname($directory)); |
|
| 167 | + |
|
| 168 | + if (!@mkdir($directory) && !is_dir($directory)) { |
|
| 169 | + throw new \RuntimeException("Could not create folder \"$directory\""); |
|
| 170 | + } |
|
| 171 | + } |
|
| 172 | 172 | } |