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