1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license http://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\db; |
9
|
|
|
|
10
|
|
|
use yii\base\Component; |
11
|
|
|
use yii\di\Instance; |
12
|
|
|
use yii\helpers\StringHelper; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Migration is the base class for representing a database migration. |
16
|
|
|
* |
17
|
|
|
* Migration is designed to be used together with the "yii migrate" command. |
18
|
|
|
* |
19
|
|
|
* Each child class of Migration represents an individual database migration which |
20
|
|
|
* is identified by the child class name. |
21
|
|
|
* |
22
|
|
|
* Within each migration, the [[up()]] method should be overridden to contain the logic |
23
|
|
|
* for "upgrading" the database; while the [[down()]] method for the "downgrading" |
24
|
|
|
* logic. The "yii migrate" command manages all available migrations in an application. |
25
|
|
|
* |
26
|
|
|
* If the database supports transactions, you may also override [[safeUp()]] and |
27
|
|
|
* [[safeDown()]] so that if anything wrong happens during the upgrading or downgrading, |
28
|
|
|
* the whole migration can be reverted in a whole. |
29
|
|
|
* |
30
|
|
|
* Note that some DB queries in some DBMS cannot be put into a transaction. For some examples, |
31
|
|
|
* please refer to [implicit commit](http://dev.mysql.com/doc/refman/5.7/en/implicit-commit.html). If this is the case, |
32
|
|
|
* you should still implement `up()` and `down()`, instead. |
33
|
|
|
* |
34
|
|
|
* Migration provides a set of convenient methods for manipulating database data and schema. |
35
|
|
|
* For example, the [[insert()]] method can be used to easily insert a row of data into |
36
|
|
|
* a database table; the [[createTable()]] method can be used to create a database table. |
37
|
|
|
* Compared with the same methods in [[Command]], these methods will display extra |
38
|
|
|
* information showing the method parameters and execution time, which may be useful when |
39
|
|
|
* applying migrations. |
40
|
|
|
* |
41
|
|
|
* For more details and usage information on Migration, see the [guide article on Migration](guide:db-migrations). |
42
|
|
|
* |
43
|
|
|
* @author Qiang Xue <[email protected]> |
44
|
|
|
* @since 2.0 |
45
|
|
|
*/ |
46
|
|
|
class Migration extends Component implements MigrationInterface |
47
|
|
|
{ |
48
|
|
|
use SchemaBuilderTrait; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var Connection|array|string the DB connection object or the application component ID of the DB connection |
52
|
|
|
* that this migration should work with. Starting from version 2.0.2, this can also be a configuration array |
53
|
|
|
* for creating the object. |
54
|
|
|
* |
55
|
|
|
* Note that when a Migration object is created by the `migrate` command, this property will be overwritten |
56
|
|
|
* by the command. If you do not want to use the DB connection provided by the command, you may override |
57
|
|
|
* the [[init()]] method like the following: |
58
|
|
|
* |
59
|
|
|
* ```php |
60
|
|
|
* public function init() |
61
|
|
|
* { |
62
|
|
|
* $this->db = 'db2'; |
63
|
|
|
* parent::init(); |
64
|
|
|
* } |
65
|
|
|
* ``` |
66
|
|
|
*/ |
67
|
|
|
public $db = 'db'; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @var int max number of characters of the SQL outputted. Useful for reduction of long statements and making |
71
|
|
|
* console output more compact. |
72
|
|
|
* @since 2.0.13 |
73
|
|
|
*/ |
74
|
|
|
public $maxSqlOutputLength; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @var bool indicates whether the console output should be compacted. |
78
|
|
|
* If this is set to true, the individual commands ran within the migration will not be output to the console. |
79
|
|
|
* Default is false, in other words the output is fully verbose by default. |
80
|
|
|
* @since 2.0.13 |
81
|
|
|
*/ |
82
|
|
|
public $compact = false; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Initializes the migration. |
86
|
|
|
* This method will set [[db]] to be the 'db' application component, if it is `null`. |
87
|
|
|
*/ |
88
|
21 |
|
public function init() |
89
|
|
|
{ |
90
|
21 |
|
parent::init(); |
91
|
21 |
|
$this->db = Instance::ensure($this->db, Connection::class); |
92
|
21 |
|
$this->db->getSchema()->refresh(); |
93
|
21 |
|
$this->db->enableSlaves = false; |
94
|
21 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @inheritdoc |
98
|
|
|
* @since 2.0.6 |
99
|
|
|
*/ |
100
|
7 |
|
protected function getDb() |
101
|
|
|
{ |
102
|
7 |
|
return $this->db; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* This method contains the logic to be executed when applying this migration. |
107
|
|
|
* Child classes may override this method to provide actual migration logic. |
108
|
|
|
* @return bool return a false value to indicate the migration fails |
109
|
|
|
* and should not proceed further. All other return values mean the migration succeeds. |
110
|
|
|
*/ |
111
|
1 |
|
public function up() |
112
|
|
|
{ |
113
|
1 |
|
$transaction = $this->db->beginTransaction(); |
114
|
|
|
try { |
115
|
1 |
|
if ($this->safeUp() === false) { |
116
|
|
|
$transaction->rollBack(); |
117
|
|
|
return false; |
118
|
|
|
} |
119
|
1 |
|
$transaction->commit(); |
120
|
|
|
} catch (\Throwable $e) { |
121
|
|
|
$this->printException($e); |
122
|
|
|
$transaction->rollBack(); |
123
|
|
|
return false; |
124
|
|
|
} |
125
|
|
|
|
126
|
1 |
|
return null; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* This method contains the logic to be executed when removing this migration. |
131
|
|
|
* The default implementation throws an exception indicating the migration cannot be removed. |
132
|
|
|
* Child classes may override this method if the corresponding migrations can be removed. |
133
|
|
|
* @return bool return a false value to indicate the migration fails |
134
|
|
|
* and should not proceed further. All other return values mean the migration succeeds. |
135
|
|
|
*/ |
136
|
|
|
public function down() |
137
|
|
|
{ |
138
|
|
|
$transaction = $this->db->beginTransaction(); |
139
|
|
|
try { |
140
|
|
|
if ($this->safeDown() === false) { |
141
|
|
|
$transaction->rollBack(); |
142
|
|
|
return false; |
143
|
|
|
} |
144
|
|
|
$transaction->commit(); |
145
|
|
|
} catch (\Throwable $e) { |
146
|
|
|
$this->printException($e); |
147
|
|
|
$transaction->rollBack(); |
148
|
|
|
return false; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return null; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param \Throwable $e |
156
|
|
|
*/ |
157
|
|
|
private function printException($e) |
158
|
|
|
{ |
159
|
|
|
echo 'Exception: ' . $e->getMessage() . ' (' . $e->getFile() . ':' . $e->getLine() . ")\n"; |
160
|
|
|
echo $e->getTraceAsString() . "\n"; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* This method contains the logic to be executed when applying this migration. |
165
|
|
|
* This method differs from [[up()]] in that the DB logic implemented here will |
166
|
|
|
* be enclosed within a DB transaction. |
167
|
|
|
* Child classes may implement this method instead of [[up()]] if the DB logic |
168
|
|
|
* needs to be within a transaction. |
169
|
|
|
* |
170
|
|
|
* Note: Not all DBMS support transactions. And some DB queries cannot be put into a transaction. For some examples, |
171
|
|
|
* please refer to [implicit commit](http://dev.mysql.com/doc/refman/5.7/en/implicit-commit.html). If this is the case, |
172
|
|
|
* you should still implement `up()` and `down()`, instead. |
173
|
|
|
* |
174
|
|
|
* @return bool return a false value to indicate the migration fails |
175
|
|
|
* and should not proceed further. All other return values mean the migration succeeds. |
176
|
|
|
*/ |
177
|
|
|
public function safeUp() |
178
|
|
|
{ |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* This method contains the logic to be executed when removing this migration. |
183
|
|
|
* This method differs from [[down()]] in that the DB logic implemented here will |
184
|
|
|
* be enclosed within a DB transaction. |
185
|
|
|
* Child classes may implement this method instead of [[down()]] if the DB logic |
186
|
|
|
* needs to be within a transaction. |
187
|
|
|
* |
188
|
|
|
* Note: Not all DBMS support transactions. And some DB queries cannot be put into a transaction. For some examples, |
189
|
|
|
* please refer to [implicit commit](http://dev.mysql.com/doc/refman/5.7/en/implicit-commit.html). If this is the case, |
190
|
|
|
* you should still implement `up()` and `down()`, instead. |
191
|
|
|
* |
192
|
|
|
* @return bool return a false value to indicate the migration fails |
193
|
|
|
* and should not proceed further. All other return values mean the migration succeeds. |
194
|
|
|
*/ |
195
|
|
|
public function safeDown() |
196
|
|
|
{ |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Executes a SQL statement. |
201
|
|
|
* This method executes the specified SQL statement using [[db]]. |
202
|
|
|
* @param string $sql the SQL statement to be executed |
203
|
|
|
* @param array $params input parameters (name => value) for the SQL execution. |
204
|
|
|
* See [[Command::execute()]] for more details. |
205
|
|
|
*/ |
206
|
|
|
public function execute($sql, $params = []) |
207
|
|
|
{ |
208
|
|
|
$sqlOutput = $sql; |
209
|
|
|
if ($this->maxSqlOutputLength !== null) { |
210
|
|
|
$sqlOutput = StringHelper::truncate($sql, $this->maxSqlOutputLength, '[... hidden]'); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
$time = $this->beginCommand("execute SQL: $sqlOutput"); |
214
|
|
|
$this->db->createCommand($sql)->bindValues($params)->execute(); |
215
|
|
|
$this->endCommand($time); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Creates and executes an INSERT SQL statement. |
220
|
|
|
* The method will properly escape the column names, and bind the values to be inserted. |
221
|
|
|
* @param string $table the table that new rows will be inserted into. |
222
|
|
|
* @param array $columns the column data (name => value) to be inserted into the table. |
223
|
|
|
*/ |
224
|
|
|
public function insert($table, $columns) |
225
|
|
|
{ |
226
|
|
|
$time = $this->beginCommand("insert into $table"); |
227
|
|
|
$this->db->createCommand()->insert($table, $columns)->execute(); |
228
|
|
|
$this->endCommand($time); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Creates and executes an batch INSERT SQL statement. |
233
|
|
|
* The method will properly escape the column names, and bind the values to be inserted. |
234
|
|
|
* @param string $table the table that new rows will be inserted into. |
235
|
|
|
* @param array $columns the column names. |
236
|
|
|
* @param array $rows the rows to be batch inserted into the table |
237
|
|
|
*/ |
238
|
|
|
public function batchInsert($table, $columns, $rows) |
239
|
|
|
{ |
240
|
|
|
$time = $this->beginCommand("insert into $table"); |
241
|
|
|
$this->db->createCommand()->batchInsert($table, $columns, $rows)->execute(); |
242
|
|
|
$this->endCommand($time); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Creates and executes an UPDATE SQL statement. |
247
|
|
|
* The method will properly escape the column names and bind the values to be updated. |
248
|
|
|
* @param string $table the table to be updated. |
249
|
|
|
* @param array $columns the column data (name => value) to be updated. |
250
|
|
|
* @param array|string $condition the conditions that will be put in the WHERE part. Please |
251
|
|
|
* refer to [[Query::where()]] on how to specify conditions. |
252
|
|
|
* @param array $params the parameters to be bound to the query. |
253
|
|
|
*/ |
254
|
|
|
public function update($table, $columns, $condition = '', $params = []) |
255
|
|
|
{ |
256
|
|
|
$time = $this->beginCommand("update $table"); |
257
|
|
|
$this->db->createCommand()->update($table, $columns, $condition, $params)->execute(); |
258
|
|
|
$this->endCommand($time); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Creates and executes a DELETE SQL statement. |
263
|
|
|
* @param string $table the table where the data will be deleted from. |
264
|
|
|
* @param array|string $condition the conditions that will be put in the WHERE part. Please |
265
|
|
|
* refer to [[Query::where()]] on how to specify conditions. |
266
|
|
|
* @param array $params the parameters to be bound to the query. |
267
|
|
|
*/ |
268
|
|
|
public function delete($table, $condition = '', $params = []) |
269
|
|
|
{ |
270
|
|
|
$time = $this->beginCommand("delete from $table"); |
271
|
|
|
$this->db->createCommand()->delete($table, $condition, $params)->execute(); |
272
|
|
|
$this->endCommand($time); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Builds and executes a SQL statement for creating a new DB table. |
277
|
|
|
* |
278
|
|
|
* The columns in the new table should be specified as name-definition pairs (e.g. 'name' => 'string'), |
279
|
|
|
* where name stands for a column name which will be properly quoted by the method, and definition |
280
|
|
|
* stands for the column type which can contain an abstract DB type. |
281
|
|
|
* |
282
|
|
|
* The [[QueryBuilder::getColumnType()]] method will be invoked to convert any abstract type into a physical one. |
283
|
|
|
* |
284
|
|
|
* If a column is specified with definition only (e.g. 'PRIMARY KEY (name, type)'), it will be directly |
285
|
|
|
* put into the generated SQL. |
286
|
|
|
* |
287
|
|
|
* @param string $table the name of the table to be created. The name will be properly quoted by the method. |
288
|
|
|
* @param array $columns the columns (name => definition) in the new table. |
289
|
|
|
* @param string $options additional SQL fragment that will be appended to the generated SQL. |
290
|
|
|
*/ |
291
|
7 |
|
public function createTable($table, $columns, $options = null) |
292
|
|
|
{ |
293
|
7 |
|
$time = $this->beginCommand("create table $table"); |
294
|
7 |
|
$this->db->createCommand()->createTable($table, $columns, $options)->execute(); |
295
|
7 |
|
foreach ($columns as $column => $type) { |
296
|
7 |
|
if ($type instanceof ColumnSchemaBuilder && $type->comment !== null) { |
297
|
7 |
|
$this->db->createCommand()->addCommentOnColumn($table, $column, $type->comment)->execute(); |
298
|
|
|
} |
299
|
|
|
} |
300
|
7 |
|
$this->endCommand($time); |
301
|
7 |
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Builds and executes a SQL statement for renaming a DB table. |
305
|
|
|
* @param string $table the table to be renamed. The name will be properly quoted by the method. |
306
|
|
|
* @param string $newName the new table name. The name will be properly quoted by the method. |
307
|
|
|
*/ |
308
|
|
|
public function renameTable($table, $newName) |
309
|
|
|
{ |
310
|
|
|
$time = $this->beginCommand("rename table $table to $newName"); |
311
|
|
|
$this->db->createCommand()->renameTable($table, $newName)->execute(); |
312
|
|
|
$this->endCommand($time); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* Builds and executes a SQL statement for dropping a DB table. |
317
|
|
|
* @param string $table the table to be dropped. The name will be properly quoted by the method. |
318
|
|
|
*/ |
319
|
7 |
|
public function dropTable($table) |
320
|
|
|
{ |
321
|
7 |
|
$time = $this->beginCommand("drop table $table"); |
322
|
7 |
|
$this->db->createCommand()->dropTable($table)->execute(); |
323
|
7 |
|
$this->endCommand($time); |
324
|
7 |
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* Builds and executes a SQL statement for truncating a DB table. |
328
|
|
|
* @param string $table the table to be truncated. The name will be properly quoted by the method. |
329
|
|
|
*/ |
330
|
|
|
public function truncateTable($table) |
331
|
|
|
{ |
332
|
|
|
$time = $this->beginCommand("truncate table $table"); |
333
|
|
|
$this->db->createCommand()->truncateTable($table)->execute(); |
334
|
|
|
$this->endCommand($time); |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* Builds and executes a SQL statement for adding a new DB column. |
339
|
|
|
* @param string $table the table that the new column will be added to. The table name will be properly quoted by the method. |
340
|
|
|
* @param string $column the name of the new column. The name will be properly quoted by the method. |
341
|
|
|
* @param string $type the column type. The [[QueryBuilder::getColumnType()]] method will be invoked to convert abstract column type (if any) |
342
|
|
|
* into the physical one. Anything that is not recognized as abstract type will be kept in the generated SQL. |
343
|
|
|
* For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become 'varchar(255) not null'. |
344
|
|
|
*/ |
345
|
|
|
public function addColumn($table, $column, $type) |
346
|
|
|
{ |
347
|
|
|
$time = $this->beginCommand("add column $column $type to table $table"); |
348
|
|
|
$this->db->createCommand()->addColumn($table, $column, $type)->execute(); |
349
|
|
|
if ($type instanceof ColumnSchemaBuilder && $type->comment !== null) { |
350
|
|
|
$this->db->createCommand()->addCommentOnColumn($table, $column, $type->comment)->execute(); |
351
|
|
|
} |
352
|
|
|
$this->endCommand($time); |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* Builds and executes a SQL statement for dropping a DB column. |
357
|
|
|
* @param string $table the table whose column is to be dropped. The name will be properly quoted by the method. |
358
|
|
|
* @param string $column the name of the column to be dropped. The name will be properly quoted by the method. |
359
|
|
|
*/ |
360
|
|
|
public function dropColumn($table, $column) |
361
|
|
|
{ |
362
|
|
|
$time = $this->beginCommand("drop column $column from table $table"); |
363
|
|
|
$this->db->createCommand()->dropColumn($table, $column)->execute(); |
364
|
|
|
$this->endCommand($time); |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* Builds and executes a SQL statement for renaming a column. |
369
|
|
|
* @param string $table the table whose column is to be renamed. The name will be properly quoted by the method. |
370
|
|
|
* @param string $name the old name of the column. The name will be properly quoted by the method. |
371
|
|
|
* @param string $newName the new name of the column. The name will be properly quoted by the method. |
372
|
|
|
*/ |
373
|
|
|
public function renameColumn($table, $name, $newName) |
374
|
|
|
{ |
375
|
|
|
$time = $this->beginCommand("rename column $name in table $table to $newName"); |
376
|
|
|
$this->db->createCommand()->renameColumn($table, $name, $newName)->execute(); |
377
|
|
|
$this->endCommand($time); |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
/** |
381
|
|
|
* Builds and executes a SQL statement for changing the definition of a column. |
382
|
|
|
* @param string $table the table whose column is to be changed. The table name will be properly quoted by the method. |
383
|
|
|
* @param string $column the name of the column to be changed. The name will be properly quoted by the method. |
384
|
|
|
* @param string $type the new column type. The [[QueryBuilder::getColumnType()]] method will be invoked to convert abstract column type (if any) |
385
|
|
|
* into the physical one. Anything that is not recognized as abstract type will be kept in the generated SQL. |
386
|
|
|
* For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become 'varchar(255) not null'. |
387
|
|
|
*/ |
388
|
|
|
public function alterColumn($table, $column, $type) |
389
|
|
|
{ |
390
|
|
|
$time = $this->beginCommand("alter column $column in table $table to $type"); |
391
|
|
|
$this->db->createCommand()->alterColumn($table, $column, $type)->execute(); |
392
|
|
|
if ($type instanceof ColumnSchemaBuilder && $type->comment !== null) { |
393
|
|
|
$this->db->createCommand()->addCommentOnColumn($table, $column, $type->comment)->execute(); |
394
|
|
|
} |
395
|
|
|
$this->endCommand($time); |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* Builds and executes a SQL statement for creating a primary key. |
400
|
|
|
* The method will properly quote the table and column names. |
401
|
|
|
* @param string $name the name of the primary key constraint. |
402
|
|
|
* @param string $table the table that the primary key constraint will be added to. |
403
|
|
|
* @param string|array $columns comma separated string or array of columns that the primary key will consist of. |
404
|
|
|
*/ |
405
|
|
|
public function addPrimaryKey($name, $table, $columns) |
406
|
|
|
{ |
407
|
|
|
$time = $this->beginCommand("add primary key $name on $table (" . (is_array($columns) ? implode(',', $columns) : $columns) . ')'); |
408
|
|
|
$this->db->createCommand()->addPrimaryKey($name, $table, $columns)->execute(); |
409
|
|
|
$this->endCommand($time); |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
/** |
413
|
|
|
* Builds and executes a SQL statement for dropping a primary key. |
414
|
|
|
* @param string $name the name of the primary key constraint to be removed. |
415
|
|
|
* @param string $table the table that the primary key constraint will be removed from. |
416
|
|
|
*/ |
417
|
|
|
public function dropPrimaryKey($name, $table) |
418
|
|
|
{ |
419
|
|
|
$time = $this->beginCommand("drop primary key $name"); |
420
|
|
|
$this->db->createCommand()->dropPrimaryKey($name, $table)->execute(); |
421
|
|
|
$this->endCommand($time); |
422
|
|
|
} |
423
|
|
|
|
424
|
|
|
/** |
425
|
|
|
* Builds a SQL statement for adding a foreign key constraint to an existing table. |
426
|
|
|
* The method will properly quote the table and column names. |
427
|
|
|
* @param string $name the name of the foreign key constraint. |
428
|
|
|
* @param string $table the table that the foreign key constraint will be added to. |
429
|
|
|
* @param string|array $columns the name of the column to that the constraint will be added on. If there are multiple columns, separate them with commas or use an array. |
430
|
|
|
* @param string $refTable the table that the foreign key references to. |
431
|
|
|
* @param string|array $refColumns the name of the column that the foreign key references to. If there are multiple columns, separate them with commas or use an array. |
432
|
|
|
* @param string $delete the ON DELETE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
433
|
|
|
* @param string $update the ON UPDATE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
434
|
|
|
*/ |
435
|
|
|
public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null) |
436
|
|
|
{ |
437
|
|
|
$time = $this->beginCommand("add foreign key $name: $table (" . implode(',', (array) $columns) . ") references $refTable (" . implode(',', (array) $refColumns) . ')'); |
438
|
|
|
$this->db->createCommand()->addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete, $update)->execute(); |
439
|
|
|
$this->endCommand($time); |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
/** |
443
|
|
|
* Builds a SQL statement for dropping a foreign key constraint. |
444
|
|
|
* @param string $name the name of the foreign key constraint to be dropped. The name will be properly quoted by the method. |
445
|
|
|
* @param string $table the table whose foreign is to be dropped. The name will be properly quoted by the method. |
446
|
|
|
*/ |
447
|
|
|
public function dropForeignKey($name, $table) |
448
|
|
|
{ |
449
|
|
|
$time = $this->beginCommand("drop foreign key $name from table $table"); |
450
|
|
|
$this->db->createCommand()->dropForeignKey($name, $table)->execute(); |
451
|
|
|
$this->endCommand($time); |
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
/** |
455
|
|
|
* Builds and executes a SQL statement for creating a new index. |
456
|
|
|
* @param string $name the name of the index. The name will be properly quoted by the method. |
457
|
|
|
* @param string $table the table that the new index will be created for. The table name will be properly quoted by the method. |
458
|
|
|
* @param string|array $columns the column(s) that should be included in the index. If there are multiple columns, please separate them |
459
|
|
|
* by commas or use an array. Each column name will be properly quoted by the method. Quoting will be skipped for column names that |
460
|
|
|
* include a left parenthesis "(". |
461
|
|
|
* @param bool $unique whether to add UNIQUE constraint on the created index. |
462
|
|
|
*/ |
463
|
6 |
|
public function createIndex($name, $table, $columns, $unique = false) |
464
|
|
|
{ |
465
|
6 |
|
$time = $this->beginCommand('create' . ($unique ? ' unique' : '') . " index $name on $table (" . implode(',', (array) $columns) . ')'); |
466
|
6 |
|
$this->db->createCommand()->createIndex($name, $table, $columns, $unique)->execute(); |
467
|
6 |
|
$this->endCommand($time); |
468
|
6 |
|
} |
469
|
|
|
|
470
|
|
|
/** |
471
|
|
|
* Builds and executes a SQL statement for dropping an index. |
472
|
|
|
* @param string $name the name of the index to be dropped. The name will be properly quoted by the method. |
473
|
|
|
* @param string $table the table whose index is to be dropped. The name will be properly quoted by the method. |
474
|
|
|
*/ |
475
|
|
|
public function dropIndex($name, $table) |
476
|
|
|
{ |
477
|
|
|
$time = $this->beginCommand("drop index $name on $table"); |
478
|
|
|
$this->db->createCommand()->dropIndex($name, $table)->execute(); |
479
|
|
|
$this->endCommand($time); |
480
|
|
|
} |
481
|
|
|
|
482
|
|
|
/** |
483
|
|
|
* Builds and execute a SQL statement for adding comment to column. |
484
|
|
|
* |
485
|
|
|
* @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
486
|
|
|
* @param string $column the name of the column to be commented. The column name will be properly quoted by the method. |
487
|
|
|
* @param string $comment the text of the comment to be added. The comment will be properly quoted by the method. |
488
|
|
|
* @since 2.0.8 |
489
|
|
|
*/ |
490
|
|
|
public function addCommentOnColumn($table, $column, $comment) |
491
|
|
|
{ |
492
|
|
|
$time = $this->beginCommand("add comment on column $column"); |
493
|
|
|
$this->db->createCommand()->addCommentOnColumn($table, $column, $comment)->execute(); |
494
|
|
|
$this->endCommand($time); |
495
|
|
|
} |
496
|
|
|
|
497
|
|
|
/** |
498
|
|
|
* Builds a SQL statement for adding comment to table. |
499
|
|
|
* |
500
|
|
|
* @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
501
|
|
|
* @param string $comment the text of the comment to be added. The comment will be properly quoted by the method. |
502
|
|
|
* @since 2.0.8 |
503
|
|
|
*/ |
504
|
|
|
public function addCommentOnTable($table, $comment) |
505
|
|
|
{ |
506
|
|
|
$time = $this->beginCommand("add comment on table $table"); |
507
|
|
|
$this->db->createCommand()->addCommentOnTable($table, $comment)->execute(); |
508
|
|
|
$this->endCommand($time); |
509
|
|
|
} |
510
|
|
|
|
511
|
|
|
/** |
512
|
|
|
* Builds and execute a SQL statement for dropping comment from column. |
513
|
|
|
* |
514
|
|
|
* @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
515
|
|
|
* @param string $column the name of the column to be commented. The column name will be properly quoted by the method. |
516
|
|
|
* @since 2.0.8 |
517
|
|
|
*/ |
518
|
|
|
public function dropCommentFromColumn($table, $column) |
519
|
|
|
{ |
520
|
|
|
$time = $this->beginCommand("drop comment from column $column"); |
521
|
|
|
$this->db->createCommand()->dropCommentFromColumn($table, $column)->execute(); |
522
|
|
|
$this->endCommand($time); |
523
|
|
|
} |
524
|
|
|
|
525
|
|
|
/** |
526
|
|
|
* Builds a SQL statement for dropping comment from table. |
527
|
|
|
* |
528
|
|
|
* @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
529
|
|
|
* @since 2.0.8 |
530
|
|
|
*/ |
531
|
|
|
public function dropCommentFromTable($table) |
532
|
|
|
{ |
533
|
|
|
$time = $this->beginCommand("drop comment from table $table"); |
534
|
|
|
$this->db->createCommand()->dropCommentFromTable($table)->execute(); |
535
|
|
|
$this->endCommand($time); |
536
|
|
|
} |
537
|
|
|
|
538
|
|
|
/** |
539
|
|
|
* Prepares for a command to be executed, and outputs to the console |
540
|
|
|
* |
541
|
|
|
* @param string $description the description for the command, to be output to the console. |
542
|
|
|
* @return float the time before the command is executed, for the time elapsed to be calculated. |
543
|
|
|
* @since 2.0.13 |
544
|
|
|
*/ |
545
|
7 |
|
protected function beginCommand($description) |
546
|
|
|
{ |
547
|
7 |
|
if (!$this->compact) { |
548
|
7 |
|
echo " > $description ..."; |
549
|
|
|
} |
550
|
7 |
|
return microtime(true); |
551
|
|
|
} |
552
|
|
|
|
553
|
|
|
/** |
554
|
|
|
* Finalizes after the command has been executed, and outputs to the console the time elapsed |
555
|
|
|
* |
556
|
|
|
* @param float the time before the command was executed. |
557
|
|
|
* @since 2.0.13 |
558
|
|
|
*/ |
559
|
7 |
|
protected function endCommand($time) |
560
|
|
|
{ |
561
|
7 |
|
if (!$this->compact) { |
562
|
7 |
|
echo ' done (time: ' . sprintf('%.3f', microtime(true) - $time) . "s)\n"; |
563
|
|
|
} |
564
|
7 |
|
} |
565
|
|
|
} |
566
|
|
|
|