From 94a95c59832c8edad38c7d036d891686a094486e Mon Sep 17 00:00:00 2001 From: Zacchaeus Bolaji Date: Wed, 6 Nov 2019 13:52:41 +0100 Subject: [PATCH 1/5] Fixed exception in generating enum column type --- src/Xethron/MigrationsGenerator/Generators/FieldGenerator.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Xethron/MigrationsGenerator/Generators/FieldGenerator.php b/src/Xethron/MigrationsGenerator/Generators/FieldGenerator.php index 0ac0220..d671b60 100644 --- a/src/Xethron/MigrationsGenerator/Generators/FieldGenerator.php +++ b/src/Xethron/MigrationsGenerator/Generators/FieldGenerator.php @@ -73,6 +73,8 @@ protected function getEnum($table) protected function setEnum(array $fields, $table) { foreach ($this->getEnum($table) as $column) { + $column->column_name = $column->column_name ?? $column->COLUMN_NAME; + $column->column_type = $column->column_type ?? $column->COLUMN_TYPE; $fields[$column->column_name]['type'] = 'enum'; $fields[$column->column_name]['args'] = str_replace('enum(', 'array(', $column->column_type); } From 998b932879f223f2ce4d3b8467691cf3fd6154f7 Mon Sep 17 00:00:00 2001 From: Zacchaeus Bolaji Date: Wed, 6 Nov 2019 16:28:54 +0100 Subject: [PATCH 2/5] Support option of --ignore-created-migration --- .idea/.gitignore | 3 + .../MigrateGenerateCommand.php | 67 ++++++++++++++++++- 2 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 .idea/.gitignore diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..0e40fe8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ + +# Default ignored files +/workspace.xml \ No newline at end of file diff --git a/src/Xethron/MigrationsGenerator/MigrateGenerateCommand.php b/src/Xethron/MigrationsGenerator/MigrateGenerateCommand.php index 90b78d7..79083de 100644 --- a/src/Xethron/MigrationsGenerator/MigrateGenerateCommand.php +++ b/src/Xethron/MigrationsGenerator/MigrateGenerateCommand.php @@ -105,6 +105,11 @@ class MigrateGenerateCommand extends GeneratorCommand { */ protected $connection = null; + /** + * @var bool + */ + protected $ignoreCreatedMigration = false; + /** * @param \Way\Generators\Generator $generator * @param \Way\Generators\Filesystem\Filesystem $file @@ -180,6 +185,10 @@ public function fire() $this->batch = $this->askNumeric( 'Next Batch Number is: '. $batch .'. We recommend using Batch Number 0 so that it becomes the "first" migration', 0 ); } + if($this->hasOption('ignore-created-migration')) { + $this->ignoreCreatedMigration = true; + } + $this->info( "Setting up Tables and Index Migrations" ); $this->datePrefix = date( 'Y_m_d_His' ); $this->generateTablesAndIndices( $tables ); @@ -240,12 +249,58 @@ protected function generateTablesAndIndices( array $tables ) foreach ( $tables as $table ) { $this->table = $table; $this->migrationName = 'create_'. $this->table .'_table'; + + if($this->ignoreCreatedMigration && $this->migrationExist($this->table)) { + $this->info( "Migration $this->migrationName already created: Skipping..." ); + continue; + } + $this->fields = $this->schemaGenerator->getFields( $this->table ); $this->generate(); } } + /**Return a substring between 2 strings + * @param $string + * @param $start + * @param $end + * @param bool $trim + * @return false|string + */ + public function getStringBetween($string, $start, $end, $trim = true) { + $string = ' ' . $string; + $ini = strpos($string, $start); + if ($ini == 0) return ''; + $ini += strlen($start); + $len = strpos($string, $end, $ini) - $ini; + $output = substr($string, $ini, $len); + return ($trim) ? trim(strip_tags($output)) : $output; + } + + /**Check if migration for table already exists + * @param $table + * @param string $prefix + * @param string $suffix + * @return bool + */ + protected function migrationExist($table, $prefix = 'create_', $suffix = '_table') + { + $exists = false; + $path = $this->getPathByOptionOrConfig( 'path', 'migration_target_path' ); + $files = scandir($path); + + foreach ($files as $file) + { + if($this->getStringBetween($file, $prefix, $suffix) == $table) { + $exists = true; + break; + } + } + + return $exists; + } + /** * Generate foreign key migrations. * @@ -255,10 +310,19 @@ protected function generateTablesAndIndices( array $tables ) protected function generateForeignKeys( array $tables ) { $this->method = 'table'; + $prefix = 'add_foreign_keys_to_'; + $suffix = '_table'; foreach ( $tables as $table ) { $this->table = $table; $this->migrationName = 'add_foreign_keys_to_'. $this->table .'_table'; + + $this->migrationName = $prefix . $this->table . $suffix; + if($this->ignoreCreatedMigration && $this->migrationExist($this->table, $prefix, $suffix)) { + $this->info( "Migration $this->migrationName already created: Skipping..." ); + continue; + } + $this->fields = $this->schemaGenerator->getForeignKeyConstraints( $this->table ); $this->generate(); @@ -363,7 +427,8 @@ protected function getOptions() ['connection', 'c', InputOption::VALUE_OPTIONAL, 'The database connection to use.', $this->config->get( 'database.default' )], ['tables', 't', InputOption::VALUE_OPTIONAL, 'A list of Tables you wish to Generate Migrations for separated by a comma: users,posts,comments'], ['ignore', 'i', InputOption::VALUE_OPTIONAL, 'A list of Tables you wish to ignore, separated by a comma: users,posts,comments' ], - ['path', 'p', InputOption::VALUE_OPTIONAL, 'Where should the file be created?'], + ['ignore-created-migration', 'icm', InputOption::VALUE_OPTIONAL, 'Flag to ignore migrations already created' ], + ['path', 'p', InputOption::VALUE_OPTIONAL, 'Where should the file be created?'], ['templatePath', 'tp', InputOption::VALUE_OPTIONAL, 'The location of the template for this generator'], ['defaultIndexNames', null, InputOption::VALUE_NONE, 'Don\'t use db index names for migrations'], ['defaultFKNames', null, InputOption::VALUE_NONE, 'Don\'t use db foreign key names for migrations'], From a8a4cf7b284af116a5be88c030a5095708208dcb Mon Sep 17 00:00:00 2001 From: Zacchaeus Bolaji Date: Wed, 6 Nov 2019 16:29:57 +0100 Subject: [PATCH 3/5] Added .idea to .gitignore --- .gitignore | 1 + .idea/.gitignore | 3 --- 2 files changed, 1 insertion(+), 3 deletions(-) delete mode 100644 .idea/.gitignore diff --git a/.gitignore b/.gitignore index 7c528b4..f7c8344 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ vendor report composer.lock +.idea diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 0e40fe8..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ - -# Default ignored files -/workspace.xml \ No newline at end of file From 5b75535ed80f57796d554f9296dbd55ca2e21f1e Mon Sep 17 00:00:00 2001 From: Zacchaeus Bolaji Date: Wed, 6 Nov 2019 17:22:21 +0100 Subject: [PATCH 4/5] make ignore-created-migration flag --- src/Xethron/MigrationsGenerator/MigrateGenerateCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Xethron/MigrationsGenerator/MigrateGenerateCommand.php b/src/Xethron/MigrationsGenerator/MigrateGenerateCommand.php index 79083de..baa827b 100644 --- a/src/Xethron/MigrationsGenerator/MigrateGenerateCommand.php +++ b/src/Xethron/MigrationsGenerator/MigrateGenerateCommand.php @@ -427,7 +427,7 @@ protected function getOptions() ['connection', 'c', InputOption::VALUE_OPTIONAL, 'The database connection to use.', $this->config->get( 'database.default' )], ['tables', 't', InputOption::VALUE_OPTIONAL, 'A list of Tables you wish to Generate Migrations for separated by a comma: users,posts,comments'], ['ignore', 'i', InputOption::VALUE_OPTIONAL, 'A list of Tables you wish to ignore, separated by a comma: users,posts,comments' ], - ['ignore-created-migration', 'icm', InputOption::VALUE_OPTIONAL, 'Flag to ignore migrations already created' ], + ['ignore-created-migration', 'icm', null, 'Flag to ignore migrations already created' ], ['path', 'p', InputOption::VALUE_OPTIONAL, 'Where should the file be created?'], ['templatePath', 'tp', InputOption::VALUE_OPTIONAL, 'The location of the template for this generator'], ['defaultIndexNames', null, InputOption::VALUE_NONE, 'Don\'t use db index names for migrations'], From 912a45dc6f9a76216e11306ab4b2df1b6ca3b07e Mon Sep 17 00:00:00 2001 From: Zacchaeus Bolaji Date: Sat, 7 Dec 2019 18:42:09 +0100 Subject: [PATCH 5/5] Remove PHP7 syntax --- src/Xethron/MigrationsGenerator/Generators/FieldGenerator.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Xethron/MigrationsGenerator/Generators/FieldGenerator.php b/src/Xethron/MigrationsGenerator/Generators/FieldGenerator.php index d671b60..2e62612 100644 --- a/src/Xethron/MigrationsGenerator/Generators/FieldGenerator.php +++ b/src/Xethron/MigrationsGenerator/Generators/FieldGenerator.php @@ -73,8 +73,8 @@ protected function getEnum($table) protected function setEnum(array $fields, $table) { foreach ($this->getEnum($table) as $column) { - $column->column_name = $column->column_name ?? $column->COLUMN_NAME; - $column->column_type = $column->column_type ?? $column->COLUMN_TYPE; + $column->column_name = property_exists($column, 'column_name') ? $column->column_name : $column->COLUMN_NAME; + $column->column_type = property_exists($column, 'column_type') ? $column->column_type : $column->COLUMN_TYPE; $fields[$column->column_name]['type'] = 'enum'; $fields[$column->column_name]['args'] = str_replace('enum(', 'array(', $column->column_type); }