我该如何运行CodeIgniter迁移?

50

尝试一下这个,我写了一个库,可以通过CLI非常容易地使用它。它可以用于创建迁移文件并向后或向前运行迁移。这是库的链接:https://github.com/AimalAzmi/codeigniter-migrations - Aimal Azmi
8个回答

66
你可以通过以下方式将对迁移控制器的访问限制在命令行上(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());
    }
  }
}

然后要执行最新的迁移,进入项目目录的根目录并运行以下命令:
php index.php migrate

但是当你尝试通过网页服务器访问example.com/migrate时,你会看到上面脚本中的文本。

3
请注意,上面的答案仅适用于CI2。CI3安装应将文件保存为application/controllers/Migrate.php。 - Luke A. Leber
我正在尝试让上面的示例工作,但是当我从CLI访问时尝试加载迁移库时它失败了。如果我注释掉is_cli_request行,在浏览器中它完美地工作。有什么想法吗? - ViggoV
@ViggoV 听起来可能是在 is_cli_request() 中更改 CI 的问题,请验证您的 CI 版本并查看调用是否不同。注释掉它将允许在浏览器中运行迁移,这是应该避免的。 - skilleo
尝试一下这个,我写了一个库,可以通过CLI非常容易地使用它。它可以用于创建迁移文件并向前或向后运行迁移。这是库的链接:https://github.com/AimalAzmi/codeigniter-migrations - Aimal Azmi

33

我不确定这是否是正确的方法,但对我有效。

我创建了一个名为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());
      }   
    }
}

然后我会从浏览器中调用这个URL来执行控制器中的操作
例如:http://localhost/index.php/migrate/index/1


2
迁移后,建议您将此控制器从服务器中删除,直到下一次迁移。这是一个公共网址,如果您在服务器上保留它,任何人都可以轻易地删除您的表。 - RSK
12
我认为你只想根据“环境”执行。 - Shamoon

6

您也可以运行一些版本进行向下或向上迁移:

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');;
        }
     }
 }

对于CLI,请运行以下命令php index.php migrate version 5,其中5是迁移版本。如果版本高于当前迁移版本,则向上迁移;否则,向下迁移到输入的版本。


1
这是最简单的Codeigniter数据库迁移。
  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


1
我认为我这里有最简单的解决方案。(适用于Codeigniter 3.1.11)
以上的解决方案都建议通过url或cli进行迁移。
第一个问题是你让这个本应该在幕后处理的问题公开可见。
第二个问题是,如果你在共享托管平台上部署这个项目,你就没有机会通过cli运行它。
所以我认为最简单的解决方案是让codeigniter为我们做这件事:(假设你已经完成了数据库设置和迁移的创建)
1. 在/application/config/migration.php中将$config['migration_enabled'] = TRUE; 2. 像这样定义迁移版本$config['migration_version'] = 20201019123900; 3. 设置$config['migration_auto_latest'] = TRUE; 4. 自动加载或手动加载迁移库(在/application/config/autoload.php中定义它,如$autoload['libraries'] = array('migration'); 或在控制器构造函数中加载它,如$this->load->library('migration');) 5. 然后只需运行你的应用程序(通过浏览器打开)。

完成了!你可以检查你的数据库,看看表格是否已经正确创建。

在开发环境中,这样的设置应该是没问题的。

但是在生产环境中,我们应该重置$config['migration_enabled'] = FALSE;,正如注释部分所指出的那样。

这种解决方案可能会受到批评,因为每次运行应用程序或控制器时都会加载迁移库,但我并没有说这是最好的解决方案,我说的是这是最简单的解决方案。


0

CI 4版本的答案(2022):

在版本4中,稍有不同: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>';
        }
    }
}

请注意,如果您希望回滚迁移,我已经包含了回归选项。

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
}

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接