1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* MIT License |
5
|
|
|
* For full license information, please view the LICENSE file that was distributed with this source code. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Phinx\Db\Adapter; |
9
|
|
|
|
10
|
|
|
use Phinx\Db\Action\AddColumn; |
11
|
|
|
use Phinx\Db\Action\AddForeignKey; |
12
|
|
|
use Phinx\Db\Action\AddIndex; |
13
|
|
|
use Phinx\Db\Action\CreateTable; |
14
|
|
|
use Phinx\Db\Action\DropForeignKey; |
15
|
|
|
use Phinx\Db\Action\DropIndex; |
16
|
|
|
use Phinx\Db\Action\DropTable; |
17
|
|
|
use Phinx\Db\Action\RemoveColumn; |
18
|
|
|
use Phinx\Db\Action\RenameColumn; |
19
|
|
|
use Phinx\Db\Action\RenameTable; |
20
|
|
|
use Phinx\Db\Plan\Intent; |
21
|
|
|
use Phinx\Db\Plan\Plan; |
22
|
|
|
use Phinx\Db\Table\Table; |
23
|
|
|
use Phinx\Migration\IrreversibleMigrationException; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Phinx Proxy Adapter. |
27
|
|
|
* |
28
|
|
|
* Used for recording migration commands to automatically reverse them. |
29
|
|
|
* |
30
|
|
|
* @author Rob Morgan <[email protected]> |
31
|
|
|
*/ |
32
|
|
|
class ProxyAdapter extends AdapterWrapper |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var \Phinx\Db\Action\Action[] |
36
|
|
|
*/ |
37
|
|
|
protected $commands = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @inheritDoc |
41
|
|
|
*/ |
42
|
|
|
public function getAdapterType() |
43
|
|
|
{ |
44
|
|
|
return 'ProxyAdapter'; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @inheritDoc |
49
|
|
|
*/ |
50
|
|
|
public function createTable(Table $table, array $columns = [], array $indexes = []) |
51
|
|
|
{ |
52
|
|
|
$this->commands[] = new CreateTable($table); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @inheritDoc |
57
|
|
|
*/ |
58
|
|
|
public function executeActions(Table $table, array $actions) |
59
|
|
|
{ |
60
|
|
|
$this->commands = array_merge($this->commands, $actions); |
61
|
|
|
} |
62
|
4 |
|
|
63
|
|
|
/** |
64
|
4 |
|
* Gets an array of the recorded commands in reverse. |
65
|
4 |
|
* |
66
|
|
|
* @throws \Phinx\Migration\IrreversibleMigrationException if a command cannot be reversed. |
67
|
|
|
* @return \Phinx\Db\Plan\Intent |
68
|
|
|
*/ |
69
|
|
|
public function getInvertedCommands() |
70
|
4 |
|
{ |
71
|
|
|
$inverted = new Intent(); |
72
|
4 |
|
|
73
|
4 |
|
foreach (array_reverse($this->commands) as $command) { |
74
|
|
|
switch (true) { |
75
|
|
|
case $command instanceof CreateTable: |
76
|
|
|
$inverted->addAction(new DropTable($command->getTable())); |
77
|
|
|
break; |
78
|
|
|
|
79
|
|
|
case $command instanceof RenameTable: |
80
|
|
|
$inverted->addAction(new RenameTable(new Table($command->getNewName()), $command->getTable()->getName())); |
81
|
|
|
break; |
82
|
|
|
|
83
|
|
|
case $command instanceof AddColumn: |
84
|
|
|
$inverted->addAction(new RemoveColumn($command->getTable(), $command->getColumn())); |
85
|
|
|
break; |
86
|
|
|
|
87
|
|
|
case $command instanceof RenameColumn: |
88
|
|
|
$column = clone $command->getColumn(); |
89
|
|
|
$name = $column->getName(); |
90
|
|
|
$column->setName($command->getNewName()); |
91
|
|
|
$inverted->addAction(new RenameColumn($command->getTable(), $column, $name)); |
92
|
|
|
break; |
93
|
|
|
|
94
|
|
|
case $command instanceof AddIndex: |
95
|
4 |
|
$inverted->addAction(new DropIndex($command->getTable(), $command->getIndex())); |
96
|
|
|
break; |
97
|
4 |
|
|
98
|
4 |
|
case $command instanceof AddForeignKey: |
99
|
|
|
$inverted->addAction(new DropForeignKey($command->getTable(), $command->getForeignKey())); |
100
|
|
|
break; |
101
|
|
|
|
102
|
|
|
default: |
103
|
4 |
|
throw new IrreversibleMigrationException(sprintf( |
104
|
|
|
'Cannot reverse a "%s" command', |
105
|
4 |
|
get_class($command) |
106
|
4 |
|
)); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $inverted; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Execute the recorded commands in reverse. |
115
|
|
|
* |
116
|
|
|
* @return void |
117
|
|
|
*/ |
118
|
|
|
public function executeInvertedCommands() |
119
|
|
|
{ |
120
|
|
|
$plan = new Plan($this->getInvertedCommands()); |
121
|
|
|
$plan->executeInverse($this->getAdapter()); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|