Skip to content

Commit 6d39d5b

Browse files
committed
Add ability to run migrate:single with class name instead file name
1 parent 4450270 commit 6d39d5b

File tree

3 files changed

+64
-6
lines changed

3 files changed

+64
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ Include the service provider within your `config/app.php` file.
2121
Available commands:
2222

2323
* `php artisan db:drop-tables` — drops all tables.
24-
* `php artisan migrate:single {file}` — migrate single migration by filename.
24+
* `php artisan migrate:single {"file" or "class"}` — migrate single migration by file or class name.

src/Console/Commands/DropTables.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace McMatters\DbCommands\Console\Commands;
66

77
use Illuminate\Console\Command;
8+
use Illuminate\Support\Facades\Config;
89
use Illuminate\Support\Facades\DB;
910
use Illuminate\Support\Facades\Schema;
1011

@@ -30,7 +31,7 @@ class DropTables extends Command
3031
*/
3132
public function handle()
3233
{
33-
if (!$this->isForced() && !$this->isLocalEnvironment()) {
34+
if (!$this->isForced() && $this->isProduction()) {
3435
$this->error(
3536
'This action can\'t be performed in non-local environment.
3637
Please use --force option if you really want to proceed.'
@@ -59,8 +60,8 @@ protected function isForced(): bool
5960
/**
6061
* @return bool
6162
*/
62-
protected function isLocalEnvironment(): bool
63+
protected function isProduction(): bool
6364
{
64-
return in_array(config('app.env'), ['production', 'live'], true);
65+
return in_array(Config::get('app.env'), ['production', 'live'], true);
6566
}
6667
}

src/Console/Commands/MigrateSingle.php

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44

55
namespace McMatters\DbCommands\Console\Commands;
66

7+
use Illuminate\Contracts\Filesystem\FileNotFoundException;
78
use Illuminate\Database\Console\Migrations\MigrateCommand;
9+
use Illuminate\Support\Arr;
10+
use Illuminate\Support\Str;
811
use McMatters\DbCommands\Extenders\Database\Migrator;
12+
use RuntimeException;
913

1014
/**
1115
* Class MigrateSingle
@@ -17,7 +21,9 @@ class MigrateSingle extends MigrateCommand
1721
/**
1822
* @var string
1923
*/
20-
protected $signature = 'migrate:single {file : The file of migration to be executed.}
24+
protected $signature = 'migrate:single
25+
{--file : The file of migration to be executed.}
26+
{--class : The class name of migration.}
2127
{--database= : The database connection to use.}
2228
{--force : Force the operation to run when in production.}
2329
{--pretend : Dump the SQL queries that would be run.}';
@@ -39,21 +45,72 @@ public function __construct(Migrator $migrator)
3945

4046
/**
4147
* @return void
48+
* @throws FileNotFoundException
49+
* @throws RuntimeException
4250
*/
4351
public function fire()
4452
{
4553
if (!$this->confirmToProceed()) {
4654
return;
4755
}
4856

57+
$this->checkRequirements();
4958
$this->prepareDatabase();
5059

51-
$file = $this->getMigrationPath().'/'.$this->argument('file');
60+
$file = $this->getMigrationFile();
5261

5362
$this->migrator->run($file, ['pretend' => $this->option('pretend')]);
5463

5564
foreach ($this->migrator->getNotes() as $note) {
5665
$this->output->writeln($note);
5766
}
5867
}
68+
69+
/**
70+
* @return string
71+
* @throws FileNotFoundException
72+
*/
73+
protected function getMigrationFile(): string
74+
{
75+
if ($this->hasOption('class')) {
76+
$file = $this->getFileByClass();
77+
} else {
78+
$file = $this->getMigrationPath().'/'.$this->argument('file');
79+
}
80+
81+
if (!$file || !file_exists($file)) {
82+
throw new FileNotFoundException('File with migration not found.');
83+
}
84+
85+
return $file;
86+
}
87+
88+
/**
89+
* @return string|null
90+
*/
91+
protected function getFileByClass()
92+
{
93+
$class = $this->option('class');
94+
95+
if (!$class) {
96+
return null;
97+
}
98+
99+
$class = Str::snake($class);
100+
101+
$files = glob("{$this->getMigrationPath()}/[0-9_]*{$class}.php");
102+
103+
return Arr::first($files);
104+
}
105+
106+
/**
107+
* @return void
108+
* @throws RuntimeException
109+
*/
110+
protected function checkRequirements()
111+
{
112+
if (!$this->hasOption('file') && !$this->hasOption('class')) {
113+
throw new RuntimeException('You must pass at least one argument "file" or "class"');
114+
}
115+
}
59116
}

0 commit comments

Comments
 (0)