Yii 1.1: extended-database-migration

Extension to MigrateCommand that adds module support and many more usefull features.
15 followers

Extended Migration Command

This extension is an enhanced version of the Yii Database Migration Tool that adds module support and many more usefull features. If there is anything you would like to have added, or you found a bug please report it on github or contact me via email.

Features

  • Module-Support (migrations are distributed in a seperate folder for every module) so you can...
    • ...enable and disable Modules
    • ...add new module by migrating it up
    • ...remove a module by running its migrations down
    • ...select the modules you want to run migrations for in every run
    • ...declare Module-dependencies (coming soon)
    • ...different migration templates depending on modules (coming soon)

Resources

Requirements

  • Yii 1.1.6 or above (MigrateCommand was introduced in this version) if you copy MigrateCommand and CDbMigration you should be able to use this extension with any yii version.

Installation

  • Extract the release file under protected/extensions.
  • Add the following to your config file for yiic command:
'commandMap' => array(
        'migrate' => array(
            // alias of the path where you extracted the zip file
            'class' => 'application.extensions.yiiext.commands.migrate.EMigrateCommand',
            // this is the path where you want your core application migrations to be created
            'migrationPath' => 'application.db.migrations',
            // the name of the table created in your database to save versioning information
            'migrationTable' => 'tbl_migration',
            // the application migrations are in a pseudo-module called "core" by default
            'applicationModuleName' => 'core',
            // define all available modules
            'modulePaths' => array(
                'admin'      => 'application.modules.admin.db.migrations',
                'user'       => 'application.modules.user.db.migrations',
                'yourModule' => 'application.any.other.path.possible',
                // ...
            ),
            // here you can configrue which modules should be active, you can disable a module by adding its name to this array
            'disabledModules' => array(
                'admin', 'anOtherModule', // ...
            ),
            // the name of the application component that should be used to connect to the database
            'connectionID'=>'db',
            // alias of the template file used to create new migrations
            'templateFile'=>'application.db.migration_template',
        ),
    ),

Please note: if you already used MigrateCommand before, make sure to add the module column to your migrationTable:

ALTER TABLE `tbl_migration` ADD COLUMN `module` varchar(32) DEFAULT NULL;
 UPDATE `tbl_migration` SET module='core';

Usage

General Usage

You can run yiic migrate help to see all parameters and a short example on how to use them.

The basics are explained in the Definitive Guide to Yii. Read them first if you haven't used database migration before. The usage of Extended Migration Command is not much different from the native one. The only command that is different is create where you have to additionally specify the modulename:

   yiic migrate create modulename create_user_table

This creates a new migration named 'create_user_table' in module 'modulename'. The native usage

   yiic migrate create create_user_table

creates a new migration named 'create_user_table' in the application(core).

--module Parameter

In all other commands (up, down, history, new, to and mark) you can use the parameter --module=<modulenames> where <modulenames> can be a comma seperated list of module names or a single module name. This parameter will limit the current command to affect only the specified modules. Some Examples:

   yiic migrate new --module=core

This will show you all new migrations for module core and

   yiic migrate up 5 --module=core,user

will migrate up the next 5 new migrations in modules core and user. If there are new migrations in other modules they will be ignored.

   yiic migrate history --module=core,user

will show you which migrations have been applied for modules core and user in the past. If you do not specify a module the command behaves like the native one but does the migration for all modules.

add a module

Simply enable it in your config and run yiic migrate up --module=yourModule.

remove a module

Run yiic migrate to m000000_000000 --module=yourModule. For this to work all your migrations must have the down()-method implemented correctly.

Changelog

0.7.1 (2012-01-31)

  • fix for getTable trying to hit a db cache and die in endless loop (cebe)
  • made sure that mark and to action are working correctly (cebe)

0.7.0 (2012-01-28)

  • adjusted sql commands to be compatible with nearly all pdo db systems (cebe)
  • modules are now loaded from yii application config if not set (cebe)
  • improved create action error handling (cebe)

0.6.0 (2011-09-12)

  • adjusted sql commands to be compatible with sqlite and postgres (cebe)
  • added compatibility with CDbMigration, migrations need not extend EDbMigration (cebe)

0.5.0 (2011-08-09)

  • implemented mark-action so it now works with modules (cebe)
  • implemented to-action so it now works with modules (cebe)
  • fixed problem that base-migrations where not cretated on mark command (cebe)

