一、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. OpenCV-Python入门教程3-图像基本操作(访问像素点/ROI/通道分离)

    一.获取和修改像素点的值 import cv2img = cv2.imread('lena.jpg') # 100, 90表示行列坐标 px = img[100, 90] print(px) # 获取 ...

  2. python 把类当作 装饰器

    # class Test(object): # def __call__(self): # print('-----test----') # t= Test()# t() 调用主要有个__call__ ...

  3. java线程间的通信方式

    1.同步 synchronized 2.轮询 while   volatile 3.wait/notify机制 syncrhoized加锁的线程的Object类的wait()/notify()/not ...

  4. [转] ES6展开运算符

    语法 用于函数调用 myFunction(...iterableObj); 用于数组字面量 [...iterableObj, 4, 5, 6] 函数传参 目前为止,我们都是使用Function.pro ...

  5. php让一个数组按照另外一个数组的键名进行排序

    $a = [ 'id', 'name', 'identityId', 'phone', 'email', 'schoolId' ]; $b = [ 'id' => '唯一标识', 'identi ...

  6. Oier们的镜子(mirror)

    题解: 这题真是把我坑的很惨.. 题目看了很久才看懂.. 然后刚开始又没看见每个只能匹配一个这种条件 #include <bits/stdc++.h> using namespace st ...

  7. WebApi接口返回值不困惑:返回值类型详解

    前言:已经有一个月没写点什么了,感觉心里空落落的.今天再来篇干货,想要学习Webapi的园友们速速动起来,跟着博主一起来学习吧.作为程序猿,我们都知道参数和返回值是编程领域不可分割的两大块,此前分享了 ...

  8. zabbix通过自动发现tomcat应用端口监控连接数

    192.168.10.98上 netstat -anp | wc -l netstat -anp|grep 8094 | grep ESTABLISHED | wc -l netstat -anp|g ...

  9. 舞蹈链 DLX

    欢迎访问——该文出处-博客园-zhouzhendong 去博客园看该文章--传送门 舞蹈链是一个非常玄学的东西…… 问题模型 精确覆盖问题:在一个01矩阵中,是否可以选出一些行的集合,使得在这些行的集 ...

  10. Linux安装Tomcat-Nginx-FastDFS-Redis-Solr-集群——【第二集之新建虚拟机】

    1, 2, 3, 4,(如果选择版本时,发现选项中没有centos,可以选择other linux2.6.x kernel) 5,(虚拟机命名:Centos_用途_IP) 6,(选择Split vir ...