All Projects → bizley → Yii2 Migration

bizley / Yii2 Migration

Licence: apache-2.0
Yii 2 Migration Creator And Updater

Projects that are alternatives of or similar to Yii2 Migration

A database migration tool. Supports SQL migrations and Go functions.
Stars: ✭ 2,112 (+706.11%)
Mutual labels:  migration, database, database-migrations
Yii2 Apidoc
Yii 2 apidoc extension.
Stars: ✭ 236 (-9.92%)
Mutual labels:  hacktoberfest, yii2, generator
Main Liquibase Source
Stars: ✭ 2,910 (+1010.69%)
Mutual labels:  hacktoberfest, database-migrations, database
Faker is a pure Elixir library for generating fake data.
Stars: ✭ 673 (+156.87%)
Mutual labels:  hacktoberfest, database, generator
Laravel Sync Migration
Developer tool helps to sync migrations without refreshing the database
Stars: ✭ 89 (-66.03%)
Mutual labels:  migration, database-migrations, database
Database migrations. CLI and Golang library.
Stars: ✭ 7,712 (+2843.51%)
Mutual labels:  hacktoberfest, migration, database
Github Profilinator
🚀 This tool contains mini GUI components that you can hook together to automatically generate markdown code for a perfect readme.
Stars: ✭ 225 (-14.12%)
Mutual labels:  hacktoberfest, generator
Laravel Craftsman
Laravel Craftsman CLI for easily crafting Laravel assets for any project (artisan make on steroids)
Stars: ✭ 227 (-13.36%)
Mutual labels:  hacktoberfest, generator
🐨 - PouchDB is a pocket-sized database.
Stars: ✭ 14,625 (+5482.06%)
Mutual labels:  hacktoberfest, database
PostgreSQL ➕ REST, low-code, simplify and accelerate development, ⚡ instant, realtime, high-performance on any Postgres application, existing or new
Stars: ✭ 3,023 (+1053.82%)
Mutual labels:  hacktoberfest, database
R2DBC database migration tool & library
Stars: ✭ 83 (-68.32%)
Mutual labels:  migration, database-migrations
Migrations is a database migration tool that uses go's database/sql from the standard library
Stars: ✭ 17 (-93.51%)
Mutual labels:  migration, database-migrations
Yii 2: The Fast, Secure and Professional PHP Framework
Stars: ✭ 13,852 (+5187.02%)
Mutual labels:  hacktoberfest, yii2
Php Ulid
A PHP port of alizain/ulid with some minor improvements.
Stars: ✭ 203 (-22.52%)
Mutual labels:  hacktoberfest, generator
Yii2 Bootstrap4
Yii 2 Bootstrap 4 Extension
Stars: ✭ 204 (-22.14%)
Mutual labels:  hacktoberfest, yii2
TypeScript SQLite layer with support for both native C++ & pure JavaScript drivers.
Stars: ✭ 195 (-25.57%)
Mutual labels:  hacktoberfest, database
Kotlin Database Migration Tool. This tool makes it really easy to create table, index, add columns, and so on, with Kotlin DSL.
Stars: ✭ 123 (-53.05%)
Mutual labels:  migration, database-migrations
Stars: ✭ 35 (-86.64%)
Mutual labels:  yii2, migration
Yii2 to Laravel Migration Package
Stars: ✭ 71 (-72.9%)
Mutual labels:  yii2, migration
Yii2 REST doc generator
Stars: ✭ 35 (-86.64%)
Mutual labels:  yii2, generator

Yii 2 Migration

Yii 2 Migration

Latest Stable Version Total Downloads License Infection MSI

Migration Creator And Updater

Generates migration file based on the existing database table and previous migrations.


Run console command

composer require bizley/migration

Or add the package to your composer.json file:

    "require": {
        "bizley/migration": "^4.0"

then run composer update.


Add the following in your configuration file (preferably console configuration file):

    'controllerMap' => [
        'migration' => [
            'class' => 'bizley\migration\controllers\MigrationController',


The following console command are available (assuming you named the controller migration like in the example above):

  • List all the tables in the database:

    php yii migration


    php yii migration/list
  • Generate migration to create DB table table_name:

    php yii migration/create table_name
  • Generate migration to update DB table table_name:

    php yii migration/update table_name

To generate migrations for all the tables in the database at once (except the excluded ones) use asterisk (*):

php yii migration/create *
php yii migration/update *

In environments that hijack asterisk (like dockerized env) use "*".
You can generate multiple migrations for many tables at once by separating the names with comma:

php yii migration/create table_name1,table_name2,table_name3

You can provide an asterisk as a part of table name to use all tables matching the pattern:

php yii migration/update prefix_*

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

You can easily generate updating migration for database table by comparing its current schema with the migration history.

  1. History of applied migrations is scanned to gather all modifications made to the table.
  2. Virtual table schema is prepared and compared with current table schema.
  3. Differences are generated as update migration.
  4. In case of migration history not keeping information about the table creating migration is generated.

Command line parameters

command alias description
migrationPath mp Directory (one or more) storing the migration classes.
migrationNamespace mn Namespace (one or more) in case of generating namespaced migration.
useTablePrefix tp Whether the table names generated should consider the tablePrefix setting of the DB connection.
migrationTable mt Name of the table for keeping applied migration information.
onlyShow os Whether to only display changes instead of generating update migration.
generalSchema gs Whether to use general column schema instead of database specific (see [1] below).
fixHistory fh Whether to add migration history entry when migration is generated.
skipMigrations List of migrations from the history table that should be skipped during the update process (see [2] below).
excludeTables List of tables that should be skipped.
experimental ex New in 4.1.0 - Whether to run in experimental mode (see [3] below).

[1] Remember that with different database types general column schemas may be generated with different length.

MySQL examples:

Column varchar(255)
generalSchema=false: $this->string(255)
generalSchema=true: $this->string()

generalSchema=false: $this->integer(11)->notNull()->append('AUTO_INCREMENT PRIMARY KEY')
generalSchema=true: $this->primaryKey()

When column size is different from DBMS' default it's kept:
Column varchar(45)
generalSchema=false: $this->string(45)
generalSchema=true: $this->string(45)

[2] Here you can place migrations containing actions that cannot be covered by the 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.

[3] This mode allows using raw SQL column definition for migration updater (i.e. ['column' => 'varchar(255)'] instead of ['column' => $this->string()]). Since the generating process in this mode depends on the individual DBMS syntax the results might not be correct. All help improving this mode is more than welcome.


When you rename a table or a 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 the 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. 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.

Migrating from v2 or v3 to v4

See Migrating to version 4.0 section.


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)

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 (except for 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 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).
Docker Compose file for setting up the databases is stored in tests/docker.

Previous versions

These versions are not developed anymore but still available for all poor souls that are stuck with EOL PHP. Some of the newest features may not be available there.

version constraint PHP requirements Yii requirements
^3.6 >= 7.1 >=
^2.9 < 7.1 2.0.13 to track non-unique indexes, 2.0.14 to handle TINYINT and JSON type columns.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].