Codeigniter Migrations Class Library


Codeigniter Migrations Class Library – We can load image manuipulation class library like this $this->load->library(‘migration’);. This library provide various functions that are used for automatically detects and run sql queries. We make some change in database by using migration class library. Codeigniter also allow us to setup migration. Here in this tutorial, we are going to explain how to use migration class library.


Codeigniter migration class library

Let us understand how migration class library works in codeigniter with examples.

Load migration class library

First load migration class library to use its functions, this library can be loaded simply as below-

How to load migration class library:

$this->load->library('migration');

Functions:-

There are following functions available in migration class library. Now we will explain one by one.

  • 1. Create a Migration.
  • 2. Delete a Migration.

1. Create a Migration.

Here is simple demo of Create a Migration.

This function allows users to create a migration.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class migration_controller extends CI_Controller 
{
public function up()
{
$this->load->library('migration');
$this->dbforge->add_field(array(
'id' => array('type' => 'INT', 'constraint' => 5,
'unsigned' => TRUE, 'auto_increment' => TRUE),
 'title' => array('type' => 'VARCHAR',
 'constraint' => '100'),
 'description' => array('type' => 'TEXT',
 'null' => TRUE)));
 $this->dbforge->add_key('id', TRUE);
 $this->dbforge->create_table('users');
if ($this->migration->current() === FALSE)
{
    show_error($this->migration->error_string());
}
else
{
    echo "database inserted successfully";
}
}
}
?>
Codeigniter Migrations Class Library

2. Delete a Migration.

Here is simple demo of delete a Migration.

This function allows users to delete a Migration.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class migration_controller extends CI_Controller 
{
public function down()
{
$this->load->library('migration');
$delete = $this->dbforge->drop_table('users');
if($delete==true)
{
echo "Migration deleted successfully";
}
}
}
?>
Codeigniter Migrations Class Library

Class reference:-

There are following references available in migration class library. Now we will explain.

1. Current.

This reference is used to set migrate up to the current version.

current()
  • Parameters :
  • Returns : TRUE if no migrations are found, current version string on success, FALSE on failure
  • Returns type : mixed
Example.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class migration_controller extends CI_Controller 
{
public function currentMigrate()
{
$this->load->library('migration');
if ($this->migration->current() === FALSE)
{
echo show_error($this->migration->error_string());
}
else{
echo "run successfully";
}
}
}
?>

Output will be like this:-

Codeigniter Migrations Class Library

2. Error string.

This function return error which is detected while performing a migration.

error_string()
  • Parameters :
  • Returns : Error messages
  • Returns type : String

3. Find migrations.

This function returned that are found in migration path .

find_migrations()
  • Parameters :
  • Returns : An array of migration files
  • Returns type : Array

4. latest.

This function returned that are found in migration path .

latest()
  • Parameters :
  • Returns : Current version string on success, FALSE on failure
  • Returns type : Mixed
Example.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class migration_controller extends CI_Controller 
{
public function latestMigrate()
{
$this->load->library('migration');
if ($this->migration->latest() === FALSE)
{
   echo show_error($this->migration->error_string());
}
else
{
echo "Latest migration run successfully";
}
}
}
?>

5. version.

Version function can be used to rollback changes to specific version.

latest()
  • Parameters :
  • $target_version (mixed) : Migration version to process
  • Returns : TRUE if no migrations are found, current version string on success, FALSE on failure
  • Returns type : Mixed
Example.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class migration_controller extends CI_Controller 
{
public function version()
{
if($this->input->is_cli_request())
{
$this->load->library('migration');
$migration = $this->migration->version($version);
if(!$migration)
{
   echo $this->migration->error_string();
}
else
{
   echo 'Migration(s) done'.PHP_EOL;
}
}
else
{
  echo 'You don\'t have permission for this action';
}
}
}
?>

Output will be like this:-

Codeigniter Migrations Class Library

Advertisements

Add Comment

📖 Read More