1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace yentu\commands; |
4
|
|
|
|
5
|
|
|
use yentu\ChangeReverser; |
6
|
|
|
use yentu\database\DatabaseItem; |
7
|
|
|
use yentu\factories\DatabaseManipulatorFactory; |
8
|
|
|
use clearice\io\Io; |
9
|
|
|
|
10
|
|
|
class Rollback extends Command |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
private $schemaCondition; |
14
|
|
|
private $schemaConditionData = []; |
15
|
|
|
private $manipulatorFactory; |
16
|
|
|
private $io; |
17
|
|
|
private $changeReverser; |
18
|
|
|
|
19
|
|
|
public function __construct(DatabaseManipulatorFactory $manipulatorFactory, Io $io, ChangeReverser $changeReverser) |
20
|
|
|
{ |
21
|
|
|
$this->manipulatorFactory = $manipulatorFactory; |
22
|
|
|
$this->io = $io; |
23
|
|
|
$this->changeReverser = $changeReverser; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @throws \yentu\exceptions\DatabaseManipulatorException |
28
|
|
|
*/ |
29
|
|
|
public function run() |
30
|
|
|
{ |
31
|
|
|
$db = $this->manipulatorFactory->createManipulator(); |
32
|
|
|
$this->changeReverser->setDriver($db); |
33
|
|
|
$previousMigration = ''; |
34
|
|
|
|
35
|
|
|
if (isset($this->options['__args'])) { |
36
|
|
|
$operations = []; |
37
|
|
|
foreach ($this->options['__args'] ?? [] as $set) { |
38
|
|
|
$operations += $this->getOperations($db, $set); |
39
|
|
|
} |
40
|
|
|
} else { |
41
|
|
|
$session = $db->getLastSession(); |
42
|
|
|
$operations = $db->query( |
43
|
|
|
"SELECT id, method, arguments, migration, default_schema FROM yentu_history WHERE $this->schemaCondition session = ? ORDER BY id DESC", $this->schemaConditionData + [$session] |
44
|
|
|
); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
foreach ($operations as $operation) { |
48
|
|
|
if ($previousMigration !== $operation['migration']) { |
49
|
|
|
$this->io->output( |
50
|
|
|
"Rolling back '{$operation['migration']}' migration" . |
51
|
|
|
($operation['default_schema'] != '' ? " on `{$operation['default_schema']}` schema." : ".") . "\n" |
52
|
|
|
); |
53
|
|
|
$previousMigration = $operation['migration']; |
54
|
|
|
} |
55
|
|
|
$this->changeReverser->call($operation['method'], json_decode($operation['arguments'], true)); |
56
|
|
|
$db->query('DELETE FROM yentu_history WHERE id = ?', array($operation['id'])); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
private function getOperations($db, $set) |
61
|
|
|
{ |
62
|
|
|
$operations = []; |
63
|
|
|
if (preg_match("/[0-9]{14}/", $set)) { |
64
|
|
|
$operations = $db->query( |
65
|
|
|
"SELECT id, method, arguments, migration, default_schema FROM yentu_history WHERE $this->schemaCondition version = ? ORDER by id DESC", $this->schemaConditionData + [$set] |
66
|
|
|
); |
67
|
|
|
} elseif (preg_match("/[a-zA-Z\_\-]+/", $set)) { |
68
|
|
|
$operations = $db->query( |
69
|
|
|
"SELECT id, method, arguments, migration, default_schema FROM yentu_history WHERE $this->schemaCondition migration = ? ORDER by id DESC", $this->schemaConditionData + [$set] |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
return $operations; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
} |
76
|
|
|
|