Skip to content

Commit 837e56a

Browse files
committed
✨ add usefile() method to specify a particular migration file to run
Signed-off-by: otengkwame <[email protected]>
1 parent 7e651fe commit 837e56a

File tree

1 file changed

+92
-1
lines changed

1 file changed

+92
-1
lines changed

Core/controllers/commands/Migration.php

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,11 @@ private function executedMigration($file)
190190
* Executed Migrations
191191
*
192192
* @param boolean $latest
193+
* @param boolean $single
193194
* @return mixed
194195
*/
195-
private function executedMigrations($latest = true, $single = false) {
196+
private function executedMigrations($latest = true, $single = false)
197+
{
196198

197199
if ($single) {
198200
return $this->db
@@ -360,6 +362,95 @@ public function run($step = 0) {
360362
}
361363
}
362364

365+
/**
366+
* Specify file to use for migrations
367+
* and which method to run
368+
*
369+
* @param string $file
370+
* @param string $key
371+
* @return void
372+
*/
373+
public function usefile($file, $key)
374+
{
375+
376+
$migration = $this->executedMigration($file);
377+
378+
if ($migration && $key === '--down') {
379+
380+
try {
381+
382+
$startTime = microtime(true);
383+
384+
$file = $migration->migration;
385+
$path = self::PATH . $file;
386+
$exists = file_exists($path);
387+
388+
if ($exists) {
389+
390+
echo $this->info("\tDropping migration $file \n");
391+
392+
$this->prepareDownMigration($file);
393+
394+
$this->db->delete(self::TABLE, ['migration' => $migration->migration]);
395+
396+
echo $this->success("\tDropped $file successfully".PHP_EOL);
397+
}
398+
399+
$elapsedTime = round(microtime(true) - $startTime, 3) * 1000;
400+
401+
echo $this->warning("\tTook $elapsedTime ms to drop migration");
402+
exit;
403+
404+
} catch (\Exception $e) {
405+
exit("Error: ".$e->getMessage().PHP_EOL);
406+
}
407+
408+
}
409+
410+
if ($migration) {
411+
412+
echo $this->info("\n\tMigration exists already, dropping: $file \n");
413+
414+
$this->prepareDownMigration($file);
415+
416+
$this->db->delete(self::TABLE, ['migration' => $migration->migration]);
417+
418+
echo $this->warning("\tRun --up --usefile={$file} once more to complete migration".PHP_EOL);
419+
}
420+
421+
if (!$migration) {
422+
try {
423+
424+
$startTime = microtime(true);
425+
426+
$batch = $this->executedMigrations(true, true);
427+
428+
if (!empty($batch)) {
429+
$batch = $batch->batch + 1;
430+
} else {
431+
$batch = 1;
432+
}
433+
434+
echo $this->info("\tProcessing $file \n");
435+
436+
$this->prepareUpMigration($file);
437+
438+
$this->db->insert(self::TABLE, ['migration' => $file, 'batch' => $batch]);
439+
440+
echo $this->success("\t$file done".PHP_EOL);
441+
442+
$elapsedTime = round(microtime(true) - $startTime, 3) * 1000;
443+
444+
echo $this->warning("\tTook $elapsedTime ms to run migrations", 1);
445+
446+
} catch (\Exception $e) {
447+
exit("Error: ".$e->getMessage().PHP_EOL);
448+
}
449+
}
450+
451+
}
452+
453+
363454
/**
364455
* Truncate Migrations Table
365456
*

0 commit comments

Comments
 (0)