一、migrate命令

由于Yii migrate 生成的迁移文件默认是存放在 app/migrations 目录下面,如果想要生成的迁移文件到自己指定的目录(例如 dir/migrations),加上如下参数:

yii migrate/create test_table -p=@dir/migrations

如果需要指定到特定的数据库,加上如下参数:

yii migrate/create test_table --db=dbName

查看migrate帮助: ./yii help migrate

生成一个迁移文件的命令:./yii migrate/create  test_table -p=@dir/migrations  --db=dbName

执行该迁移文件可使用  ./yii migrate/to  m180227_023510_test_table -p=@dir/migrations --db=dbName

查看 dbName 的最近 n 条历史记录:./yii migrate/history n

还原 dbName 最近的 n 条数据库迁移: ./yii migrate/down n --db=dbName

还原上一次执行的迁移文件可使用  ./yii migrate/down  -p=@dir/migrations 

执行全部 migrations :./yii migrate/auto

执行某个特定目录下面的migrations :./yii migrate -p=@dir/migrations/test  --db=dbName

二、migration文件写法

1、migrate 的标准格式:

<?php
/*
* @purpose : migrate的标准格式
* @author : daicr
* @time : 2018-05-7
* */
use yii\db\Migration; class m180505_051217_table_name_init extends Migration
{
public $db = 'db1';
public $tableName = 'table_name'; public function init()
{
//$this->tableName = 'db2.table_name_2'; // 如果要修改另一个库的表加上这句
parent::init();
} public function up()
{
$tableOptions = null;
if($this->db->driverName='mysql'){
$tableOptions = 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB';
} $this->createTable($this->tableName,[
'id'=>$this->primaryKey(11)->unsigned()->notNull()->comment('主键ID'),
'name'=>$this->string(32)->notNull()->defaultValue('')->comment('姓名'),
'remarks'=>$this->text()->comment('备注'); ... 'created_at'=>$this->integer(11)->notNull()->defaultValue(0)->comment('创建时间'),
'updated_at'=>$this->integer(11)->notNull()->defaultValue(0)->comment('更新时间'),
'is_delete'=>$this->smallInteger(1)->notNull()->defaultValue(0)->comment('是否删除,1:是,0:否'),
],$tableOptions.' COMMENT "用户表"');
} public function down()
{
$this->dropTable($this->tableName);
return true;
} }

2、在迁移文件中执行原生 sql :

public function safeUp()
{
$this->execute(<<<EOF
INSERT INTO tableName VALUES ('1', 'value1', 'value2', 'value3');
INSERT INTO tableName VALUES ('2', 'value1', 'value2', 'value3');
EOF
);
}

3、添加和删除字段:

    public function up()
{
$this->addColumn($this->tableName,'test_field', $this->integer()->notNull()->defaultValue()->comment('测试字段'));
} public function down()
{
$this->dropColumn($this->tableName, 'im_customer_id'); return true;
}

4、批量迁移多张表

<?php

use yii\db\Migration;

