migrate数据库迁移
一、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数据库迁移的更多相关文章
- Laravel5.x的php artisan migrate数据库迁移创建操作报错SQLSTATE[42000]解决
Laravel5.x运行迁移命令创建数据表:php artisan migrate报错. Illuminate\Database\QueryException : SQLSTATE[42000]: ...
- yii2 migrate 数据库迁移的简单分享
开发中经常会用到的方法小结: 1../yii migrate xxx_xx 在表中插入某字段 : public function up() {$this->addColumn('{{applic ...
- Flask_Migrate数据库迁移
migrate数据库迁移 有models,没有迁移仓库.本地新建数据库:首次创建迁移仓库.迁移脚本:执行迁移脚本生成数据库表: python manage.py db init python mana ...
- laravel数据库迁移(三)
laravel号称世界上最好的框架,数据库迁移算上一个,在这里先简单入个门: laravel很强大,它把表中的操作写成了migrations迁移文件,然后可以直接通过迁移文件来操作表.所以 , 数据迁 ...
- Code First开发系列之数据库迁移
返回<8天掌握EF的Code First开发>总目录 本篇目录 开启并运行迁移 使用迁移API 应用迁移 给已存在的数据库添加迁移 EF的其他功能 本章小结 自我测试 本系列的源码本人已托 ...
- Laravel学习笔记(三)数据库 数据库迁移
该章节内容翻译自<Database Migration using Laravel>,一切版权为原作者. 原作者:Stable Host, LLC 翻译作者:Bowen Huang 正文: ...
- ASP.NET MVC5--为数据库新增字段(涉及数据库迁移技术)
Setting up Code First Migrations for Model Changes--为模型更改做数据库迁移. 1.打开资源管理器,在App_Data文件夹下,找到movies.md ...
- Laravel 5 基础(六)- 数据库迁移(Migrations)
database migrations 是laravel最强大的功能之一.数据库迁移可以理解为数据库的版本控制器. 在 database/migrations 目录中包含两个迁移文件,一个建立用户表, ...
- yii 数据库迁移
在我们开发程序的过程中,数据库的结构也是不断调整的.我们的开发中要保证代码和数据库库的同步.因为我们的应用离不开数据库.例如: 在开发过程中,我们经常需要增加一个新的表,或者我们后期投入运营的产品,可 ...
随机推荐
- Leetcode刷题第001天
一.合并两个有序链表 [题目]206. 反转链表 /** * Definition for singly-linked list. * struct ListNode { * int val; * L ...
- Git和Github入门教程
一.常用命令 所有命令前都要加 git,如表中的init是指 git init.点击命令可直接跳转至本文第一次使用的地方.以下命令都在命令行里执行. 1.本地命令 行为 命令 备注 初始化 init ...
- jloi2015
题解: [JLOI2015]管道连接 这个很水 比较裸的斯坦纳树dp 斯坦纳树dp就是 g[i][j]表示当前在i点,状态为j 然后转移分为两种 g[i][j]=g[i][k]+g[i][k^j] 另 ...
- outlook邮件中样式问题
目前要做一个定时发送邮件的功能,邮件的大致内容布局如下: HTML中 在QQ邮件中,可以进行正常显示. 在outlook网页版,也可以正常显示, outlook客户端 但是到了客户端就会出现很多很神奇 ...
- 黑色半透明镂空遮罩指引效果实现jQuery小插件
/*! * by zhangxinxu(.com) 2017-05-18 * 新版上线时候的黑色半透明镂空遮罩指引效果实现jQuery小插件 * 兼容到IE8+ * MIT使用协议,使用时候保留版权 ...
- 阿里巴巴Java开发规范手册
Java开发手册 版本号 制定团队 更新日期 备 注 1.0.0 阿里巴巴集团技术部 2016.12.7 首次向Java业界公开 一.编程规约 (一) 命名规约 1. [强制]所有编程相关命 ...
- SORT--不要仅限于题目中
输入n,m 表示输入n个数输出前m个最大的数 Input The input file contains many test cases. Each case has 2 lines. The fir ...
- TF:Tensorflor之session会话的使用,定义两个矩阵,两种方法输出2个矩阵相乘的结果—Jason niu
import tensorflow as tf matrix1 = tf.constant([[3, 20]]) matrix2 = tf.constant([[6], [100]]) product ...
- CNN:人工智能之神经网络算法进阶优化,六种不同优化算法实现手写数字识别逐步提高,应用案例自动驾驶之捕捉并识别周围车牌号—Jason niu
import mnist_loader from network3 import Network from network3 import ConvPoolLayer, FullyConnectedL ...
- Windows10家庭版如何升级至Windows10专业版
Windows10家庭版和专业版系统文件其实是一样的iso镜像文件,但是由于Microsoft某些限制导致一些用户无法享受到专业版的福利,说实话这是一种很让人蛋疼的操作... 接下来我来告诉各位如何把 ...