1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Phinx |
4
|
|
|
* |
5
|
|
|
* (The MIT license) |
6
|
|
|
* Copyright (c) 2015 Rob Morgan |
7
|
|
|
* |
8
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
9
|
|
|
* of this software and associated * documentation files (the "Software"), to |
10
|
|
|
* deal in the Software without restriction, including without limitation the |
11
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
12
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is |
13
|
|
|
* furnished to do so, subject to the following conditions: |
14
|
|
|
* |
15
|
|
|
* The above copyright notice and this permission notice shall be included in |
16
|
|
|
* all copies or substantial portions of the Software. |
17
|
|
|
* |
18
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
19
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
20
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
21
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
22
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
23
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
24
|
|
|
* IN THE SOFTWARE. |
25
|
|
|
* |
26
|
|
|
* @package Phinx |
27
|
|
|
* @subpackage Phinx\Db\Adapter |
28
|
|
|
*/ |
29
|
|
|
namespace Phinx\Db\Adapter; |
30
|
|
|
|
31
|
|
|
use Phinx\Db\Action\AddColumn; |
32
|
|
|
use Phinx\Db\Action\AddForeignKey; |
33
|
|
|
use Phinx\Db\Action\AddIndex; |
34
|
|
|
use Phinx\Db\Action\CreateTable; |
35
|
|
|
use Phinx\Db\Action\DropForeignKey; |
36
|
|
|
use Phinx\Db\Action\DropIndex; |
37
|
|
|
use Phinx\Db\Action\DropTable; |
38
|
|
|
use Phinx\Db\Action\RemoveColumn; |
39
|
|
|
use Phinx\Db\Action\RenameColumn; |
40
|
|
|
use Phinx\Db\Action\RenameTable; |
41
|
|
|
use Phinx\Db\Plan\Intent; |
42
|
|
|
use Phinx\Db\Plan\Plan; |
43
|
|
|
use Phinx\Db\Table\Table; |
44
|
|
|
use Phinx\Migration\IrreversibleMigrationException; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Phinx Proxy Adapter. |
48
|
|
|
* |
49
|
|
|
* Used for recording migration commands to automatically reverse them. |
50
|
|
|
* |
51
|
|
|
* @author Rob Morgan <[email protected]> |
52
|
|
|
*/ |
53
|
|
|
class ProxyAdapter extends AdapterWrapper |
54
|
|
|
{ |
55
|
|
|
/** |
56
|
|
|
* @var array |
57
|
|
|
*/ |
58
|
|
|
protected $commands = []; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
4 |
|
*/ |
63
|
|
|
public function getAdapterType() |
64
|
4 |
|
{ |
65
|
4 |
|
return 'ProxyAdapter'; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
4 |
|
*/ |
71
|
|
|
public function createTable(Table $table, array $columns = [], array $indexes = []) |
72
|
4 |
|
{ |
73
|
4 |
|
$this->commands[] = new CreateTable($table); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
|
|
public function executeActions(Table $table, array $actions) |
80
|
|
|
{ |
81
|
|
|
$this->commands = array_merge($this->commands, $actions); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Gets an array of the recorded commands in reverse. |
86
|
|
|
* |
87
|
|
|
* @throws \Phinx\Migration\IrreversibleMigrationException if a command cannot be reversed. |
88
|
|
|
* @return \Phinx\Db\Plan\Intent |
89
|
|
|
*/ |
90
|
|
|
public function getInvertedCommands() |
91
|
|
|
{ |
92
|
|
|
$inverted = new Intent(); |
93
|
|
|
|
94
|
|
|
foreach (array_reverse($this->commands) as $com) { |
95
|
4 |
|
switch (true) { |
96
|
|
|
case $com instanceof CreateTable: |
97
|
4 |
|
$inverted->addAction(new DropTable($com->getTable())); |
98
|
4 |
|
break; |
99
|
|
|
|
100
|
|
|
case $com instanceof RenameTable: |
101
|
|
|
$inverted->addAction(new RenameTable(new Table($com->getNewName()), $com->getTable()->getName())); |
102
|
|
|
break; |
103
|
4 |
|
|
104
|
|
|
case $com instanceof AddColumn: |
105
|
4 |
|
$inverted->addAction(new RemoveColumn($com->getTable(), $com->getColumn())); |
106
|
4 |
|
break; |
107
|
|
|
|
108
|
|
|
case $com instanceof RenameColumn: |
109
|
|
|
$column = clone $com->getColumn(); |
110
|
|
|
$name = $column->getName(); |
111
|
|
|
$column->setName($com->getNewName()); |
112
|
|
|
$inverted->addAction(new RenameColumn($com->getTable(), $column, $name)); |
113
|
|
|
break; |
114
|
|
|
|
115
|
|
|
case $com instanceof AddIndex: |
116
|
|
|
$inverted->addAction(new DropIndex($com->getTable(), $com->getIndex())); |
117
|
|
|
break; |
118
|
|
|
|
119
|
|
|
case $com instanceof AddForeignKey: |
120
|
|
|
$inverted->addAction(new DropForeignKey($com->getTable(), $com->getForeignKey())); |
121
|
|
|
break; |
122
|
|
|
|
123
|
|
|
default: |
124
|
|
|
throw new IrreversibleMigrationException(sprintf( |
125
|
|
|
'Cannot reverse a "%s" command', |
126
|
|
|
get_class($com) |
127
|
1 |
|
)); |
128
|
|
|
} |
129
|
1 |
|
} |
130
|
1 |
|
|
131
|
|
|
return $inverted; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Execute the recorded commands in reverse. |
136
|
|
|
* |
137
|
|
|
* @return void |
138
|
|
|
*/ |
139
|
|
|
public function executeInvertedCommands() |
140
|
|
|
{ |
141
|
|
|
$plan = new Plan($this->getInvertedCommands()); |
142
|
|
|
$plan->executeInverse($this->getAdapter()); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|