Merge pull request #50 from davidporos92/hotfix/migration-generator

Delete old migration file if exists when creating a new one
This commit is contained in:
Amine Ben hammou 2018-06-09 05:30:26 +00:00 committed by GitHub
commit 8d804e09eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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 . ';';