Since version 2.0.7 migration console which provides convenient way creating migrations.
Since version 2.0.7 migration console provides a convenient way to create migrations.
If the migration name is of the form "create_xxx" or "drop_xxx" then a migration creating the table xxx with the columns listed will be generated. For example:
If the migration name is of a special form including but not limited to `create_xxx` or `drop_xxx` then migration
file would contain extra code when generated.
### Create Table
```php
yii migrate/create create_post
```
generates
generates
```php
class m150811_220037_create_post extends Migration
@ -210,13 +213,13 @@ class m150811_220037_create_post extends Migration
}
```
For create column schema you may use fields option with as follows:
To create table fields right away, specify them via `--fields` option.
@ -264,10 +267,11 @@ class m150811_220037_create_post extends Migration
}
```
> Note: primary Key is added automatically, if you want to use another name for primary key, you
may specify in fields options, for example "--fields=name:primaryKey"
> Note: primary key is added automatically and is named `id` by default. If you want to use another name you may
> specify it explicitly like `--fields=name:primaryKey`.
Similarly, you can generate a migration to drop table from the command line:
### Drop Table
```php
yii migrate/create drop_post
@ -291,16 +295,21 @@ class m150811_220037_drop_post extends Migration
}
```
If the migration name is of the form "add_xxx_from_yyy" or "drop_xxx_from_yyy" then a migration containing the appropriate addColumn and dropColumn statements will be created.
### Add Column
If the migration name is of the form `add_xxx_to_yyy` then the file content would contain `addColumn` and `dropColumn`
]`), specifies template files for generating migration code automatically. See [Creating Migrations with Generators](#creating-migrations-with-generators)
]`), specifies template files for generating migration code. See "[Generating Migrations](#generating-migrations)"
for more details.
* `fields`: array (defaults to `[]`), specifies the fields column use to creating migration automatically. The format that it use when declaring any applicable schema it is
`COLUMN_NAME:COLUMN_TYPE:COLUMN_DECORATOR`, for example `--fields=name:string(12):notNull`, it specify string column with 12 size and not null.
* `fields`: array of column definition strings used for creating migration code. Defaults to `[]`. The format of each
definition is `COLUMN_NAME:COLUMN_TYPE:COLUMN_DECORATOR`. For example, `--fields=name:string(12):notNull` produces
a string column of size 12 which is not null.
The following example shows how you can use these options.