如何在PHP项目中使用phinx进行数据迁移和建表
建表
1 | phinx\bin\phinx.bat migrate -e production |
建设 phinx.yml文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | 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 |
1 | %%PHINX_CONFIG_DIR%%\database\migrations下面的文件示例20161222061456_create_users_table.php如下: |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | <?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(); } } |
数据迁移命令如下:
1 | phinx\bin\phinx.bat seed:run -e production |
1 | %%PHINX_CONFIG_DIR%%\database\seeds下面的文件示例CreateGroupsTable.php如下: |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | <?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
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
2016-03-29 Azure 怎么开通FTP
2015-03-29 让浏览器进行跨域访问, 开发阶段需要跨域访问的测试方案 chrome的快捷方式里面 加 "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --args --disable-web-security