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/src/Xethron/MigrationsGenerator/Generators/FieldGenerator.php b/src/Xethron/MigrationsGenerator/Generators/FieldGenerator.php index 0ac0220..2e62612 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 = 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); } diff --git a/src/Xethron/MigrationsGenerator/MigrateGenerateCommand.php b/src/Xethron/MigrationsGenerator/MigrateGenerateCommand.php index 90b78d7..baa827b 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', 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'], ['defaultFKNames', null, InputOption::VALUE_NONE, 'Don\'t use db foreign key names for migrations'],