esempla/yii2tech-migration-creator

Migration generator for Yii 2.

0.1.3 2019-03-04 12:28 UTC

This package is auto-updated.

Last update: 2024-04-17 22:49:52 UTC


README

Latest Stable Version Total Downloads License Build Status

Migration creator and updater

Generates migration file based on the existing database table and previous migrations. >Modified and adjusted for postgresql, and separate foreign Keys into a separate map as a separate migration.https://github.com/bizley/yii2-migration . Acest modul este creat pentru uz intern, poate fi folosit liber, compania nu asigura o careva garantie pentru acesta.

For orginall package try: https://github.com/bizley/yii2-migration

Installation for PHP >= 7.1

Add the package to your composer.json:

{
    "require": {
        "esempla/yii2tech-migration-creator ": "0.1.*"
    }
}

and run composer update or alternatively run composer require esempla/yii2tech-migration-creator: 0.1.*

Configuration

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

'components' => [
    // ...
],
'controllerMap' => [
   'migration' => [
           'class' => 'esempla\yii2tech_migration_creator\controllers\MigrationController',
           'db' =>  'db',
           'migrationPath'=>'@app/migrations/test'
         ],
],

>If you have more than one schema in the database or more databases. For more schema configure in the following way:

              'db' => [
                  'class' => 'yii\db\Connection',
                  'dsn' => "pgsql:host=localhost;port=5432;dbname=database_name",
                  'username' => 'postgres',
                  'password' => 'postgres',
                  'charset' => "utf-8",
                  'schemaMap' => [
                      'pgsql' => [
                          'class' => 'yii\db\pgsql\Schema',
                          'defaultSchema' => 'public',//specify your schema here
                      ],
                  ],
              ],
              'db1' => [
                  'class' => 'yii\db\Connection',
                  'dsn' => "pgsql:host=localhost;port=5432;dbname=database_name",
                  'username' => 'postgres',
                  'password' => 'postgres',
                  'charset' => "utf-8",
                  'schemaMap' => [
                      'pgsql' => [
                          'class' => 'yii\db\pgsql\Schema',
                          'defaultSchema' => 'classifiers',//specify your schema here
                      ],
      
      
                  ],
              ],
               ]

Usage

The following console command are available:

  • List all the tables in the database:

    php yii migration  --db='db1'
    
    

    or

    php yii migration/list  --db='db1'
    
  • Generate migration to create DB table table_name:

    php yii migration/create table_name  --db='db1'
    
  • Generate migrations to create all DB tables:

    php yii migration/create-all  --db='db1'
    
  • Generate migration to update DB table table_name:

    php yii migration/update table_name  --db='db1'
    
  • Generate migrations to update all DB tables:

    php yii migration/update-all --db='db1'
    

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  --db='db1'

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

commandaliasdescription
dbApplication component's ID of the DB connection to use when generating migrations. default: 'db'
migrationPathpDirectory storing the migration classes. default: '@app/migrations'
migrationNamespacenNamespace in case of generating namespaced migration. default: null
templateFileFTemplate file for generating create migrations. default: '@bizley/migration/views/create_migration.php'
templateFileUpdateUTemplate file for generating update migrations. default: '@bizley/migration/views/update_migration.php'
useTablePrefixPWhether the table names generated should consider the tablePrefix setting of the DB connection. default: 1
migrationTabletName of the table for keeping applied migration information. default: '{{%migration}}'
showOnlysWhether to only display changes instead of generating update migration. default: 0
generalSchemagWhether to use general column schema instead of database specific (1). default: 1
fixHistoryhWhether to add migration history entry when migration is generated. default: 0
skipMigrationsList of migrations from the history table that should be skipped during the update process (2). default: []
tableOptionsInitOString 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'; }
tableOptionsoString rendered in the create migration template for table options. default: $tableOptions

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.