0.4.0 (2011-08-08)

  • added confirm() method to EDbMigration (cebe)

0.3.1 (2011-08-05)

  • fixed a problem with finding new migrations (cebe)
  • added base migration for every single module (cebe)
  • fixed problem with history and down migration when --module parameter is set (cebe)
  • added $moduleDelimiter property and replaced string function with multibyte versions (cebe)
  • complete refactoring of basic functionality, more stability and more straight forward (cebe)

0.1.0 (2011-08-04)

  • Initial public release (cebe)
  • module support for migrations
  • extended execute() of CDbMigration with parameter $verbose

Total 9 comments

#11895 report it
CeBe at 2013/02/11 03:18pm
tag 0.7.2

https://github.com/yiiext/migrate-command/tree/0.7.2

Module dependecies will allow you to add dependecies between modules so that you can only migrate one module up when another is migrated too. I've seen no real need for this in my projects so far, so I did not implement it.

#11594 report it
schmunk at 2013/01/22 08:05am
@fad, @CeBe

@fad: What do you mean with Module-dependencies?

@CeBe: Could you just add a 0.7.2 tag for the latest changes? So I could remove the dev option, when getting this extension via composer :)

#11593 report it
CeBe at 2013/01/22 07:33am
No next version soon...

sry guys I don't have time to work on this right now. Will try to release 0.8 in April or May, but I can not promise that I will do it. When you have time for implementing features feel free to discuss ideas in the Issuetracker on github or send me pull requests. https://github.com/yiiext/migrate-command

#11592 report it
fad at 2013/01/22 07:21am
Module-dependencies

declare Module-dependencies

can we expect it?

#9877 report it
schmunk at 2012/09/18 05:17pm
This is so awesome!

Works without any problems and provides very nice features, thank you! Will be part of Phundament soon.

btw: Would be also nice to see a 0.8 version.

#6686 report it
CeBe at 2012/01/28 12:05pm
Just released version 0.7.0

Just released version 0.7.0 which should now be compatible with all db systems supported by Yii.

I need your help to find out if it is working as expected, I were only able to test it with sqlite and MySQL.

Please let me know if your system is working and if not file a bug on github or discuss it in the forum.

Thanks!

#4981 report it
CeBe at 2011/09/03 06:35am
thanks!

@redguy: thank you for your feedback, will consider this in the next release!

btw: for feature requests and bug reports, please use the issue tracker on github: https://github.com/yiiext/migrate-command/issues

#4973 report it
redguy at 2011/09/02 10:32am
more suggestions...

One more thing - consider applying modyfication to allow using both EDbMigration and CDbMigration (by checking "if( $migration instanceof EDbMigration)" in some places) - this could make possible to use standard migrations provided with third party modules.

#4972 report it
redguy at 2011/09/02 09:57am
suggestions to improve this extension

Hi, I am using your extension but found that it works only with mysql database. To make it work with postgres (and possibly with other) you have to change "getMigrationHistory" function like this:

if ($this->_scopeNewMigrations || !$this->_scopeAddModule) {
            $select = "version AS version_name, apply_time";
            $params = array();
        } else {
            if( $db->getDriverName() == 'mysql' ) {
                $select = "CONCAT(module,:delimiter,version) AS version_name, apply_time";
            } else {
                $select = "(module || :delimiter || version) AS version_name, apply_time";
            }
            $params = array(':delimiter' => $this->moduleDelimiter);
        }
 
        $command = $db->createCommand()
                      ->select($select)
                      ->from($this->migrationTable)
                      ->order('version DESC')
                      ->limit($limit);
 
        if (!is_null($this->module)) {
            $criteria = new CDbCriteria();
            $criteria->addInCondition('module', explode(',', $this->module));
            $command->where = $criteria->condition;
            $params += $criteria->params;
        }
        return CHtml::listData($command->queryAll(true, $params), 'version_name', 'apply_time');

you have to change "versionName" column name to "version_name" - I have noticed that postgres returned "versionname" instead of case-sensitive, and CHtml::listData did not work properly in such case. You have to use different concatenation syntax. "CONCAT" function works only in mysql, in postgresql you have to to use "||" (double pipe) operator.

Also - it would be nice improvement to change "modules" configuration to be compatible with definition in main configuration file (main.php). Now I have maintain same configuration in both configs but in different format...

Leave a comment

Please to leave your comment.

Create extension