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

@ -21,6 +21,7 @@ class MigrationCommand extends BaseCommand {
{
$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');