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