How do I run CodeIgniter migrations?
Asked Answered
R

8

50

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?

Ryurik answered 5/2, 2012 at 23:26 Comment(1)
github.com/AimalAzmi/codeigniter-migrations Try this, I've written a library for this which can be used very easily through the CLI. It can be used to create migrations files and run migrations backwards or forwards.Flamboyant
S
34

I am not sure this is the right way to do it, But It works for me.

I created a controller named migrate (controllers/migrate.php).

<?php defined("BASEPATH") or exit("No direct script access allowed");

class Migrate extends CI_Controller{

    public function index($version){
        $this->load->library("migration");

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

Then from browser I will call this url to execute index action in migrate controller
Eg : http://localhost/index.php/migrate/index/1

Secluded answered 8/2, 2012 at 7:32 Comment(2)
Once after the migration I recommend you to remove this controller from server until the next migration.This is a public url and if you keep this in server anyone can easily drop your tables.Secluded
I think you'd only want to execute based on ENVIRONMENTRyurik
B
66

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 example.com/migrate you will see the text in the script above.

Barnabe answered 31/7, 2012 at 16:25 Comment(4)
Note that the above answer only works with CI2. CI3 installations should save the file as application/controllers/Migrate.phpEdda
I am trying to get the above example to work, but it fails when trying to load the migration library, when I access it from the CLI. If I comment out the is_cli_request line it works perfectly in browser. Any ideas?Luther
@Luther sounds like potentially a change in CI to the is_cli_request(), verify your CI version and see if the call is different. Commenting it out would then allow migrations to be run in browser, which is what should be avoided.Barnabe
github.com/AimalAzmi/codeigniter-migrations Try this, I've written a library for this which can be used very easily through the CLI. It can be used to create migrations files and run migrations backwards or forwards.Flamboyant
S
34

I am not sure this is the right way to do it, But It works for me.

I created a controller named migrate (controllers/migrate.php).

<?php defined("BASEPATH") or exit("No direct script access allowed");

class Migrate extends CI_Controller{

    public function index($version){
        $this->load->library("migration");

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

Then from browser I will call this url to execute index action in migrate controller
Eg : http://localhost/index.php/migrate/index/1

Secluded answered 8/2, 2012 at 7:32 Comment(2)
Once after the migration I recommend you to remove this controller from server until the next migration.This is a public url and if you keep this in server anyone can easily drop your tables.Secluded
I think you'd only want to execute based on ENVIRONMENTRyurik
U
6

You can also run some version for down or up migrations:

if(!defined('BASEPATH')) exit('No direct script access allowed');
class Migrate extends CI_Controller{

    public function __construct()
    {
        parent::__construct();
        $this->load->library('migration');
    }

     public function version($version)
     {
         if($this->input->is_cli_request())
         {
            $migration = $this->migration->version($version);
            if(!$migration)
            {
                echo $this->migration->error_string();
            }
            else
            {
                echo 'Migration(s) done'.PHP_EOL;
            }
        }
        else
        {
            show_error('You don\'t have permission for this action');;
        }
     }
 }

For CLI run this command php index.php migrate version 5, where 5 is version of migration. If version is more of current migration - migration up, else - down to entered version.

Ullage answered 26/7, 2013 at 16:16 Comment(0)
C
1

This is simplest Codeigniter Database Migrations

  1. Configure application/database.php to your database name settings.

  2. Create application/config mirate.php

    <?php defined("BASEPATH") or exit("No direct script access allowed");
    
    class Migrate extends CI_Controller
    {
        public function index()
        {
            if (ENVIRONMENT == 'development') {
                $this->load->library('migration');
                if (!$this->migration->current()) {
                    show_error($this->migration->error_string());
                } else {
                    echo "success";
                }
            } else {
                echo "go away";
            }
        }
    }
    
  3. In application\migration.php, change $config['migration_enabled'] = TRUE; .

  4. open CLI in folder and type php index.php migrate

Correspondence answered 22/12, 2017 at 6:57 Comment(0)
E
1

I think I have the simplest solution around here. (This is for Codeigniter 3.1.11)

The solutions above either suggest doing the migration through url or cli.

Problem with the first one is you make this should-be-behind-the-scenes issue publicly available.

Problem with the second one is if you are deploying this project on shared hosting platform you don't have the chance to run it via cli.

So the simplest solution to my opinion is to let codeigniter do the work for us: (assuming that you have done your database settings and created migrations properly)

  1. make $config['migration_enabled'] = TRUE; in /application/config/migration.php
  2. define migration version $config['migration_version'] = 20201019123900; like this
  3. set $config['migration_auto_latest'] = TRUE;
  4. autoload or manually load migration library (define it in /application/config/autoload.php like $autoload['libraries'] = array('migration'); or load it in controller constructor like $this->load->library('migration');)
  5. and just run your application (open it via browser)

Tata! There you go. You can check your database if your tables have been created correctly.

It should be OK for development environment to leave the settings like this.

BUT for production environment we should reset $config['migration_enabled'] = FALSE; as it is pointed out in the comment section.

This solution might be criticized because migration library is loaded each time the application or the controller is run but I haven't said it's the best solution, I have said it's the simplest solution.

Encaenia answered 19/10, 2020 at 10:36 Comment(0)
L
0

In application\migration.php, change $config['migration_enabled'] = TRUE; .this is the actual CI migration.php file path right

You said that application\migration.php, actual is application\config\migration.php.

Lail answered 12/6, 2021 at 4:46 Comment(0)
D
0
<?php defined("BASEPATH") or exit("No direct script access allowed");

class Migrate extends CI_Controller
{
    public function index()
    {
        if (ENVIRONMENT == 'development') {
            $this->load->library('migration');
            if (!$this->migration->current()) {
                show_error($this->migration->error_string());
            } else {
                echo "success";
            }
        } else {
            echo "go away";
        }
    }enter code here
}
Dna answered 29/9, 2021 at 7:33 Comment(0)
P
0

A CI version 4 answer (2022):

In version 4 it is slightly different: https://codeigniter.com/user_guide/dbmgmt/migration.html

namespace App\Controllers;

use CodeIgniter\Controller;
use Throwable;

class Migrate extends Controller {

    public function index(){
        $migrate = \Config\Services::migrations();

        try {
            $migrate->latest();
            $migrate->regress();

            echo 'Migration complete';
        } catch (Throwable $e) {
            print'<pre>';print_r($e);print'</pre>';
        }
    }
}

Note I've included the regress option if you wish to roll back your migrations as well.

Publicly answered 4/11, 2022 at 15:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.