{"id":14966594,"url":"https://github.com/bizley/yii2-migration","last_synced_at":"2025-05-14T21:07:47.585Z","repository":{"id":9684138,"uuid":"62985747","full_name":"bizley/yii2-migration","owner":"bizley","description":"Yii 2 Migration Creator And Updater","archived":false,"fork":false,"pushed_at":"2024-12-31T09:22:19.000Z","size":1071,"stargazers_count":291,"open_issues_count":1,"forks_count":37,"subscribers_count":20,"default_branch":"master","last_synced_at":"2025-04-13T17:46:57.812Z","etag":null,"topics":["database","database-migrations","generator","hacktoberfest","migration","php","updater","yii2"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bizley.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["bizley"]}},"created_at":"2016-07-10T06:41:45.000Z","updated_at":"2025-02-07T16:25:17.000Z","dependencies_parsed_at":"2025-01-31T06:39:31.391Z","dependency_job_id":null,"html_url":"https://github.com/bizley/yii2-migration","commit_stats":{"total_commits":321,"total_committers":9,"mean_commits":"35.666666666666664","dds":"0.18380062305295952","last_synced_commit":"cbe9c0e7b1f2749e1d63ce51dddc83a6c5fd0af0"},"previous_names":[],"tags_count":69,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bizley%2Fyii2-migration","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bizley%2Fyii2-migration/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bizley%2Fyii2-migration/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bizley%2Fyii2-migration/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bizley","download_url":"https://codeload.github.com/bizley/yii2-migration/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254227613,"owners_count":22035670,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database","database-migrations","generator","hacktoberfest","migration","php","updater","yii2"],"created_at":"2024-09-24T13:36:39.445Z","updated_at":"2025-05-14T21:07:42.575Z","avatar_url":"https://github.com/bizley.png","language":"PHP","funding_links":["https://github.com/sponsors/bizley"],"categories":[],"sub_categories":[],"readme":"![Yii 2 Migration](yii2-migration.png)\n\n# Yii 2 Migration\n\n![Latest Stable Version](https://img.shields.io/packagist/v/bizley/migration.svg)\n[![Total Downloads](https://img.shields.io/packagist/dt/bizley/migration.svg)](https://packagist.org/packages/bizley/migration)\n![License](https://img.shields.io/packagist/l/bizley/migration.svg)\n[![Infection MSI](https://badge.stryker-mutator.io/github.com/bizley/yii2-migration/master)](https://infection.github.io)\n\n## Migration Creator And Updater\n\nIn a perfect world you prepare migration files for your database first so you can run it when ready. \nBut since this is not a perfect world, sometimes you start with a database already set - with this package \nyou can easily **create a migration file** based on your DB schema with **one console command**.\n\nFurthermore, when your DB is updated later on you can **generate a migration file updating the schema** to \nthe current state. The package is comparing it with the migration history:\n\n1. History of applied migrations is scanned to gather all modifications made to the table.\n2. Virtual table schema is prepared and compared with the current table schema.\n3. Differences are generated as an update migration.\n4. In case of the migration history not keeping information about the table, a creating migration is generated.\n\n## Installation\n\nRun console command\n\n```\ncomposer require --dev bizley/migration\n```\n\nOr add the package to your `composer.json` file:\n\n```json\n{\n    \"require-dev\": {\n        \"bizley/migration\": \"^4.0\"\n    }\n}\n```\n\nthen run `composer update`. \n\n## Configuration\n\nAdd the following in your configuration file (preferably console configuration file):\n\n```php\n[\n    'controllerMap' =\u003e [\n        'migration' =\u003e [\n            'class' =\u003e 'bizley\\migration\\controllers\\MigrationController',\n        ],\n    ],\n]\n```\n\nAdditional options are available as following (with their default values):\n\n```php\n[\n    'controllerMap' =\u003e [\n        'migration' =\u003e [\n            'class' =\u003e 'bizley\\migration\\controllers\\MigrationController',\n            'migrationPath' =\u003e '@app/migrations', // Directory storing the migration classes\n            'migrationNamespace' =\u003e null, // Full migration namespace\n            'useTablePrefix' =\u003e true, // Whether the table names generated should consider the $tablePrefix setting of the DB connection\n            'onlyShow' =\u003e false, // Whether to only display changes instead of generating update migration\n            'fixHistory' =\u003e false, // Whether to add generated migration to migration history\n            'skipMigrations' =\u003e [], // List of migrations from the history table that should be skipped during the update process\n            'excludeTables' =\u003e [], // List of database tables that should be skipped for actions with \"*\"\n            'fileMode' =\u003e null, // Permission to be set for newly generated migration files\n            'fileOwnership' =\u003e null, // User and/or group ownership to be set for newly generated migration files\n            'leeway' =\u003e 0, // Leeway in seconds to apply to a starting timestamp when generating migration\n        ],\n    ],\n]\n```\n\n## Usage\n\nThe following console command are available (assuming you named the controller `migration` like in the example above):\n\n- List all the tables in the database:\n\n  ```\n  php yii migration\n  ```\n    \n  or\n\n  ```\n  php yii migration/list\n  ```\n\n- Generate migration to create DB table `table_name`:\n\n  ```\n  php yii migration/create \"table_name\"\n  ```\n\n- Generate migration to update DB table `table_name`:\n\n  ```\n  php yii migration/update \"table_name\"\n  ```\n\n  To generate migrations for all the tables in the database at once (except the excluded ones) use asterisk (*):\n\n  ```\n  php yii migration/create \"*\"\n  php yii migration/update \"*\"\n  ```\n\n  You can generate multiple migrations for many tables at once by separating the names with comma:\n\n  ```\n  php yii migration/create \"table_name1,table_name2,table_name3\"\n  ```\n\n  You can provide an asterisk as a part of table name to use all tables matching the pattern:\n\n  ```\n  php yii migration/update \"prefix_*\"\n  ```\n\n  Creating multiple table migrations at once forces the proper migration order based on the presence of the foreign keys. \n  When tables are cross-referenced the additional foreign keys migration is generated at the end of default generation.\n\n- Extract SQL statements of migration `migration_name` (UP) **New in 4.4.0**:\n\n  ```\n  php yii migration/sql \"migration_name\"\n  ```\n\n- Extract SQL statements of migration `migration_name` (DOWN) **New in 4.4.0**:\n\n  ```\n  php yii migration/sql \"migration_name\" \"down\"\n  ```\n\n## Command line parameters\n\n| command              | alias | description                                                                                                            |\n|----------------------|:-----:|------------------------------------------------------------------------------------------------------------------------|\n| `migrationPath`      | `mp`  | Directory (one or more) storing the migration classes.                                                                 |\n| `migrationNamespace` | `mn`  | Namespace (one or more) in case of generating a namespaced migration.                                                  |\n| `useTablePrefix`     | `tp`  | Whether the generated table names should consider the `tablePrefix` setting of the DB connection.                      |\n| `migrationTable`     | `mt`  | Name of the table for keeping the applied migration information.                                                       |\n| `onlyShow`           | `os`  | Whether to only display changes instead of generating an update migration.                                             |\n| `generalSchema`      | `gs`  | Whether to use the general column schema instead of the database specific one (see [1] below).                         |\n| `fixHistory`         | `fh`  | Whether to add a migration history entry when the migration is generated.                                              |\n| `skipMigrations`     |       | List of migrations from the history table that should be skipped during the update process (see [2] below).            |\n| `excludeTables`      |       | List of tables that should be skipped.                                                                                 |\n| `experimental`       | `ex`  | Whether to run in the experimental mode (see [3] below).                                                               |\n| `fileMode`           | `fm`  | Generated file mode to be changed using `chmod`.                                                                       |\n| `fileOwnership`      | `fo`  | Generated file ownership to be changed using `chown`/`chgrp`.                                                          |\n| `leeway`             | `lw`  | The leeway in seconds to apply to a starting timestamp when generating migration, so it can be saved with a later date |\n\n[1] Remember that with different database types, general column schemas may be generated with different length.\n\n\u003e ### MySQL examples:  \n\u003e Column `varchar(255)`  \n\u003e generalSchema=false: `$this-\u003estring(255)`    \n\u003e generalSchema=true: `$this-\u003estring()`  \n\n\u003e Column `int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY`    \n\u003e generalSchema=false: `$this-\u003einteger(11)-\u003enotNull()-\u003eappend('AUTO_INCREMENT PRIMARY KEY')`  \n\u003e generalSchema=true: `$this-\u003eprimaryKey()`\n\n\u003e When column size is different from DBMS' default, it's kept:  \n\u003e Column `varchar(45)`  \n\u003e generalSchema=false: `$this-\u003estring(45)`    \n\u003e generalSchema=true: `$this-\u003estring(45)`\n\n[2] Here you can place the migrations containing actions that cannot be covered by the extractor, i.e. when there is a migration \nsetting the RBAC hierarchy with the authManager component. Such actions should be kept in a separated migration and placed on \nthis list to prevent them from being run during the extraction process.\n\n[3] This mode allows using a raw SQL column definition for the migration updater (i.e. `['column' =\u003e 'varchar(255)']` instead \nof `['column' =\u003e $this-\u003estring()]`). Since the generating process in this mode depends on the individual DBMS syntax \nthe results might not be correct. All help improving this mode is more than welcome.\n\n## Important information about RENAMING tables or columns\n\nWhen you rename a table or a column, remember to generate appropriate migration manually, otherwise this extension will \nnot generate an updating migration (in case of the table) or will generate a migration with the command to drop the original column \nand add a renamed one (in case of the column). This is happening because yii2-migration can only compare two states of \nthe table without the knowledge of how one state turned into another. While the very result of the migration renaming \nthe column and the one dropping it and adding another is the same in terms of structure, the latter **makes you lose data**.\n\nOnce you add a renaming migration to the history, it's being tracked by the extension.\n\n## Migrating from v2 or v3 to v4\n\nSee [Migrating to version 4.0](migrating_to_v4.md) section.\n\n## Notes\n\nThis extension should work with all database types supported in Yii 2 core:\n\n- CUBRID (9.3.x and higher)\n- MS SQL Server (2008 and above)\n- MySQL (4.1.x, 5.x, 8.x)\n- Oracle\n- PostgreSQL (9.x and above)\n- SQLite (2/3)\n\nOnly history of the migrations extending `yii\\db\\Migration` class can be properly scanned, and only changes applied with\ndefault `yii\\db\\Migration` methods can be recognised (except for `execute()`, `addCommentOnTable()`, and \n`dropCommentFromTable()` methods). Changes made to the table's data (like `insert()`, `upsert()`, `delete()`, `truncate()`, \netc.) are not tracked.\n\nUpdating migrations process requires for the methods `createTable()`, `addColumn()`, and `alterColumn()` to provide changes \nin columns definition in the form of an instance of `yii\\db\\ColumnSchemaBuilder` (like `$this-\u003estring()` instead of `'varchar(255)'`).\n\nThe new 4.4.0 feature with extracting SQL statements from the existing migration supports all methods available in\n`yii\\db\\Migration`.\n\n## Tests\n\nTests for MySQL, PostgreSQL, and SQLite are provided. Database configuration is stored in `tests/config.php` (you can \noverride it by creating `config.local.php` file there).  \nDocker Compose file for setting up the databases is stored in `tests/docker`.\n\n## Previous versions\n\nThese versions are not developed anymore but still available for all poor souls that are stuck with EOL PHP.\nSome of the newest features may not be available there.\n\n| version constraint | PHP requirements |                                    Yii requirements                                     |                                                            \n|:------------------:|:----------------:|:---------------------------------------------------------------------------------------:|\n|        ^3.6        |      \u003e= 7.1      |                                       \u003e= 2.0.15.1                                       |\n|        ^2.9        |      \u003c 7.1       | 2.0.13 to track non-unique indexes, 2.0.14 to handle `TINYINT` and `JSON` type columns. |\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbizley%2Fyii2-migration","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbizley%2Fyii2-migration","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbizley%2Fyii2-migration/lists"}