/**
* @purpose: xxx模块儿表迁移文件
* Class m190528_031851_organize_tables_init
*/
class m190528_031851_fusionpbx_tables_init extends Migration
{
public $db = 'dbConf'; public $tableName = 'conf_field'; public function init()
{
$this->db = 'dbConf'; parent::init();
} /**
* {@inheritdoc}
*/
public function safeUp()
{
$map = call_user_func([$this,'map']); foreach($map as $key => $val){
$data = $this->migrateSchema($key,call_user_func([$this,$val]));
$this->batchInsert($this->tableName, array_keys($data[0]), $data);
}
} /**
* {@inheritdoc}
*/
public function safeDown()
{
$map = call_user_func([$this,'map']);
foreach ($map as $key => $val){
$arrField = call_user_func([$this,$val]);
foreach ($arrField as $v){
$this->delete($this->tableName,['table_name'=>$key]);
}
} return true;
} /**
* @purpose: 定义表和表字段方法的对应关系
* @return array
*/
public function map()
{
return [
//用户表
'user' => 'defineUserTable',
//部门表
'dept' => 'defineDeptTable',
];
} /**
* @purpose: 合并默认值和私有值
* @param string $table 表明
* @param array $data 各个字段的私有值
* @return array $data 合并后的数组
*/
public function migrateSchema($table,$data)
{
$default = [
'company_id' => COMPANY_ID, //企业编号
'branch_id' => BRANCH_ID, //网点编号
'belong_module' => 'organize', //所属模块, ticket:工单 crm:客户资料
'table_name' => $table, //字段所属表名
'table_type' => '', //所属类型(如工单的业务类型)
'field_name' => '', //字段名
'field_label' => '', //字段标签
'field_type' => 'cf', //字段类型 fixed:固定字段 cf:自定义字段
'extra_fields' => '', //固定字段输出时需额外获取的字段
'input_type' => 'text', //输入控件类型
'input_options' => '', //输入控件的选项值,以json格式组织
'default_value' => '', //字段默认值
'max_length' => 100, //最大长度
'is_enabled' => 1, //是否启用
'is_required' => 0, //是否必填
'is_uniqued' => 0, //是否唯一
'sequence' => 0, //排序
'visible_set' => '', //可见设置,以逗号分隔的集合(new,edit,detail,search,list,export)
'visible_set_default'=>'', //默认显示值是否可修改
'is_edit_readonly'=>0, //是否编辑页只读
'is_add_readonly'=>0, //是否添加页只读
'created_by' => 'sys', //创建人工号
'created_by_name' => '', //创建人姓名
'created_at' => time(), //创建时间
'updated_by' => 'sys', //更新人
'updated_by_name' => '', //更新人姓名
'updated_at' => time(), //更新时间
];
foreach($data as $key => $val){
$data[$key] = \yii\helpers\ArrayHelper::merge($default,$val);
}
return $data;
} /**
* @purpose: 定义用户字段
* @return array
*/
public function defineUserTable()
{
return [
[
'belong_module' => 'fusionpbx',
'table_name' => 'user',
'field_name' => 'id',
'field_label' => '主键ID',
'field_type' => 'fixed',
'input_type' => 'number',
'extra_fields' => '',
'max_length' => 100,
'is_enabled' => 1,
'is_required' => 1,
'is_uniqued' => 1,
'sequence' => 1,
'visible_set' => '',
'visible_set_default' => '{"new": {"disabled": true},"edit": {"disabled": true},"detail": {"disabled": true},"is_enabled": {"disabled": true},"is_uniqued":{"disabled": true}, "is_required": {"disabled": true}}',
'is_edit_readonly' => 0,
'is_add_readonly' => 0,
],
[
'belong_module' => 'fusionpbx',
'table_name' => 'user',
'field_name' => 'user_name',
'field_label' => '姓名',
'field_type' => 'fixed',
'input_type' => 'text',
'extra_fields' => '',
'max_length' => 100,
'is_enabled' => 1,
'is_required' => 1,
'is_uniqued' => 0,
'sequence' => 2,
'visible_set' => 'new,edit,detail,search,list,export',
'visible_set_default' => '{"new": {"disabled": true},"edit": {"disabled": true},"detail": {"disabled": true},"is_enabled": {"disabled": true},"is_uniqued":{"disabled": true}, "is_required": {"disabled": true}}',
'is_edit_readonly' => 0,
'is_add_readonly' => 0,
],
//可以继续添加更多字段
];
} /**
* @purpose: 定义部门字段
* @return array
*/
public function defineDeptTable()
{
return [
[
'belong_module' => 'fusionpbx',
'table_name' => 'dept',
'field_name' => 'id',
'field_label' => '主键ID',
'field_type' => 'fixed',
'input_type' => 'number',
'extra_fields' => '',
'max_length' => 100,
'is_enabled' => 1,
'is_required' => 1,
'is_uniqued' => 1,
'sequence' => 1,
'visible_set' => '',
'visible_set_default' => '{"new": {"disabled": true},"edit": {"disabled": true},"detail": {"disabled": true},"is_enabled": {"disabled": true},"is_uniqued":{"disabled": true}, "is_required": {"disabled": true}}',
'is_edit_readonly' => 0,
'is_add_readonly' => 0,
],
[
'belong_module' => 'fusionpbx',
'table_name' => 'dept',
'field_name' => 'dept_name',
'field_label' => '部门名称',
'field_type' => 'fixed',
'input_type' => 'text',
'extra_fields' => '',
'max_length' => 100,
'is_enabled' => 1,
'is_required' => 1,
'is_uniqued' => 0,
'sequence' => 2,
'visible_set' => 'new,edit,detail,search,list,export',
'visible_set_default' => '{"new": {"disabled": true},"edit": {"disabled": true},"detail": {"disabled": true},"is_enabled": {"disabled": true},"is_uniqued":{"disabled": true}, "is_required": {"disabled": true}}',
'is_edit_readonly' => 0,
'is_add_readonly' => 0,
],
//可以继续添加更多字段
];
}
}

本文为袋鼠学习中的总结,如有转载请注明出处:https://www.cnblogs.com/chrdai/p/8477269.html

 可先参看博友的博文:https://segmentfault.com/a/1190000005599416

