mirror of
https://github.com/ZorgCC/lumen-generators.git
synced 2025-01-14 14:27:55 +03:00
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:
commit
8d804e09eb
@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
class MigrationCommand extends BaseCommand {
|
class MigrationCommand extends BaseCommand {
|
||||||
|
|
||||||
protected $signature = 'wn:migration
|
protected $signature = 'wn:migration
|
||||||
{table : The table name.}
|
{table : The table name.}
|
||||||
{--schema= : the schema.}
|
{--schema= : the schema.}
|
||||||
{--add= : specifies additional columns like timestamps, softDeletes, rememberToken and nullableTimestamps.}
|
{--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}
|
{--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}
|
{--force= : override the existing files}
|
||||||
';
|
';
|
||||||
// {action : One of create, add, remove or drop options.}
|
// {action : One of create, add, remove or drop options.}
|
||||||
// The action is only create for the moment
|
// 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()
|
public function handle()
|
||||||
{
|
{
|
||||||
$table = $this->argument('table');
|
$table = $this->argument('table');
|
||||||
$name = 'Create' . ucwords(camel_case($table));
|
$name = 'Create' . ucwords(camel_case($table));
|
||||||
|
$snakeName = snake_case($name);
|
||||||
|
|
||||||
$content = $this->getTemplate('migration')
|
$content = $this->getTemplate('migration')
|
||||||
->with([
|
->with([
|
||||||
@ -34,12 +35,26 @@ class MigrationCommand extends BaseCommand {
|
|||||||
|
|
||||||
$file = $this->option('file');
|
$file = $this->option('file');
|
||||||
if(! $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");
|
$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()
|
protected function getSchema()
|
||||||
{
|
{
|
||||||
$schema = $this->option('schema');
|
$schema = $this->option('schema');
|
||||||
@ -127,20 +142,20 @@ class MigrationCommand extends BaseCommand {
|
|||||||
|
|
||||||
if($key['on_delete']){
|
if($key['on_delete']){
|
||||||
$constraint .= PHP_EOL . $this->getTemplate('migration/on-constraint')
|
$constraint .= PHP_EOL . $this->getTemplate('migration/on-constraint')
|
||||||
->with([
|
->with([
|
||||||
'event' => 'Delete',
|
'event' => 'Delete',
|
||||||
'action' => $key['on_delete']
|
'action' => $key['on_delete']
|
||||||
])
|
])
|
||||||
->get();
|
->get();
|
||||||
}
|
}
|
||||||
|
|
||||||
if($key['on_update']){
|
if($key['on_update']){
|
||||||
$constraint .= PHP_EOL . $this->getTemplate('migration/on-constraint')
|
$constraint .= PHP_EOL . $this->getTemplate('migration/on-constraint')
|
||||||
->with([
|
->with([
|
||||||
'event' => 'Update',
|
'event' => 'Update',
|
||||||
'action' => $key['on_update']
|
'action' => $key['on_update']
|
||||||
])
|
])
|
||||||
->get();
|
->get();
|
||||||
}
|
}
|
||||||
|
|
||||||
return $constraint . ';';
|
return $constraint . ';';
|
||||||
|
Loading…
x
Reference in New Issue
Block a user