Yii 1.1: Migration the database schema to another one

5 followers

In this wiki I will show how to copy a schema from a database to another one

Note that there are compatible issues depended of the source and the destination database, so I preferred to keep the migration as simple as possible

In this wiki we will use as source the mysql database and for destination an sqlite database

1) Add in protected/config/main.php both of two database connection in the component section

'dblite' => array(
            'class'=>'CDbConnection',
            'connectionString' => 'sqlite:' . dirname(__FILE__) . '/../data/sqlite.db',
 ),
 
'db' => array(
    'connectionString' => 'mysql:host=localhost;dbname=mydatabase',
    'username' => 'root',
    'password' => '',
    'emulatePrepare' => true,
    'charset' => 'utf8',
),

2) Make a php file (for example Myglobals.php) In your components

class Myglobals {
 
  public static function MigrateMsqlToSqlite() {
 
        $path = Yii::getPathOfAlias('application.data');
 
 
        $del = (@unlink($path . '/thedatabase.db'));
        if (!$del) {
            throw new CHttpException(403,'Access file permission denied!');
        }
 
        touch($path . '/thedatabase.db');
 
        $cmd = Yii::app()->dblite->createCommand();
 
        $connection = Yii::app()->db;
        $dbSchema = $connection->schema;
        $tables = $dbSchema->getTables();
        foreach ($tables as $tbl) {
            $cols = [];
            foreach ($tbl->columns as $c)
                    $cols[$c->name] = $c->type;
 
            $cmd->createTable($tbl->name, $cols);
        }
    }   
}

3) Call in your Controller/action the above function like that Myglobals::MigrateMsqlToSqlite();

Now the sqlite.db conteains the database schema of the mysql

Total 2 comments

#17584 report it
KonApaz at 2014/07/04 08:37am
Re: 17572

Thank you Suthar :)

#17572 report it
Rohit Suthar at 2014/07/04 03:16am
Nice..

hey thanks for sharing this )..

Leave a comment

Please to leave your comment.

Write new article