如何在CodeIgniter中一次运行多个迁移文件


How to run multiple migrate file in CodeIgniter at once?

我正在创建一个迁移,在第一个表似乎还好,但当我创建我的第二次迁移没有错误,但表没有创建。

我这样命名我的2迁移类:

001_inititial_schema.php
002_create_table_quotation_header.php

第一个包含这个:

class Migration_Initial_Schema extends CI_Migration {
    public function up() {
        $this->dbforge->add_field(array(
            'id' => array(
                'type' => 'int',
                'constraint' => 11,
                'unsigned' => true,
                'auto_increment' => true
            ),
            'project_name' => array(
                'type' => 'varchar',
                'constraint' => 100,
            ),
            'description' => array(
                'type' => 'text'
            ),
            'date_created' => array(
                'type' => 'datetime',
            ),
            'date_updated' => array(
                'type' => 'datetime',
            ),
            'status' => array(
                'type' => 'tinyint',
                'default' => 1
            ),
        ));
        $this->dbforge->add_key('id', TRUE);
        $this->dbforge->create_table('flx_project');
    }
    public function down() {
        $this->dbforge->drop_table('flx_project');
    }

}

然后第二个:

    class CreateTableQuotationHeader extends CI_Migration {
    public function up() {
        $this->dbforge->add_field(array(
            'id' => array(
                'type' => 'int',
                'constraint' => 11,
                'unsigned' => true,
                'auto_increment' => true
            ),
            'project_id' => array(
                'type' => 'int',
                'constraint' => 11,
                'unsigned' => true,
            ),
            'receiver' => array(
                'type' => 'varchar',
                'constraint' => 100,
            ),
            'address' => array(
                'type' => 'text',
            ),
            'attention' => array(
                'type' => 'varchar',
                'constraint' => 100
            ),
            'reference_number' => array(
                'type' => 'varchar',
                'constraint' => 50
            ),
            'date_issued' => array(
                'type' => 'date'
            )
        ));
        $this->dbforge->add_key('id', TRUE);
        $this->dbforge->create_table('flx_project_header');
    }
    public function down() {
        $this->dbforge->drop_table('flx_project_header');
    }

}

然后在我的控制器中:

<?php
class Migrate extends CI_Controller {
    public function __construct() {
        parent::__construct(0);
        $this->load->library('migration');
    }
    public function index() {
        $this->load->helper('template');
        if(!$this->migration->current()) {
            show_error($this->migration->error_string());
        } else {
            $data['message'] = 'migrate success';
        }
        renderPage('common/migrate', $data);
    }
}
?>

好了,我解决了我的问题,我所做的是我只在一个文件中包含两个迁移。但我不知道这是否是一个正确的方式在运行迁移有多个表。

<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Migration_Initial_Schema extends CI_Migration {
    public function up() {
        $this->dbforge->add_field(array(
            'id' => array(
                'type' => 'int',
                'constraint' => 11,
                'unsigned' => true,
                'auto_increment' => true
            ),
            'project_name' => array(
                'type' => 'varchar',
                'constraint' => 100,
            ),
            'description' => array(
                'type' => 'text'
            ),
            'date_created' => array(
                'type' => 'datetime',
            ),
            'date_updated' => array(
                'type' => 'datetime',
            ),
            'status' => array(
                'type' => 'tinyint',
                'default' => 1
            ),
        ));
        $this->dbforge->add_key('id', TRUE);
        $this->dbforge->create_table('flx_project');
        $this->dbforge->add_field(array(
            'blog_id' => array(
                'type' => 'INT',
                'constraint' => 5,
                'unsigned' => TRUE,
                'auto_increment' => TRUE
            ),
            'blog_title' => array(
                'type' => 'VARCHAR',
                'constraint' => '100',
            ),
            'blog_description' => array(
                'type' => 'TEXT',
                'null' => TRUE,
            ),
        ));
        $this->dbforge->add_key('blog_id', TRUE);
        $this->dbforge->create_table('blog');
    }
    public function down() {
        $this->dbforge->drop_table('flx_project');
        $this->dbforge->drop_table('blog');
    }

}
?>