如何在PHP项目中使用phinx进行数据迁移和建表
建表
phinx\bin\phinx.bat migrate -e production
建设 phinx.yml文件
paths:
migrations: %%PHINX_CONFIG_DIR%%\database\migrations
seeds: %%PHINX_CONFIG_DIR%%\database\seeds environments:
default_migration_table: phinxlog
default_database: development
production:
adapter: mysql
host: localhost
name: jitamin2
user: root
pass: ''
port: 3306
charset: utf8 development:
adapter: mysql
host: localhost
name: development_db
user: root
pass: ''
port: 3306
charset: utf8 testing:
adapter: mysql
host: localhost
name: testing_db
user: root
pass: ''
port: 3306
charset: utf8
%%PHINX_CONFIG_DIR%%\database\migrations下面的文件示例20161222061456_create_users_table.php如下:
<?php /*
* This file is part of Jitamin.
*
* Copyright (C) Jitamin Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/ use Phinx\Migration\AbstractMigration; class CreateUsersTable extends AbstractMigration
{
/**
* Change Method.
*/
public function change()
{
$table = $this->table('users');
$table->addColumn('username', 'string', ['limit'=>50])
->addColumn('password', 'string', ['null' => true])
->addColumn('is_ldap_user', 'boolean', ['null' => true, 'default' => false])
->addColumn('name', 'string', ['null' => true])
->addColumn('email', 'string')
->addColumn('google_id', 'string', ['null'=> true, 'limit' => 30])
->addColumn('github_id', 'string', ['null' => true, 'limit' => 30])
->addColumn('notifications_enabled', 'boolean', ['null' => true, 'default' => false])
->addColumn('timezone', 'string', ['null' => true, 'limit' => 50])
->addColumn('language', 'string', ['null' => true, 'limit' => 5])
->addColumn('disable_login_form', 'boolean', ['null' => true, 'default' => false])
->addColumn('twofactor_activated', 'boolean', ['null' => true, 'default' => false])
->addColumn('twofactor_secret', 'string', ['null' => true, 'limit' => 16])
->addColumn('token', 'string', ['null'=> true, 'default' => ''])
->addColumn('notifications_filter', 'integer', ['null' => true, 'default' => 4])
->addColumn('nb_failed_login', 'integer', ['null' => true, 'default' => 0])
->addColumn('lock_expiration_date', 'biginteger', ['null' => true])
->addColumn('gitlab_id', 'integer', ['null' => true])
->addColumn('role', 'string', ['limit' => 25, 'default' => 'app-user'])
->addColumn('is_active', 'boolean', ['null' => true, 'default' => true])
->addColumn('avatar_path', 'string', ['null' => true])
->addColumn('skin', 'string', ['null' => true, 'limit'=>15])
->addIndex(['username'], ['unique' => true])
->addIndex(['email'], ['unique' => true])
->create();
}
}
数据迁移命令如下:
phinx\bin\phinx.bat seed:run -e production
%%PHINX_CONFIG_DIR%%\database\seeds下面的文件示例CreateGroupsTable.php如下:
<?php /*
* This file is part of Jitamin.
*
* Copyright (C) Jitamin Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/ use Jitamin\Foundation\Security\Role;
use Phinx\Seed\AbstractSeed; class UserSeeder extends AbstractSeed
{
/**
* Run Method.
*/
public function run()
{
$data = [
[
'username' => 'admin',
'password' => bcrypt('admin'),
'email' => 'admin@admin.com',
'role' => Role::APP_ADMIN,
],
]; $users = $this->table('users');
$users->insert($data)
->save();
}
}
如何在PHP项目中使用phinx进行数据迁移和建表的更多相关文章
- EF 中 Code First 的数据迁移以及创建视图
写在前面: EF 中 Code First 的数据迁移网上有很多资料,我这份并没什么特别.Code First 创建视图网上也有很多资料,但好像很麻烦,而且亲测好像是无效的方法(可能是我太笨,没搞成功 ...
- [Laravel-Swagger]如何在 Laravel 项目中使用 Swagger
如何在 Laravel 项目中使用 Swagger http://swagger.io/getting-started/ 安装依赖 swagger-php composer require zirco ...
- 如何在cocos2d项目中enable ARC
如何在cocos2d项目中enable ARC 基本思想就是不支持ARC的代码用和支持ARC的分开,通过xcode中设置编译选项,让支持和不支持ARC的代码共存. cocos2d是ios app开发中 ...
- 如何在NodeJS项目中优雅的使用ES6
如何在NodeJS项目中优雅的使用ES6 NodeJs最近的版本都开始支持ES6(ES2015)的新特性了,设置已经支持了async/await这样的更高级的特性.只是在使用的时候需要在node后面加 ...
- 如何在VUE项目中添加ESLint
如何在VUE项目中添加ESLint 1. 首先在项目的根目录下 新建 .eslintrc.js文件,其配置规则可以如下:(自己小整理了一份),所有的代码如下: // https://eslint.or ...
- 如何在mvc项目中使用apiController
文章地址:How do you route from an MVC project to an MVC ApiController in another project? 文章地址:How to Us ...
- 如何在Ionic2项目中使用第三方JavaScript库
onic的官网放出一记大招Ionic and Typings,来介绍如何在Ionic2项目中使用第三方JavaScript库. 因为在前阵子正好想用一个非常有名的第三方JS库ChartJs来实现一些东 ...
- 如何在maven项目中使用spring
今天开始在maven项目下加入spring. 边学习边截图. 在这个过程中我新建了一个hellospring的项目.于是乎从这个项目出发开始研究如何在maven项目中使用spring.鉴于网上的学习资 ...
- 如何在Vue-cli项目中使用JTopo
1.前言 jTopo(Javascript Topology library)是一款完全基于HTML5 Canvas的关系.拓扑图形化界面开发工具包.其体积小,性能优异,由一群开发爱好者来维护.唯一感 ...
随机推荐
- 使用Laya引擎开发微信小游戏
在支持微信小游戏的游戏引擎中,Cocos,Egret,Laya都对小游戏的开发提供了很多强大的支持.前段时间正好抽空研究了一下这块的内容,现做一个总结,针对如何使用Laya引擎开发微信小游戏给大家做一 ...
- Android学习笔记二:activity的理解
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/7513290.html 一:activity定义了app的页面 一个app有很多个页面组成,一个页面其实就是一个 ...
- ACE .i .inl文件(转)
在ACE的源代码目录里,有源文件.cpp.头文件.h,我们还发现有以.i和.inl为扩展名的文件.其实,以.i和.inl为扩展名的文件是ACE源码中inline函数的存放形式. 在说明ACE中为什么采 ...
- unique_ptr与std::move的使用
形参为unique_ptr u2,而后实参为std::move(unique_ptr u1),这样会将原本u1的内存传递给u2,避免了传递拷贝.例如: void fun(std::unique_ptr ...
- django之创建第10-1个项目-图片上传并记录上传时间
1.百度云盘:django之创建第10-1个项目-图片上传并记录上传时间 2.主要修改的配置文件有3个,forms.views和models3个文件以及html 3.forms.py文件修改 #cod ...
- python 怎么模拟加header(如User-Agent、Content-Type等等)
# -*- coding: cp936 -*- #python 27 #xiaodeng #python 怎么模拟加header(如User-Agent.Content-Type等等) #办法一: i ...
- yml转properties
推荐一个在线工具,可以将yaml转换为properties,同时也支持反向转换 http://www.toyaml.com 非常好记的地址,to yaml,直接在地址栏里输入toyaml.com,省去 ...
- appium日志示例解读
http://www.colabug.com/thread-1048952-1-1.html
- excel怎么在插入的方框上打勾
本例主要介绍如何在excel中插入带对勾的方框. 工具/原料 Excel 操作步骤: 在编辑Excel表格模板时,比如说简历.人力信息登记表等,经常需要有一些可选项,如下例的婚姻状况就包括“有配 ...
- 使用std::find_if提取序列容器的子串
一个需求是这样的,一个vector容器中,我需要提取满足一定条件的元素的序列.就比如,一个树形结构,我把该接口拍扁成vector容器,每个节点都有一个惟一ID. 以下就是根据特定的ID查找节点下的子节 ...