/
Migration.php
167 lines (158 loc) · 6.37 KB
/
Migration.php
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
namespace nkovacs\tablebuilder;
use yii\base\InvalidConfigException;
class Migration extends \yii\db\Migration
{
/**
* @var string|boolean|null table options used in createTable.
* If true, this will be set to ENGINE=InnoDB if the db driver is mysql.
* Set it to null to disable this behavior.
*/
protected $tableOptions = true;
/**
* Initializes the migration.
* This method will set tableOptions to use InnoDB if the db driver is mysql.
*/
public function init()
{
parent::init();
if ($this->tableOptions === true) {
if ($this->db->driverName === 'mysql') {
$this->tableOptions = 'ENGINE=InnoDB';
} else {
$this->tableOptions = '';
}
}
}
/**
* Returns a foreign key name in the form "fk_$tableName__$columnName".
* @return string
*/
public function makeFkName($tableName, $columnName)
{
$tableName = $this->db->schema->getRawTableName($tableName);
return 'fk_' . $tableName . '__' . $columnName;
}
/**
* Returns a primary key name in the form "pk_$tableName".
* This is used for compound primary keys.
* @return string
*/
public function makePkName($tableName)
{
$tableName = $this->db->schema->getRawTableName($tableName);
return 'pk_' . $tableName;
}
/**
* Builds tables specified in $tables.
* Each element of $tables is a table definition array indexed by the table name.
* The elements of a table definition can be simple column types or arrays.
* Columns that are arrays are foreign keys, and should be in the following format:
* ['column definition', 'ref table', 'ref column', 'delete' => 'on delete option', 'update' => 'on update option']
* 'delete' and 'update' are optional, and default to null.
* Referenced tables can be specified in any order, and can be tables that already exist.
* Foreign keys are named automatically using makeFkName.
* Compound primary keys can be specified using an array element in the table definition like this:
* ['primary' => ['column1', 'column2']]
* or
* ['primary' => 'column1, column2']
*
* If an error occurs, build will attempt to return the database to a consistent state
* by dropping all foreign keys and tables that have already been created.
* @param array $tables array of tables to build.
*/
public function build($tables)
{
$keys = [];
$completedTables = [];
$completedKeys = [];
try {
foreach ($tables as $tableName => $config) {
$primary = false;
$columns = [];
foreach ($config as $columnName => $column) {
if (is_array($column)) {
if (array_key_exists('primary', $column)) {
if (is_array($column['primary'])) {
$primary = implode(',', $column['primary']);
} else {
$primary = $column['primary'];
}
continue;
}
if (!array_key_exists(0, $column)) {
throw new InvalidConfigException("Type missing in $tableName($columnName)");
}
if (!array_key_exists(1, $column)) {
throw new InvalidConfigException("Related table missing in $tableName($columnName)");
}
if (!array_key_exists(2, $column)) {
throw new InvalidConfigException("Related column missing in $tableName($columnName)");
}
$columns[$columnName] = $column[0];
$keys[] = [
'name' => self::makeFkName($tableName, $columnName),
'table' => $tableName,
'column' => $columnName,
'relTable' => $column[1],
'relColumn' => $column[2],
'delete' => isset($column['delete']) ? $column['delete'] : null,
'update' => isset($column['update']) ? $column['update'] : null,
];
} else {
$columns[$columnName] = $column;
}
}
$this->createTable($tableName, $columns, $this->tableOptions);
$completedTables[] = $tableName;
if ($primary !== false) {
$this->addPrimaryKey($this->makePkName($tableName), $tableName, $primary);
}
}
foreach ($keys as $key) {
$this->addForeignKey(
$key['name'],
$key['table'],
$key['column'],
$key['relTable'],
$key['relColumn'],
$key['delete'],
$key['update']
);
$completedKeys[] = [$key['name'], $key['table']];
}
} catch (\Exception $e) {
echo "\nCaught exception, cleaning up\n";
try {
foreach ($completedKeys as $key) {
$this->dropForeignKey($key[0], $key[1]);
}
foreach ($completedTables as $table) {
$this->dropTable($table);
}
} catch (\Exception $e) {
echo "Failed to clean up.\n";
echo $e->getMessage();
}
throw $e;
}
}
/**
* Drops tables specified in $tables.
* Foreign keys defined in $tables are dropped before the tables themselves.
* @param array $tables see build
*/
public function teardown($tables)
{
foreach ($tables as $tableName => $config) {
foreach ($config as $columnName => $column) {
if (is_array($column) && !array_key_exists('primary', $column)) {
$this->dropForeignKey($this->makeFkName($tableName, $columnName), $tableName);
}
}
}
foreach ($tables as $tableName => $config) {
$this->dropTable($tableName);
}
}
}