How do I run CodeIgniter migrations?

Shamoon picture Shamoon · Feb 6, 2012 · Viewed 43.8k times · Source

I know how to create them via http://codeigniter.com/user_guide/libraries/migration.html

But once I've created my migration files, how do I run them?

Answer

twmulloy picture twmulloy · Jul 31, 2012

Using these pages as references: Running via the CLI and Migration Class you're able to restrict access to your migration controller to command line with something along these lines (application/controllers/migrate.php):

<?php  if ( ! defined('BASEPATH')) exit("No direct script access allowed");

class Migrate extends CI_Controller {

  public function __construct()
  {
    parent::__construct();

    $this->input->is_cli_request() 
      or exit("Execute via command line: php index.php migrate");

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

  public function index()
  {
    if(!$this->migration->latest()) 
    {
      show_error($this->migration->error_string());
    }
  }
}

then to execute your latest migration, cd into the root of your project directory and run:

php index.php migrate

but when you attempt to access via webserver domain.com/migrate you will see the text in the script above.