Download the PHP package websvc/yii2migration without Composer
On this page you can find all versions of the php package websvc/yii2migration. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package yii2migration
yii2-migration
Disclaimer
This is a fork of https://github.com/bizley/yii2-migration - All credits to Bizley!
This is mostly for self use, and the work here is based on branch 3.x on top of version 3.6.5
Changes here add init() function to templates in order to specify the DB connection to use when executing migrations.
This allows to use the default Yii migration tool without specifying DB connection in command
What's tested
Methods: create and create-all
Migration creator and updater
Generates migration file based on the existing database table and previous migrations.
Installation
Add the package to your composer.json:
{
"require": {
"websvc/yii2migration": "^3.7"
}
}
and run composer update
or alternatively run composer require websvc/yii2migration:^3.7
Other versions
version constraint | PHP requirements | Yii requirements |
---|---|---|
^4.0 | >= 7.2 | >= 2.0.19 |
^2.9 | < 7.1 | 2.0.13 to track non-unique indexes, 2.0.14 to handle TINYINT and JSON type columns. |
Configuration
Add the following in your configuration file (preferably console configuration file):
'components' => [
// ...
],
'controllerMap' => [
'migration' => [
'class' => 'websvc\yii2migration\controllers\MigrationController',
],
],
Usage
The following console command are available:
-
List all the tables in the database:
php yii migration
or
php yii migration/list
-
Generate migration to create DB table
table_name
:php yii migration/create table_name
-
Generate migrations to create all DB tables:
php yii migration/create-all
-
Generate migration to update DB table
table_name
:php yii migration/update table_name
-
Generate migrations to update all DB tables:
php yii migration/update-all
You can generate multiple migrations for many tables at once by separating the names with a comma:
php yii migration/create table_name1,table_name2,table_name3
Starting from version 3.4/2.7 creating multiple table migrations at once forces the proper migration order based on the presence of the foreign keys. When tables are cross-referenced the additional foreign keys migration is generated at the end of default generation.
Updating migration
Starting with yii2-migration v2.0 it is possible to generate updating migration for database table.
- History of applied migrations is scanned to gather all modifications made to the table.
- Virtual table schema is prepared and compared with current table schema.
- Differences are generated as update migration.
- In case of migration history not keeping information about the table creating migration is generated.
Command line parameters
command | alias | description |
---|---|---|
db |
Application component's ID of the DB connection to use when generating migrations. default: 'db' |
|
migrationPath |
p |
Directory storing the migration classes. default: '@app/migrations' |
migrationNamespace |
n |
Namespace in case of generating namespaced migration. default: null |
templateFile |
F |
Template file for generating create migrations. default: '@websvc/yii2migration/views/create_migration.php' |
templateFileUpdate |
U |
Template file for generating update migrations. default: '@websvc/yii2migration/views/update_migration.php' |
useTablePrefix |
P |
Whether the table names generated should consider the tablePrefix setting of the DB connection. default: 1 |
migrationTable |
t |
Name of the table for keeping applied migration information. default: '{{%migration}}' |
showOnly |
s |
Whether to only display changes instead of generating update migration. default: 0 |
generalSchema |
g |
Whether to use general column schema instead of database specific (see [1] below). default: 1 |
fixHistory |
h |
Whether to add migration history entry when migration is generated. default: 0 |
skipMigrations |
List of migrations from the history table that should be skipped during the update process (see [2] below). default: [] |
|
tableOptionsInit |
O |
String rendered in the create migration template to initialize table options. default: $tableOptions = null; if ($this->db->driverName === 'mysql') { $tableOptions = 'CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE=InnoDB'; } |
tableOptions |
o |
String rendered in the create migration template for table options. default: $tableOptions |
excludeTables |
List of tables that should be skipped for *-all actions. default: [] |
|
templateFileForeignKey |
K |
Template file for generating create foreign keys migrations. default: '@websvc/yii2migration/views/create_fk_migration.php' |
[1] Remember that with different database types general column schemas may be generated with different length.
MySQL examples:
Column
varchar(255)
generalSchema=0:$this->string(255)
generalSchema=1:$this->string()
Column
int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY
generalSchema=0:$this->integer(11)->notNull()->append('AUTO_INCREMENT PRIMARY KEY')
generalSchema=1:$this->primaryKey()
Since 3.6/2.9 when column size is different from DBMS' default it's kept:
Columnvarchar(45)
generalSchema=0:$this->string(45)
generalSchema=1:$this->string(45)
[2] Here you can place migrations containing actions that can not be covered by extractor i.e. when there is a migration setting the RBAC hierarchy with authManager component. Such actions should be kept in separated migration and placed on this list to prevent them from being run during the extraction process.
Renaming
When you rename table or column remember to generate appropriate migration manually otherwise this extension will not generate updating migration (in case of the table) or will generate migration with command to drop original column and add renamed one (in case of the column). This is happening because yii2-migration can only compare two states of the table without the knowledge of how one state turned into another. And while the very result of migration renaming the column and the one dropping it and adding another is the same in terms of structure, the latter makes you lose data.
Once you add renaming migration to the history it's being tracked by the extension.
Notes
This extension should work with all database types supported in Yii 2 core:
- CUBRID (9.3.x and higher)
- MS SQL Server (2008 and above)
- MySQL (4.1.x and 5.x)
- Oracle
- PostgreSQL (9.x and above)
- SQLite (2/3)
Yii 2 limitations:
- version 2.0.13 is required to track non-unique indexes,
- version 2.0.14 is required to handle TINYINT and JSON type columns.
Only history of migrations extending yii\db\Migration
class can be properly scanned and only changes applied with
default yii\db\Migration
methods can be recognised (with the exception of execute()
, addCommentOnTable()
and
dropCommentFromTable()
methods). Changes made to table's data (like insert()
, upsert()
, delete()
, truncate()
,
etc.) are not tracked.
Updating migrations process requires for methods createTable()
, addColumn()
, and alterColumn()
to provide changes
in columns definition in form of an instance of yii\db\ColumnSchemaBuilder
(like $this->string()
instead of 'varchar(255)'
).
Tests
Tests for MySQL, PostgreSQL, and SQLite are provided. Database configuration is stored in tests/config.php
(you can override it by
creating config.local.php
file there).