Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions src/Illuminate/Database/Console/Migrations/MigrateMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Illuminate\Support\Str;
use Symfony\Component\Console\Attribute\AsCommand;

use function Illuminate\Filesystem\join_paths;

#[AsCommand(name: 'make:migration')]
class MigrateMakeCommand extends BaseCommand implements PromptsForMissingInput
{
Expand All @@ -21,7 +23,8 @@ class MigrateMakeCommand extends BaseCommand implements PromptsForMissingInput
{--table= : The table to migrate}
{--path= : The location where the migration file should be created}
{--realpath : Indicate any provided migration file paths are pre-resolved absolute paths}
{--fullpath : Output the full path of the migration (Deprecated)}';
{--fullpath : Output the full path of the migration (Deprecated)}
{--check-duplicate : Prevent duplicate migration creation}';

/**
* The console command description.
Expand Down Expand Up @@ -63,7 +66,7 @@ public function __construct(MigrationCreator $creator, Composer $composer)
/**
* Execute the console command.
*
* @return void
* @return self::SUCCESS|self::FAILURE|self::INVALID
*/
public function handle()
{
Expand All @@ -72,6 +75,18 @@ public function handle()
// to be freshly created so we can create the appropriate migrations.
$name = Str::snake(trim($this->input->getArgument('name')));

if ($this->migrationExists($name)) {
$message = 'Migration '.$name.' already exists.';

if ($this->option('check-duplicate')) {
$this->components->error($message);

return self::FAILURE;
}

$this->components->warn($message.' Make sure the name is unique if needed.');
}

$table = $this->input->getOption('table');

$create = $this->input->getOption('create') ?: false;
Expand All @@ -96,6 +111,8 @@ public function handle()
// the migration out, we will dump-autoload for the entire framework to
// make sure that the migrations are registered by the class loaders.
$this->writeMigration($name, $table, $create);

return self::SUCCESS;
}

/**
Expand Down Expand Up @@ -146,4 +163,17 @@ protected function promptForMissingArgumentsUsing()
'name' => ['What should the migration be named?', 'E.g. create_flights_table'],
];
}

/**
* Determine whether a migration with the given name already exists.
*
* @param string $name
* @return bool
*/
protected function migrationExists(string $name): bool
{
return count(glob(
join_paths($this->getMigrationPath(), '*_*_*_*_'.$name.'.php')
)) !== 0;
}
}