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