Codeigniter迁移+多个表+每个表一个迁移文件

时间:2012-03-28 20:17:38

标签: mysql codeigniter migration

我想开始使用内置于CI中的DBForge和迁移类,但我不确定如何为我的所有表创建迁移。

我的想法是在我的安装程序过程中为以下每个表创建一个迁移文件:advertisements, announcements, config, users, points ..当用户安装应用程序时,它将自动运行这些迁移文件并创建表。

IE:001_advertisements, 001_announcements, 001_config, 001_users, 001_points

001_map_advertisements

class Migration_map_advertisements extends CI_Migration {

public function up(){
    $this->dbforge->add_field(array(
        'id' => array(
            'type' => 'INT',
            'constraint' => 11,
            'unsigned' => TRUE,
            'auto_increment' => TRUE
        ),
        'youtube_id' => array(
            'type' => 'VARCHAR',
            'constraint' => '255',
        ),
        'status' => array(
            'type' => 'int',
            'constraint' => 11,
            'null' => FALSE,
            'default' => 1
        ),
        'timestamp' => array(
            'type' => 'int',
            'constraint' => 11
        ),
        'type' => array(
            'type' => 'VARCHAR',
            'default' => 'video'
        ),
        'filename' => array(
            'type' => 'VARCHAR',
            'constraint' => '255'
        ),
        'url' => array(
            'type' => 'varchar',
            'constraint' => '255'
        ),
        'description' => array(
            'type' => 'varchar',
            'constraint' => 64
        ),
        'title' => array(
            'type' => 'varchar',
            'constraint' => 64
        )
    ));
    $this->dbforge->create_table('map_advertisements', TRUE);
}

public function down()
{
    $this->dbforge->drop_table('map_advertisements');
}

但是,如果我这样做并尝试运行安全控制器:

class Developer extends ADMIN_Controller {
function __construct(){
    parent::__construct();
} #end constructor function
public function migrate($index){
    $this->load->library('migration');

    if ( !$this->migration->version($index) ){
        show_error($this->migration->error_string());
    }else{
        echo 'migrated';
    }

}
}

我收到一条错误消息,指出有多个version 1迁移,但迁移过程失败。在CI中还有另一种方法吗?

2 个答案:

答案 0 :(得分:7)

是的,每个版本只能有一个迁移文件。迁移不是特定于表的,但更像是“从我的应用程序的版本x到版本x + 1需要哪些架构更改?”

将所有现有迁移文件合并为1,并将其命名为001_initial_schema.php。然后,在添加新功能时,为该功能创建新的模式文件。如果您有部署周期(如每周SCRUM sprint),那么每次部署都可以使用其中一个迁移文件。

答案 1 :(得分:1)

根据http://www.codeigniter.com/user_guide/libraries/migration.html,“版本化”迁移的最佳方法是使用YYYYMMDDHHMMSS格式。这应该有效地克服所表达的版本问题。还建议如果文件名为“201507071208_advertisements.php”,则包含的迁移类应称为“Migration_Advertisements”。

相关问题