4
4
5
5
namespace McMatters \DbCommands \Console \Commands ;
6
6
7
+ use Illuminate \Contracts \Filesystem \FileNotFoundException ;
7
8
use Illuminate \Database \Console \Migrations \MigrateCommand ;
9
+ use Illuminate \Support \Arr ;
10
+ use Illuminate \Support \Str ;
8
11
use McMatters \DbCommands \Extenders \Database \Migrator ;
12
+ use RuntimeException ;
9
13
10
14
/**
11
15
* Class MigrateSingle
@@ -17,7 +21,9 @@ class MigrateSingle extends MigrateCommand
17
21
/**
18
22
* @var string
19
23
*/
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.}
21
27
{--database= : The database connection to use.}
22
28
{--force : Force the operation to run when in production.}
23
29
{--pretend : Dump the SQL queries that would be run.} ' ;
@@ -39,21 +45,72 @@ public function __construct(Migrator $migrator)
39
45
40
46
/**
41
47
* @return void
48
+ * @throws FileNotFoundException
49
+ * @throws RuntimeException
42
50
*/
43
51
public function fire ()
44
52
{
45
53
if (!$ this ->confirmToProceed ()) {
46
54
return ;
47
55
}
48
56
57
+ $ this ->checkRequirements ();
49
58
$ this ->prepareDatabase ();
50
59
51
- $ file = $ this ->getMigrationPath (). ' / ' . $ this -> argument ( ' file ' );
60
+ $ file = $ this ->getMigrationFile ( );
52
61
53
62
$ this ->migrator ->run ($ file , ['pretend ' => $ this ->option ('pretend ' )]);
54
63
55
64
foreach ($ this ->migrator ->getNotes () as $ note ) {
56
65
$ this ->output ->writeln ($ note );
57
66
}
58
67
}
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
+ }
59
116
}
0 commit comments