migrate数据库迁移的更多相关文章

  1. Laravel5.x的php artisan migrate数据库迁移创建操作报错SQLSTATE[42000]解决

    Laravel5.x运行迁移命令创建数据表:php artisan migrate报错. Illuminate\Database\QueryException  : SQLSTATE[42000]: ...

  2. yii2 migrate 数据库迁移的简单分享

    开发中经常会用到的方法小结: 1../yii migrate xxx_xx 在表中插入某字段 : public function up() {$this->addColumn('{{applic ...

  3. Flask_Migrate数据库迁移

    migrate数据库迁移 有models,没有迁移仓库.本地新建数据库:首次创建迁移仓库.迁移脚本:执行迁移脚本生成数据库表: python manage.py db init python mana ...

  4. laravel数据库迁移(三)

    laravel号称世界上最好的框架,数据库迁移算上一个,在这里先简单入个门: laravel很强大,它把表中的操作写成了migrations迁移文件,然后可以直接通过迁移文件来操作表.所以 , 数据迁 ...

  5. Code First开发系列之数据库迁移

    返回<8天掌握EF的Code First开发>总目录 本篇目录 开启并运行迁移 使用迁移API 应用迁移 给已存在的数据库添加迁移 EF的其他功能 本章小结 自我测试 本系列的源码本人已托 ...

  6. Laravel学习笔记(三)数据库 数据库迁移

    该章节内容翻译自<Database Migration using Laravel>,一切版权为原作者. 原作者:Stable Host, LLC 翻译作者:Bowen Huang 正文: ...

  7. ASP.NET MVC5--为数据库新增字段(涉及数据库迁移技术)

    Setting up Code First Migrations for Model Changes--为模型更改做数据库迁移. 1.打开资源管理器,在App_Data文件夹下,找到movies.md ...

  8. Laravel 5 基础(六)- 数据库迁移(Migrations)

    database migrations 是laravel最强大的功能之一.数据库迁移可以理解为数据库的版本控制器. 在 database/migrations 目录中包含两个迁移文件,一个建立用户表, ...

  9. yii 数据库迁移

    在我们开发程序的过程中,数据库的结构也是不断调整的.我们的开发中要保证代码和数据库库的同步.因为我们的应用离不开数据库.例如: 在开发过程中,我们经常需要增加一个新的表,或者我们后期投入运营的产品,可 ...

随机推荐

  1. Caffe使用新版本CUDA和CuDNN

    因为一些原因还是需要使用别人基于Caffe的代码,但是代码比较老,默认不支持高版本的cuda或者cudnn 怎么办呢?基本上就是把最新官方Caffe-BVLC的几个关键文件拿过来替换即可. 脚本如下: ...

  2. TodoMVC:帮助你选择一个MV*框架

    开发者现在有很多的MV*框架选择来组织开发web应用程序.Backbone. Ember.AngularJS.Spine… 新的稳定解决方案列表持续增长,但你如何决定在海量的框架中选择哪个使用? 为了 ...

  3. 解开一个疑惑,为什么LVS开放的端口,使用netstat或ss命令,不能查找到其监听的端口呢?

    RT, 这个疑问,本周一直在心里,今天找到一个说法. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 另外LVS规则算是内核方法,用netstat -ntulp也显 ...

  4. jQuery实用工具集

    插件描述:jQuery实用工具集,该插件封装了常用功能,如序列化表单值获取地址栏参数window对象操作等 此工具集包含判断浏览器,判断浏览终端,获取地址栏参数,获取随机数,数据校验等常用操作功能 引 ...

  5. Java集合源码学习(四)HashMap

    一.数组.链表和哈希表结构 数据结构中有数组和链表来实现对数据的存储,这两者有不同的应用场景,数组的特点是:寻址容易,插入和删除困难:链表的特点是:寻址困难,插入和删除容易:哈希表的实现结合了这两点, ...

  6. Sql与C#中日期格式转换总结

    SQL中的转换方法: 一.将string转换为datetime,主要是使用Convert方法, 方法,Convert(datetime [ ( length ) ] , expression, [st ...

  7. Codeforces 965E Short Code 启发式合并 (看题解)

    Short Code 我的想法是建出字典树, 然后让后面节点最多的点优先向上移到不能移为止, 然后gg. 正确做法是对于当前的节点如果没有被占, 那么从它的子树中选出一个深度最大的点换到当前位置. 用 ...

  8. Codeforces 542D Superhero's Job 数论 哈希表 搜索

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF542D.html 题目传送门 - CF542D 题目传送门 - 51Nod1477 题意 定义公式 $J(x ...

  9. BZOJ1497 [NOI2006]最大获利 网络流 最小割 SAP

    原文链接http://www.cnblogs.com/zhouzhendong/p/8371052.html 题目传送门 - BZOJ1497 题意概括 有n个站要被建立. 建立第i个站的花费为pi. ...

  10. js下拉列表

    js清除下拉列表所选默认值 $("#lineId").val(“”); js清除下拉列表所有选项 $("#type").html(""); ...