Codeigniter 3.1.11笔记(4) -- 数据库Migration之创建表格以及回滚演示

    技术2022-07-12  81

    数据库迁移是几乎所有MVC模式的框架都会涉及到的内容,其目的在于便于数据库相关的更新维护,比方说,在某个时间点,某个表需要修改结构(增加或者删除一列的),一个办法就是对修改前的和修改后的(数据库)数表格分别做个状态标记,迁移的程序也就起到了数据库层面上的版本控制作用,用以对应修改前以及修改后的版本。

    0.配置好数据库(略)

    1.创建第一个状态下的数据表格--状态0

    创建一个标号为0的数据库(数据表)状态,这个状态下,只创建一个叫posts的表格,其表格结构见下面的代码.

    application/migrations/001_create_posts.php.这里,我的ci3默认没有创建migrations文件夹,而application/config/migrate.php却进行了配置,或许是个bug。

    <?php class Migration_Create_Posts extends CI_Migration{ // used when create a table public function up(){ $this->dbforge->add_field(array( 'id'=>array( 'type'=>'INT', 'constraint'=>5, 'unsigned'=>TRUE, 'auto_increment'=>TRUE ), 'title'=>array( 'type'=>'VARCHAR', 'constraint'=>100 ), 'body'=>array( 'type'=>'TEXT', 'null'=>TRUE ), )); $this->dbforge->add_key('id',true); $this->dbforge->create_table('posts'); } // used when drop the table public function down(){ $this->dbforge->drop_table('posts'); } } ?>

    修改application/config/migration.php 中的选项如下,

    $config['migration_enabled'] = TRUE; $config['migration_type'] = 'sequential'; $config['migration_version'] = 1;

    这里,migration_version = 1与001_create_posts.php 名称中的001对应,这个选项是控制状态的关键参数。

    创建一个控制器,application/controllers/Migrate.php,做个测试,这是唯一用到的controller

    <?php defined('BASEPATH') OR exit('No direct script access allowed'); class Migrate extends CI_Controller { public function __construct(){ parent::__construct(); } public function index() { $this->load->library('migration'); if($this->migration->current() === false){ show_error($this->migration->error_string()); }else{ echo 'Migration executed'; } } }

    最后:做个测试, http://localhost/ci3/index.php/Migrate

    显示的结果是:Migration executed

    而查看mysql数据库会发现,有两个table建立成功。

    2.创建第而个状态下的数据表格--状态1

    更新 application/config/migration.php出个状态2

    $config['migration_version'] = 2;

    application/migrations/002_create_users.php

    <?php class Migration_Create_Users extends CI_Migration{ // used when create a table public function up(){ $this->dbforge->add_field(array( 'id'=>array( 'type'=>'INT', 'constraint'=>5, 'unsigned'=>TRUE, 'auto_increment'=>TRUE ), 'email'=>array( 'type'=>'VARCHAR', 'constraint'=>100 ), 'name'=>array( 'type'=>'VARCHAR', 'constraint'=>100 ), )); $this->dbforge->add_key('id',true); $this->dbforge->create_table('users'); } // used when drop the table public function down(){ $this->dbforge->drop_table('users'); } } ?>

    再测试一次:http://localhost/ci3/index.php/Migrate

    然后打开数据库会发现users表格被创建了出来。

    3.做一次回滚测试

    application/config/migrate.php

    $config['migration_version'] = 1;

    再测试一次:http://localhost/ci3/index.php/Migrate

    然后打开数据库会发现users表格已经消失。

    4.修改数据表

    application/migrations/003_alter_posts.php

    <?php class Migration_Alter_Posts extends CI_Migration{ // used when create a table public function up(){ $field = array( 'created_at'=>array( 'type'=>'timestamp', ), ); $this->dbforge->add_column('posts',$field); } // used when drop the table public function down(){ $this->dbforge->drop_table('users'); } } ?>

    application/config/migrate.php

    $config['migration_version'] = 3;

    再次尝试 http://localhost/ci3/index.php/Migrate, 然后查看数据库,会发现数据表posts多了一列created_at.

    至此,数据表的创建、删除、修改都完成。

    齐活。

    Processed: 0.047, SQL: 9