Cygnite - A Modern Toolkit For Web Developers

The Elegant Way of Building Full-Featured Web Applications

Cygnite Dynamic Routing

Migrations


Documentation


Migrations

Overview

Create your database version control using Cygnite Migration commands. The Migration commands gives you convenient way to alter your database schema over time and stay you up-to-date. In most of the database driven application we often need to keep track of database schema changes, similar thing as we do with our source code (with git, svn etc.). For example once your application is in production server, you may want to add a new column or add a index to the table. This is where Cygnite Migration tool comes to action, now you can keep track of your alteration with Cygnite Migration tool.

Cygnite migration tool is inspired by Ruby on Rails migration. Migrations are coupled with Cygnite Schema Builder to manage your database schema.

Creating a Migration

If you are running migration commands for the first time you need to execute migrate:init command. It will create a migrations table in your database,it holds all migration information ran in your application

You can also generate your first migration file using same init command. You just need to pass extra parameter in init command.


  cd /var/www/cygnite/console/bin/  

  php craft migrate:init user

The above command will generate a skeleton migration class with timestamp prefix into src/Apps/Resources/Database/Migrations/xxxx_user.php. Class will look like below.

Migration File Structure



 use Cygnite\Database\Table\Schema;

 class User
 {

  /**
   * Run the migrations up.
   * @return void
   */
   public function up()
   {
        //Your schema to migrate
        Schema::make('user', function ($table)
        {
            $table->create([
                    ['column'=> 'id', 'type' => 'int', 'length' => 11,
                     'increment' => true, 'key' => 'primary'],
  
                    /*..Add columns to your table schema .*/

                    ['column'=> 'created_at', 'type' => 'datetime'],
                    ['column'=> 'updated_at', 'type' => 'datetime'],

            ], 'InnoDB', 'latin1');
        });

   }

  /**
   * Revert back your migrations.
   * @return void
   */
   public function down()
   {
       //Roll back your changes done by up method.
        Schema::make('user', function ($table)
        {
            $table->drop();
        });       
   }
}

In the above migration class up method to run your migration and down method to rollback migration changes made in up method. You can make use of Schema Builder functions to alter/drop your table schema.

[Note: You can specify the database connection name where you wish to build the schema using on() method $table->on('products');. If you don't specify the database connection then Schema Builder will use default connection to build your schema.

Running Migration

Let us create a migration. Below example shows how to create a table schema and alter it using migration command.



  /**
   * Run the migrations up.
   * @return void
   */
   public function up()
   {
       Schema::make('category', function ($table)
       {
           $table->on('products');

           $table->create([
                ['column'=> 'id', 'type' => 'int', 'length' => 11,
                 'increment' => true, 'key' => 'primary'],
                ['column'=> 'type', 'type' => 'string', 'length' =>100],
                ['column'=> 'description', 'type' => 'string', 'length'  =>16],
                ['column'=> 'price', 'type' => 'decimal', 'length'  =>'10,2'],
                ['column'=> 'rise', 'type' => 'time'],
                ['column'=> 'time', 'type' => 'timestamp',
                 'length'=> "DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"],
                ['column'=> 'created_at', 'type' => 'datetime'],
                ['column'=> 'updated_at', 'type' => 'datetime'],
             ], 'InnoDB', 'latin1');
       });        
   }
 

After doing appropriate changes to your latest migration file, you should issue migration command to reflect changes into your database.


   php craft migrate

Above command will up your migration into database and also create a new version in your database migrations table.

[Note: If you running your application in higher version of MYSQL (example: MYSQL 5.7.9) you need to specify the default value for created_at, updated_at fields in length value.]
 
 //Your schema to migrate
 Schema::make('user', function ($table)
 {
    $table->create([
        ['column'=> 'created_at',
         'type' => 'datetime', 'length' => 'NOT NULL DEFAULT NOW()'],
        ['column'=> 'updated_at', 'type' => 'datetime',
         'length' => 'DEFAULT NOW()'],
    ], 'InnoDB', 'latin1');
 });

Rollback Migration

Rollback your latest migration changes using down method.


   /**
   * Revert back your migrations.
   * @return void
   */
   public function down()
   {
       //Roll back your changes done by up method.
       Schema::make($this,function ($table)
       {
           $table->on('products');

           $table->drop();
       });
   }




   cd var/www/cygnite/console/bin/
 
   php craft migrate down
    


Follow Us On Facebook Twitter Google+ Linkedin
Released Under The MIT Public License. Copyrights @2012-2017. Powered by- Sanjoy Dey Productions.