【问题标题】:How do I run CodeIgniter migrations?如何运行 CodeIgniter 迁移?
【发布时间】:2012-02-27 13:42:19
【问题描述】:

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

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

【问题讨论】:

标签: codeigniter migration codeigniter-2


【解决方案1】:

使用这些页面作为参考:Running via the CLIMigration Class 您可以通过以下方式(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

但是当您尝试通过 webserver domain.com/migrate 访问时,您会看到上面脚本中的文本。

【讨论】:

  • 请注意,上述答案仅适用于 CI2。 CI3 安装应将文件保存为 application/controllers/Migrate.php
  • 我试图让上面的示例工作,但是当我从 CLI 访问它时尝试加载迁移库时它失败了。如果我注释掉 is_cli_request 行,它可以在浏览器中完美运行。有什么想法吗?
  • @ViggoV 听起来可能是对is_cli_request() 的 CI 更改,请验证您的 CI 版本并查看调用是否不同。将其注释掉将允许在浏览器中运行迁移,这是应该避免的。
  • github.com/AimalAzmi/codeigniter-migrations 试试这个,我为此编写了一个库,可以通过 CLI 轻松使用。它可用于创建迁移文件并向后或向前运行迁移。
【解决方案2】:

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

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

【讨论】:

  • 迁移后,我建议您从服务器中删除此控制器,直到下一次迁移。这是一个公共 url,如果您将其保留在服务器中,任何人都可以轻松删除您的表。
  • 我想你只想基于ENVIRONMENT执行
【解决方案3】:

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

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

【讨论】:

    【解决方案4】:

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

    1. 将 application/database.php 配置为您的数据库名称设置。

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

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

    【讨论】:

      【解决方案5】:

      我想我有最简单的解决方案。 (这是针对 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-&gt;load-&gt;library('migration');
      5. 然后运行您的应用程序(通过浏览器打开)

      塔塔!你去吧。如果您的表已正确创建,您可以检查您的数据库。

      开发环境保留这样的设置应该没问题。

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

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

      【讨论】:

        【解决方案6】:

        在 application\migration.php 中,更改 $config['migration_enabled'] = TRUE; .this is the actual CI migration.php file path right

        你说的是application\migration.php,实际是application\config\migration.php。

        【讨论】:

          【解决方案7】:
          <?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
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-04-27
            • 2017-04-09
            • 2017-09-03
            • 1970-01-01
            相关资源
            最近更新 更多