-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDatabaseRollbackCommand.php
More file actions
executable file
·48 lines (36 loc) · 1.5 KB
/
DatabaseRollbackCommand.php
File metadata and controls
executable file
·48 lines (36 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
namespace Leaf\Commands;
use Leaf\Sprout\Command;
class DatabaseRollbackCommand extends Command
{
protected $signature = 'db:rollback
{file : The name of the schema file}
{--step|s=1 : The batch to rollback, default is 1}';
public $description = 'Rollback database to a previous state';
public $help = 'Rollback database to a previous state, add -s to time-travel to a specific state.';
protected function handle()
{
$fileToMigrate = $this->argument('file');
$migrations = function_exists('AppPaths') ?
glob(getcwd() . DIRECTORY_SEPARATOR . AppPaths('database') . DIRECTORY_SEPARATOR . '*.yml') :
glob(getcwd() . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'database' . DIRECTORY_SEPARATOR . '*.yml');
foreach ($migrations as $migration) {
$currentFileName = path($migration)->basename();
if ($fileToMigrate && preg_replace('/\.yml$/', '', $currentFileName) !== preg_replace('/\.yml$/', '', $fileToMigrate)) {
continue;
}
$this->writeln("> db rollback on <comment>$currentFileName</comment>");
if (
!\Leaf\Schema::rollback(
$migration,
(int) $this->option('step')
)
) {
$this->error("Could not rollback $currentFileName");
return 1;
}
}
$this->info("Database rollback completed!\n");
return 0;
}
}