fishingboy / codeigniter-migration
Codeigniter 迁移工具
1.0.4
2020-11-11 10:08 UTC
README
语言
安装
composer require fishingboy/codeigniter-migration
使用方法
-
创建文件:
application/controller/Migration.php
<?php use fishingboy\ci_migration\CI_Migration_Controller; class Migration extends CI_Migration_Controller { }
-
创建文件:
application/libraries/Migration.php
<?php use fishingboy\ci_migration\CI_Migration_Library; class CI_Migration extends CI_Migration_Library { }
-
修改文件:
application/config/Migration.php
$config['migration_enabled'] = true;
-
创建文件夹:
application/migrations
-
创建迁移文件: application/migrations/20001010101000_create_sample_tables.php
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Migration_Create_sample_tables extends CI_Migration { public function up() { $sql = "CREATE TABLE `users` ( `id` INT NOT NULL AUTO_INCREMENT , `name` VARCHAR(20) COMMENT 'name', `created_at` DATETIME NOT NULL , `updated_at` DATETIME NOT NULL , PRIMARY KEY (`id`) ) COMMENT = 'user';"; $this->db->query($sql); } public function down() { $sql = "DROP TABLE `users`"; $this->db->query($sql); } }
-
迁移帮助:
php index.php migration
$ php index.php migration migration php index.php migration -- help php index.php migration migrate -- execute migrations php index.php migration rollback -- rollback to prev migration php index.php migration ls -- check migrations list
-
迁移列表
$ php index.php migration ls Version Status File --- -------------- ------ ------------------------------------ 1. 20190815002100 -- application/migrations/20190815002100_create_logs_tables.php --- -------------- ------ ------------------------------------ 0 Migration not execute.
-
执行迁移
$ php index.php migration migrate Migration Run : Migration_Create_sample_tables::up() ............. OK !
-
执行迁移回滚
$ php index.php migration rollback Migration Run : Migration_Create_sample_tables::down() ............. OK !