我知道如何通过http://codeigniter.com/user_guide/libraries/migration.html创建它们。
但是,一旦我创建了迁移文件,我该如何运行它们呢?
我知道如何通过http://codeigniter.com/user_guide/libraries/migration.html创建它们。
但是,一旦我创建了迁移文件,我该如何运行它们呢?
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
时,你会看到上面脚本中的文本。is_cli_request()
中更改 CI 的问题,请验证您的 CI 版本并查看调用是否不同。注释掉它将允许在浏览器中运行迁移,这是应该避免的。 - skilleo我不确定这是否是正确的方法,但对我有效。
我创建了一个名为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
您也可以运行一些版本进行向下或向上迁移:
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
是迁移版本。如果版本高于当前迁移版本,则向上迁移;否则,向下迁移到输入的版本。
Configure application/database.php to your database name settings.
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";
}
}
}
In application\migration.php, change $config['migration_enabled'] = TRUE;
.
open CLI in folder and type php index.php migrate
完成了!你可以检查你的数据库,看看表格是否已经正确创建。
在开发环境中,这样的设置应该是没问题的。
但是在生产环境中,我们应该重置$config['migration_enabled'] = FALSE;
,正如注释部分所指出的那样。
这种解决方案可能会受到批评,因为每次运行应用程序或控制器时都会加载迁移库,但我并没有说这是最好的解决方案,我说的是这是最简单的解决方案。
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>';
}
}
}
<?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
}