How do I run CodeIgniter migrations?

2019-01-21 10:31发布

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?

5条回答
一纸荒年 Trace。
2楼-- · 2019-01-21 11:06

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.

查看更多
We Are One
3楼-- · 2019-01-21 11:13

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.

查看更多
祖国的老花朵
4楼-- · 2019-01-21 11:13

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";
      }
    }
  }
?> 

`.

  1. In application\migration.php change $config['migration_enabled'] = TRUE; .
  2. open CLI in folder and type php index.php migrate
查看更多
你好瞎i
5楼-- · 2019-01-21 11:17

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

查看更多
Explosion°爆炸
6楼-- · 2019-01-21 11:19

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.

查看更多
登录 后发表回答