如何在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的关系.拓扑图形化界面开发工具包.其体积小,性能优异,由一群开发爱好者来维护.唯一感 ...
随机推荐
- 腾讯云ubuntu安装Mysql并配置远程访问
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6378914.html 一:修改SSH配置 输入 su 进入root模式.修改ssh配置: sudo vi /e ...
- MySQL中 PK NN UQ BIN UN ZF AI 的意思
PK Belongs to primary key作为主键 NN Not Null非空 UQ Unique index不能重复 BIN Is binary column存放二进制数据的列 ...
- 怎样使用Fiddler获取WebApi的token值?
User-Agent: Fiddler Host: localhost: Content-Length: Content-Type: application/json grant_type=passw ...
- 多表连接的三种方式详解 hash join、merge join、 nested loop
在多表联合查询的时候,如果我们查看它的执行计划,就会发现里面有多表之间的连接方式.多表之间的连接有三种方式:Nested Loops,Hash Join 和 Sort Merge Join.具体适用哪 ...
- java 如何对由json对象构成的数组形式的字符串进行遍历?
1.情景展示 现在已知字符串为: [{"name":"微微笑","img":"http://zos.alipayobjects ...
- 〖Linux〗在tmux同时使用bash和zsh
个人有两份tmux配置文件: ~/.tmux.conf # 使用zsh,主要是日常使用,zsh太好使用了 ~/.tmux.conf.bash # 使用bash,主要是Android编译使用 按照tmu ...
- sparkmllib矩阵向量
Spark MLlib底层的向量.矩阵运算使用了Breeze库,Breeze库提供了Vector/Matrix的实现以及相应计算的接口(Linalg).但是在MLlib里面同时也提供了Vector和L ...
- MySQL表名大小写敏感导致的问题
最近在项目中遇到一个比较奇怪的小问题.在开发过程中自己测试没有问题,但是提测后,测试的同时在测试一个功能时报错了,日志是: Caused by: com.mysql.jdbc.exceptions ...
- 一些有用的git命令清单
以下是一些我常用的git命令清单 如果以下的命令不清晰细节,请看git的文档. 设置个人信息 git config --global user.name "John Doe" gi ...
- QQ登录整合/oauth2.0认证-02-跳转到QQ互联页
---------------------------目录---------------------------------- QQ登录整合/oauth2.0认证-01-申请appkey和appid ...