如何运行 CodeIgniter 迁移



我知道如何通过 http://codeigniter.com/user_guide/libraries/migration.html 创建它们

但是,一旦我创建了迁移文件,如何运行它们?

使用以下页面作为参考: 通过 CLI 运行和迁移类您可以使用以下行(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());
    }
  }
}

然后,要执行最新的迁移,请 cd 到项目目录的根目录并运行:

php index.php migrate

但是当您尝试通过 Web 服务器访问时example.com/migrate您将在上面的脚本中看到文本。

我不确定这是正确的方法,但它对我有用。

我创建了一个名为 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 以migrate控制器
中执行index操作例如:http://localhost/index.php/migrate/index/1

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

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是迁移的版本。如果版本更多是当前迁移 - 向上迁移,否则 - 向下到输入的版本。

这是最简单的 Codeigniter 数据库迁移

  1. 将应用程序/数据库.php配置为数据库名称设置。

  2. 创建应用程序/配置迁移.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. 在"应用程序\迁移.php"中,更改$config['migration_enabled'] = TRUE;

  4. 在文件夹中打开 CLI 并键入 php index.php migrate

我想

我这里有最简单的解决方案。(这是针对代码点火器 3.1.11(

上面的解决方案建议通过 url 或 cli 进行迁移。

第一个问题是你公开了这个应该在幕后的问题。

第二个问题是,如果您在共享托管平台上部署此项目,则没有机会通过 cli 运行它。

因此,我认为最简单的解决方案是让编码点火器为我们完成工作:(假设您已完成数据库设置并正确创建了迁移(

  1. 在/application/config/migration 中$config['migration_enabled'] = TRUE;.php
  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. (
  6. 只需运行您的应用程序(通过浏览器打开它(

塔!给你。您可以检查数据库是否已正确创建表。

开发环境应该可以保留这样的设置。

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

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

在应用程序\迁移.php中,更改 $config['migration_enabled'] = TRUE; 这是实际的 CI 迁移.php文件路径右侧

你说应用程序\迁移.php,实际是应用程序\配置\迁移.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";
        }
    }enter code here
}

CI 版本 4 答案 (2022(:

在版本 4 中略有不同:https://codeigniter.com/user_guide/dbmgmt/migration.html

namespace AppControllers;
use CodeIgniterController;
use Throwable;
class Migrate extends Controller {
    public function index(){
        $migrate = ConfigServices::migrations();
        try {
            $migrate->latest();
            $migrate->regress();
            echo 'Migration complete';
        } catch (Throwable $e) {
            print'<pre>';print_r($e);print'</pre>';
        }
    }
}

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

最新更新