PHPackages                             websvc/yii2migration - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. websvc/yii2migration

ActiveYii2-extension[Database &amp; ORM](/categories/database)

websvc/yii2migration
====================

Migration generator for Yii 2.

3.7.3(5y ago)05.7k↓50%Apache-2.0PHPPHP &gt;=5.6

Since Jun 8Pushed 5y agoCompare

[ Source](https://github.com/websvcPT/yii2-migration)[ Packagist](https://packagist.org/packages/websvc/yii2migration)[ RSS](/packages/websvc-yii2migration/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (3)Versions (23)Used By (0)

yii2-migration
==============

[](#yii2-migration)

[![Latest Version](https://camo.githubusercontent.com/5ee1cd1e1e0b3b21ae581ffcb6c502868a79dc8978e4248849541ede46550c0a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7765627376632f796969326d6967726174696f6e3f7374796c653d706c6173746963)](https://camo.githubusercontent.com/5ee1cd1e1e0b3b21ae581ffcb6c502868a79dc8978e4248849541ede46550c0a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7765627376632f796969326d6967726174696f6e3f7374796c653d706c6173746963)[![Total Downloads](https://camo.githubusercontent.com/59fdc6884a485c32696e01138e6264fa73ba423f263cca0b344171dd5990c35c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7765627376632f796969326d6967726174696f6e3f7374796c653d706c6173746963)](https://camo.githubusercontent.com/59fdc6884a485c32696e01138e6264fa73ba423f263cca0b344171dd5990c35c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7765627376632f796969326d6967726174696f6e3f7374796c653d706c6173746963)[![License](https://camo.githubusercontent.com/6cd99fb6605ad550249c377f9a4b09d9bae0a4de78b9007a5c0017814c24f6e4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f7765627376632f796969326d6967726174696f6e3f7374796c653d706c6173746963)](https://camo.githubusercontent.com/6cd99fb6605ad550249c377f9a4b09d9bae0a4de78b9007a5c0017814c24f6e4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f7765627376632f796969326d6967726174696f6e3f7374796c653d706c6173746963)

Disclaimer
----------

[](#disclaimer)

This is a fork of  - 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
-----------------------------

[](#migration-creator-and-updater)

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

Installation
------------

[](#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
--------------

[](#other-versions)

version constraintPHP requirementsYii requirements^4.0&gt;= 7.2&gt;= 2.0.19^2.9&lt; 7.12.0.13 to track non-unique indexes, 2.0.14 to handle `TINYINT` and `JSON` type columns.Configuration
-------------

[](#configuration)

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

```
'components' => [
    // ...
],
'controllerMap' => [
    'migration' => [
        'class' => 'websvc\yii2migration\controllers\MigrationController',
    ],
],

```

Usage
-----

[](#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
------------------

[](#updating-migration)

Starting with yii2-migration v2.0 it is possible to generate updating migration for database table.

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-line-parameters)

commandaliasdescription`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:
>
> [](#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:
> Column `varchar(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
--------

[](#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
-----

[](#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)

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).

###  Health Score

33

—

LowBetter than 74% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity22

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~40 days

Recently: every ~14 days

Total

22

Last Release

2053d ago

PHP version history (2 changes)3.0.0PHP &gt;=7.1.0

3.7.2PHP &gt;=5.6

### Community

Maintainers

![](https://www.gravatar.com/avatar/82408143ff5ef3329c0fff88abd46366a84c7244a43e11dffe1de80f591ba904?d=identicon)[websvc](/maintainers/websvc)

---

Top Contributors

[![thyseus](https://avatars.githubusercontent.com/u/654271?v=4)](https://github.com/thyseus "thyseus (6 commits)")[![websvcPT](https://avatars.githubusercontent.com/u/1758961?v=4)](https://github.com/websvcPT "websvcPT (6 commits)")[![ecamachoh](https://avatars.githubusercontent.com/u/24883224?v=4)](https://github.com/ecamachoh "ecamachoh (1 commits)")[![kubk](https://avatars.githubusercontent.com/u/22447849?v=4)](https://github.com/kubk "kubk (1 commits)")[![MarcoMoreno](https://avatars.githubusercontent.com/u/32285738?v=4)](https://github.com/MarcoMoreno "MarcoMoreno (1 commits)")

---

Tags

consolemigrationgeneratoryii2migrateupdater

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/websvc-yii2migration/health.svg)

```
[![Health](https://phpackages.com/badges/websvc-yii2migration/health.svg)](https://phpackages.com/packages/websvc-yii2migration)
```

###  Alternatives

[bizley/migration

Migration generator for Yii 2.

297374.3k11](/packages/bizley-migration)[dmstr/yii2-migrate-command

Console Migration Command with multiple paths/aliases support

31295.5k6](/packages/dmstr-yii2-migrate-command)[yii2tech/illuminate

Yii2 to Laravel Migration Package

11315.1k](/packages/yii2tech-illuminate)[e282486518/yii2-console-migration

yii2命令行中使用migration备份和还原数据库

388.1k4](/packages/e282486518-yii2-console-migration)[hzhihua/yii2-dump

Generate the schema from an existing database

1828.5k1](/packages/hzhihua-yii2-dump)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
