4
4
5
5
use Dentro \Patcher \Events \PatchEnded ;
6
6
use Dentro \Patcher \Events \PatchStarted ;
7
- use Illuminate \Database \Migrations \Migration ;
8
7
use Illuminate \Database \Migrations \Migrator ;
9
8
10
9
class Patcher extends Migrator
@@ -28,12 +27,10 @@ public function runPending(array $migrations, array $options = []): void
28
27
29
28
$ batch = $ this ->repository ->getNextBatchNumber ();
30
29
31
- $ pretend = $ options ['pretend ' ] ?? false ;
32
-
33
30
$ step = $ options ['step ' ] ?? false ;
34
31
35
32
foreach ($ migrations as $ file ) {
36
- $ this ->patch ($ file , $ batch, $ pretend );
33
+ $ this ->patch ($ file , $ batch );
37
34
38
35
if ($ step ) {
39
36
$ batch ++;
@@ -46,45 +43,47 @@ public function runPending(array $migrations, array $options = []): void
46
43
*
47
44
* @param string $file
48
45
* @param int $batch
49
- * @param bool $pretend
50
- *
51
46
* @return void
52
47
* @throws \Throwable
53
48
*/
54
- protected function patch (string $ file , int $ batch, bool $ pretend ): void
49
+ protected function patch (string $ file , int $ batch ): void
55
50
{
56
- $ migration = $ this ->resolvePath ($ file );
51
+ $ patch = $ this ->resolvePath ($ file );
57
52
58
53
$ name = $ this ->getMigrationName ($ file );
59
54
60
- $ migration ->setContainer (app ())->setCommand (app ('command.patcher ' ));
61
-
62
- $ this ->note ("<comment>Patching:</comment> {$ name }" );
55
+ $ this ->note ("<comment>Patching:</comment> $ name " );
63
56
64
57
$ startTime = microtime (true );
65
58
66
- if ($ this ->isEligible ()) {
67
- $ this ->runPatch ($ migration );
59
+ if ($ patch instanceof Patch && $ this ->isEligible ($ patch )) {
60
+ $ patch
61
+ ->setContainer (app ())
62
+ ->setCommand (app ('command.patcher ' ))
63
+ ->setLogger (app ('log ' )->driver (PatcherServiceProvider::$ LOG_CHANNEL ));
64
+
65
+ $ this ->runPatch ($ patch );
68
66
69
67
$ runTime = round (microtime (true ) - $ startTime , 2 );
70
68
71
69
$ this ->repository ->log ($ name , $ batch );
72
70
73
- $ this ->note ("<info>Patched:</info> { $ name} ( { $ runTime} seconds). " );
71
+ $ this ->note ("<info>Patched:</info> $ name ( $ runTime seconds). " );
74
72
} else {
75
- $ this ->note ("<comment>Skipped:</comment> { $ name} is not eligible to run in current condition. " );
73
+ $ this ->note ("<comment>Skipped:</comment> $ name is not eligible to run in current condition. " );
76
74
}
77
75
}
78
76
79
77
/**
80
78
* Determine if patcher should run.
81
79
*
80
+ * @param \Dentro\Patcher\Patch $patch
82
81
* @return bool
83
82
*/
84
- public function isEligible (): bool
83
+ public function isEligible (Patch $ patch ): bool
85
84
{
86
- if (method_exists ($ this , 'eligible ' )) {
87
- return $ this ->eligible ();
85
+ if (method_exists ($ patch , 'eligible ' )) {
86
+ return $ patch ->eligible ();
88
87
}
89
88
90
89
return true ;
@@ -93,11 +92,11 @@ public function isEligible(): bool
93
92
/**
94
93
* Run a migration inside a transaction if the database supports it.
95
94
*
96
- * @param object $patch
95
+ * @param \Dentro\Patcher\Patch $patch
97
96
* @return void
98
97
* @throws \Throwable
99
98
*/
100
- protected function runPatch (object $ patch ): void
99
+ protected function runPatch (Patch $ patch ): void
101
100
{
102
101
$ connection = $ this ->resolveConnection (
103
102
$ patch ->getConnection ()
@@ -121,9 +120,11 @@ protected function runPatch(object $patch): void
121
120
}
122
121
};
123
122
124
- $ this ->getSchemaGrammar ($ connection )->supportsSchemaTransactions ()
125
- && $ patch ->withinTransaction
126
- ? $ connection ->transaction ($ callback )
127
- : $ callback ();
123
+ if ($ patch ->withinTransaction && $ this ->getSchemaGrammar ($ connection )->supportsSchemaTransactions ()) {
124
+ $ connection ->transaction ($ callback );
125
+ return ;
126
+ }
127
+
128
+ $ callback ();
128
129
}
129
130
}
0 commit comments