From 86493ab228759e9bfc7d0409466a5fdf5ac9ab8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=B6r=C3=B6s=20D=C3=A1vid?= Date: Sat, 21 Oct 2017 01:19:28 +0200 Subject: [PATCH] delete old migration file refs #44 --- src/Commands/MigrationCommand.php | 45 ++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/src/Commands/MigrationCommand.php b/src/Commands/MigrationCommand.php index cb5b23f..8fe23bd 100644 --- a/src/Commands/MigrationCommand.php +++ b/src/Commands/MigrationCommand.php @@ -3,7 +3,7 @@ class MigrationCommand extends BaseCommand { - protected $signature = 'wn:migration + protected $signature = 'wn:migration {table : The table name.} {--schema= : the schema.} {--add= : specifies additional columns like timestamps, softDeletes, rememberToken and nullableTimestamps.} @@ -12,15 +12,16 @@ class MigrationCommand extends BaseCommand { {--parsed : tells the command that arguments have been already parsed. To use when calling the command from an other command and passing the parsed arguments and options} {--force= : override the existing files} '; - // {action : One of create, add, remove or drop options.} - // The action is only create for the moment + // {action : One of create, add, remove or drop options.} + // The action is only create for the moment - protected $description = 'Generates a migration to create a table with schema'; + protected $description = 'Generates a migration to create a table with schema'; public function handle() { $table = $this->argument('table'); $name = 'Create' . ucwords(camel_case($table)); + $snakeName = snake_case($name); $content = $this->getTemplate('migration') ->with([ @@ -34,12 +35,26 @@ class MigrationCommand extends BaseCommand { $file = $this->option('file'); if(! $file){ - $file = date('Y_m_d_His_') . snake_case($name) . '_table'; + $file = date('Y_m_d_His_') . $snakeName . '_table'; + $this->deleteOldMigration($snakeName); + }else{ + $this->deleteOldMigration($file); } $this->save($content, "./database/migrations/{$file}.php", "{$table} migration"); } + protected function deleteOldMigration($fileName) + { + foreach (new \DirectoryIterator("./database/migrations/") as $fileInfo){ + if($fileInfo->isDot()) continue; + + if(strpos($fileInfo->getFilename(), $fileName) !== FALSE){ + unlink($fileInfo->getPathname()); + } + } + } + protected function getSchema() { $schema = $this->option('schema'); @@ -127,20 +142,20 @@ class MigrationCommand extends BaseCommand { if($key['on_delete']){ $constraint .= PHP_EOL . $this->getTemplate('migration/on-constraint') - ->with([ - 'event' => 'Delete', - 'action' => $key['on_delete'] - ]) - ->get(); + ->with([ + 'event' => 'Delete', + 'action' => $key['on_delete'] + ]) + ->get(); } if($key['on_update']){ $constraint .= PHP_EOL . $this->getTemplate('migration/on-constraint') - ->with([ - 'event' => 'Update', - 'action' => $key['on_update'] - ]) - ->get(); + ->with([ + 'event' => 'Update', + 'action' => $key['on_update'] + ]) + ->get(); } return $constraint . ';';