How to Create Database Migration in Codeigniter

How to Create Database Migration in Codeigniter

Today i will explain you, how to create database migration in codeigniter

Migration are a convenient way for web developer to alter database is a structured and  well organized way. Also you would have to keep track of which things are changes to be run against the production environment next time you deploy.

In simple way migration give you a ability to keep database in all environments. Migration class is very important because every web developer not every time goes to changing in database, but changes in database through code by using migration class. Migration allows you can create and manage database structure by code.

Every migration is run on number order like upward or backward depending on which method taken. Basically there are two method are present
  • Sequential
  • Timestamp

Sequential: In Sequential, every migration start with 001. Each number must be three digits without any gap in between.

Timestamp: In timestamp, every migration is numbered format like YYYYMMDDHHIISS for example 20161021110730

Before creating the migration first create a new database (go to phpMYadmin)  for example test in this state there is no table in this database.

How to create database using phpMyadmin(see post How to Insert Data in Database Using CodeIgniter
)

How To Create Migration Follows These Steps

1.   First create a folder inside the application folder, give folder name migrations screen shown below


Create Database Migration in Codeigniter

How to Configure Migration

2.   Go to  application/config  folder and open migration.php, some change in this file by default migration is disabled so first enabled it, code shown below

     $config['migration_enabled'] = FALSE; 
     change To
     $config['migration_enabled'] = TRUE;

3.   Changes in the migration type by default migration type is timestamp

     $config['migration_type'] = 'timestamp';
     change To
     $config['migration_type'] = 'sequential';

Because in this article, i am using migration type as a sequential

4.   Change in auto migrate by default set False

     $config['migration_auto_latest'] = FALSE;
     change To
     $config['migration_auto_latest'] = TRUE;

5.   Change in migration versionby default set 0

     $config['migration_version'] = 0;
     change To
     $config['migration_version'] = 1;

Also you can see in this file migration version $config['migration_version'] = 0; before creating any migration

6.   I want to create a table for example give the table name is employee having following fields

     Emp_id
     Emp_name
     Emp_email
     Emap_mobile
     Emap_address

7.   Create a file inside application/migrations folder give the file named as 001_application1.php, screen shown below


Create Database Migration in Codeigniter

Now, open  001_application1.php, file and paste the code, shown below

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Migration_application1 extends CI_Migration {
  
  public function up()
  {
    
    if(!$this->db->table_exists('employee'))
    {
      $this->dbforge->add_field(array(
        'emp_id' => array(
        'type' => 'int',
        'constraint' => 9,
        'unsigned' => true,
        'auto_increment' => true
         ),
        'emp_name' => array(
        'type' => 'varchar',
        'constraint' => 100,
        'null' => false
         ),
        'emp_email' => array(
        'type' => 'varchar',
        'constraint' => 50,
        'null' => false
         ),
        'emp_phone' => array(
        'type' => 'varchar',
        'constraint' => 20,
        'null' => false
         ),
        'emp_address' => array(
        'type' => 'varchar',
        'constraint' => 255,
        'null' => false
         ),
      ));

        $this->dbforge->add_key('emp_id', true);
        $this->dbforge->create_table('employee', true);
    }   
  }
  
  public function down()
  {
    
  }

}

How to Call Migration:

8.   Call the Migration
      Create a controller, inside application/controllers folder give the file named as migrate.php, screen shown below


Create Database Migration in Codeigniter

Now, open migrate.php, file and paste the code shown below

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Migrate extends CI_Controller
{
public function index()
{
$this->load->library('migration');

if (! $this->migration->current())
{
show_error($this->migration->error_string());
}
}
}

At this stage created database does not contains any table and make sure that  application/config/database.php databse name same as created database name for example 'database' => 'test'

9.   Now, go to browser and type localhost/CITest/migrate and run

10.   Go to phpMyadmin, click test database , here you can see citest_migrations table is created, screen shown below


Create Database Migration in Codeigniter

Also see version 0 screen shown below


Create Database Migration in Codeigniter

11.   Go to browser, type localhost/CITest/migrate/index and run

12.   Go to phpMyadmin and check table is created or not screen shown below


Create Database Migration in Codeigniter

Here you can see table successfully created

Also see structure of table screen shown below


Create Database Migration in Codeigniter

Also see the version 1, screen shown below


Create Database Migration in Codeigniter

At this time version change 0 to1 in citest_migrations table because test database contains only one table except the citest_migration table.

Example:

Now I want to create another  table for example table name is customer having folloing fields

     cus_id
     cus_name
     cus_email
     cus_mobile
     cus_address

Now create another file inside application/migrations folder give the file named as 002_application2.php, open that file and paste the code, shown below

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Migration_application2 extends CI_Migration {
  
  public function up()
  {
    
    if(!$this->db->table_exists('customer'))
    {
      $this->dbforge->add_field(array(
        'cus_id' => array(
        'type' => 'int',
        'constraint' => 9,
        'unsigned' => true,
        'auto_increment' => true
        ),
        'cus_name' => array(
        'type' => 'varchar',
        'constraint' => 100,
        'null' => false
        ),
        'cus_email' => array(
        'type' => 'varchar',
        'constraint' => 50,
        'null' => false
        ),
        'cus_phone' => array(
        'type' => 'varchar',
        'constraint' => 20,
        'null' => false
        ),
        'cus_address' => array(
        'type' => 'varchar',
        'constraint' => 255,
        'null' => false
        ),
      ));

        $this->dbforge->add_key('cus_id', true);
        $this->dbforge->create_table('customer', true);
    }   
  }
  
  public function down()
  {
    
  }

}

Go go to browser, type localhost/CITest/migrate/index and run

Go to phpMyadmin and check, table is created or not screen shown below


Create Database Migration in Codeigniter

Here you can see table successfully created

Also see structure of table, screen shown below


Create Database Migration in Codeigniter

Also see the version 1 screen shown below


Create Database Migration in Codeigniter


At this time version 2 in citest_migrations table because test database contains two table except the citest_migration table.





                I hope, it helped you to understand, How to  Create Database Migration in Codeigniter......Thanks

About MSK TUTORIAL

Hi...I am Md Sahjad Karim and I am Engineering Graduate (B.E in Computer Science and Engineering) working as a Web Developer and write Blog Articles. http://www.msktutorial.com/ was launched on july 2016 to provide free online hints, tips, guides and tutorials related to technology(PHP, CodeIgniter, WordPress, Blogger etc). My primary goal is to provide the good, quality and easily to understand the articles to readers around the world.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment

Thanks For Visiting Blog...