如何运行 CodeIgniter 迁移?

发布于 2025-01-02 17:32:32 字数 210 浏览 0 评论 0原文

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

韬韬不绝 2025-01-09 17:32:33

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

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

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.

别挽留 2025-01-09 17:32:33

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

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

  2. 创建应用程序/配置 mirate.php

    load->library('迁移');
                if (!$this->迁移->current()) {
                    show_error($this->迁移->error_string());
                } 别的 {
                    回显“成功”;
                }
            } 别的 {
                回声“走开”;
            }
        }
    }
    
  3. 在 application\migration.php 中,更改 $config['migration_enabled'] = TRUE;

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

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

猫九 2025-01-09 17:32:33

我想我这里有最简单的解决方案。 (适用于 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. ,然后运行您的应用程序(通过浏览器打开它)

Tata!就这样吧。您可以检查数据库是否正确创建了表。

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

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

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

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.

故人的歌 2025-01-09 17:32:33

在 application\migration.php 中,更改 $config['migration_enabled'] = TRUE; .这是实际的 CI migration.php 文件路径对吧

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

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.

第几種人 2025-01-09 17:32:33
<?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
}
<?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
}
兔姬 2025-01-09 17:32:33

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

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

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.

煮酒 2025-01-09 17:32:32

您可以通过以下方式将对迁移控制器的访问限制为命令行(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

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

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.

饮湿 2025-01-09 17:32:32

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

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

然后从浏览器中我将调用此网址以在 migrate 控制器中执行 index 操作
例如:http://localhost/index.php/migrate/index/1

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文