49

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?

RSK
  • 17,210
  • 13
  • 54
  • 74
Shamoon
  • 41,293
  • 91
  • 306
  • 570
  • https://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. – Aimal Azmi Nov 21 '17 at 10:40

8 Answers8

66

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

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
skilleo
  • 2,451
  • 1
  • 27
  • 34
  • 3
    Note that the above answer only works with CI2. CI3 installations should save the file as application/controllers/Migrate.php – Luke A. Leber Mar 28 '16 at 15:10
  • 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? – ViggoV Sep 07 '16 at 09:40
  • @ViggoV 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. – skilleo Sep 14 '17 at 15:57
  • https://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. – Aimal Azmi Nov 21 '17 at 10:32
33

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

RSK
  • 17,210
  • 13
  • 54
  • 74
  • 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. – RSK Feb 08 '12 at 16:13
  • 12
    I think you'd only want to execute based on `ENVIRONMENT` – Shamoon Feb 09 '12 at 16:02
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.

joni jones
  • 2,865
  • 3
  • 23
  • 28
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

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Bidyashish Kumar
  • 306
  • 4
  • 10
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.

Adem Tepe
  • 564
  • 5
  • 10
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.

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
}
Pooja
  • 1
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.

Antony
  • 3,875
  • 30
  • 32