bizley / migration
Migration generator for Yii 2.
Fund package maintenance!
bizley
Installs: 284 028
Dependents: 14
Suggesters: 0
Security: 0
Stars: 288
Watchers: 21
Forks: 36
Open Issues: 1
Type:yii2-extension
Requires
- php: >=7.2
- ext-mbstring: *
- ext-pdo: *
- yiisoft/yii2: ^2.0.19
Requires (Dev)
- infection/infection: *
- phpstan/phpstan: *
- phpunit/phpunit: <10.0
- roave/security-advisories: dev-latest
- dev-master
- 4.4.1
- 4.4.0
- 4.3.1
- 4.3.0
- 4.2.0
- 4.1.4
- 4.1.3
- 4.1.2
- 4.1.1
- 4.1.0
- 4.0.5
- 4.0.4
- 4.0.3
- 4.0.2
- 4.0.1
- 4.0.0
- 4.0.0-RC2
- 4.0.0-RC1
- 3.x-dev
- 3.6.6
- 3.6.5
- 3.6.4
- 3.6.3
- 3.6.2
- 3.6.1
- 3.6.0
- 3.5.0
- 3.4.0
- 3.3.0
- 3.2.0
- 3.1.0
- 3.0.5
- 3.0.4
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0
- 2.x-dev
- 2.9.6
- 2.9.5
- 2.9.4
- 2.9.3
- 2.9.2
- 2.9.1
- 2.9.0
- 2.8.0
- 2.7.0
- 2.6.0
- 2.5.0
- 2.4.0
- 2.3.4
- 2.3.3
- 2.3.2
- 2.3.1
- 2.3.0
- 2.2.3
- 2.2.2
- 2.2.1
- 2.2.0
- 2.1.2
- 2.1.1
- 2.1
- 2.0
- 1.1
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- dev-dependabot/composer/phpunit/phpunit-lt-12.0
This package is auto-updated.
Last update: 2024-10-09 17:41:17 UTC
README
Yii 2 Migration
Migration Creator And Updater
In a perfect world you prepare migration files for your database first so you can run it when ready. But since this is not a perfect world, sometimes you start with a database already set - with this package you can easily create a migration file based on your DB schema with one console command.
Furthermore, when your DB is updated later on you can generate a migration file updating the schema to the current state. The package is comparing it with the migration history:
- History of applied migrations is scanned to gather all modifications made to the table.
- Virtual table schema is prepared and compared with the current table schema.
- Differences are generated as an update migration.
- In case of the migration history not keeping information about the table, a creating migration is generated.
Installation
Run console command
composer require --dev bizley/migration
Or add the package to your composer.json
file:
{ "require-dev": { "bizley/migration": "^4.0" } }
then run composer update
.
Configuration
Add the following in your configuration file (preferably console configuration file):
[ 'controllerMap' => [ 'migration' => [ 'class' => 'bizley\migration\controllers\MigrationController', ], ], ]
Additional options are available as following (with their default values):
[ 'controllerMap' => [ 'migration' => [ 'class' => 'bizley\migration\controllers\MigrationController', 'migrationPath' => '@app/migrations', // Directory storing the migration classes 'migrationNamespace' => null, // Full migration namespace 'useTablePrefix' => true, // Whether the table names generated should consider the $tablePrefix setting of the DB connection 'onlyShow' => false, // Whether to only display changes instead of generating update migration 'fixHistory' => false, // Whether to add generated migration to migration history 'skipMigrations' => [], // List of migrations from the history table that should be skipped during the update process 'excludeTables' => [], // List of database tables that should be skipped for actions with "*" 'fileMode' => null, // Permission to be set for newly generated migration files 'fileOwnership' => null, // User and/or group ownership to be set for newly generated migration files 'leeway' => 0, // Leeway in seconds to apply to a starting timestamp when generating migration ], ], ]
Usage
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
or
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 "*"
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.
-
Extract SQL statements of migration
migration_name
(UP) New in 4.4.0:php yii migration/sql "migration_name"
-
Extract SQL statements of migration
migration_name
(DOWN) New in 4.4.0:php yii migration/sql "migration_name" "down"
Command line parameters
[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()
Column
int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY
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:
Columnvarchar(45)
generalSchema=false:$this->string(45)
generalSchema=true:$this->string(45)
[2] Here you can place the migrations containing actions that cannot be covered by the extractor, i.e. when there is a migration setting the RBAC hierarchy with the authManager component. Such actions should be kept in a separated migration and placed on this list to prevent them from being run during the extraction process.
[3] This mode allows using a raw SQL column definition for the 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.
Important information about RENAMING tables or columns
When you rename a table or a column, remember to generate appropriate migration manually, otherwise this extension will not generate an updating migration (in case of the table) or will generate a migration with the command to drop the original column and add a 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 the 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 a 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.
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, 5.x, 8.x)
- Oracle
- PostgreSQL (9.x and above)
- SQLite (2/3)
Only history of the 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 the table's data (like insert()
, upsert()
, delete()
, truncate()
,
etc.) are not tracked.
Updating migrations process requires for the methods createTable()
, addColumn()
, and alterColumn()
to provide changes
in columns definition in the form of an instance of yii\db\ColumnSchemaBuilder
(like $this->string()
instead of 'varchar(255)'
).
The new 4.4.0 feature with extracting SQL statements from the existing migration supports all methods available in
yii\db\Migration
.
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).
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.