phinx是一个使用php写的一个数据库迁移管理程序,可以很方便的管理数据库,可以避免手写sql。每次对修改数据库,比如增加一个字段,添加一个数据表等都有一个对应的迁移文件,并且产生的迁移文件都有时间标识,所以可以很方便知道数据库的变动情况。
安装
composer require robmorgan/phinx
vendor/bin/phinx --version
Phinx by CakePHP - https://phinx.org.
初始化项目
vendor/bin/phinx init
# 修改数据库信息
创建项目后、,会在当前的工作目录生成一个配置文件,配置文件中包含一些路径,和数据库相关的设置。
检查是否创建成功
# !!! 这个测试通过,并没有测试 数据库设链接是否正确。
phinx test
### 默认的环境是开发环境,这个测试数据库的链接是否正确
phinx test -e development
Phinx by CakePHP - https://phinx.org.
using config file phinx.php
using config parser php
validating environment development
success!
测试通过的时候,数据库中就会创建表 phinxlog
mysql> select * from phinxlog;
Empty set (0.00 sec)
mysql> desc phinxlog;
+----------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+--------------+------+-----+---------+-------+
| version | bigint(20) | NO | PRI | NULL | |
| migration_name | varchar(100) | YES | | NULL | |
| start_time | timestamp | YES | | NULL | |
| end_time | timestamp | YES | | NULL | |
| breakpoint | tinyint(1) | NO | | 0 | |
+----------------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
mysql> select * from phinxlog;
Empty set (0.01 sec)
如果没有测试这个过程,后期进行数据库迁移操作的时候,依然会创建这个表,测试步骤不是必须的,好的习惯,还是要测试的。
创建目录
mkdir db/migrations -p
mkdir db/seeds -p
根据项目目录下的配置文件创建目录,如果目录不存在会出错
You probably used curly braces to define migration path in your Phinx configuration file, but no directories have been matched using this pattern. You need to create a migration directory manually.
创建一个迁移文件
phinx create BlogMigration
Phinx by CakePHP - https://phinx.org.
using config file phinx.php
using config parser php
using migration paths
- /test/www.liuhaolin.com/webroot/db/migrations
using seed paths
- /test/www.liuhaolin.com/webroot/db/seeds
using migration base class Phinx\Migration\AbstractMigration
using default template
created db/migrations/20210318033125_blog_migration.php
修改迁移文件
file - db/migrations/20210318033125_blog_migration.php
迁移文件,就是实现我们对数据库操作的文件。这里比如想创建一个记录博客的数据库。一步创建一个数据表用来存放文章
# 修改 change 方法即可
public function change(): void
{
$table = $this->table('tb_blog');
// 添加几个字段
$table->addColumn('title', 'string', ["length" => "30"])
->addColumn('conment', 'text')
->create();
}
运行迁移文件
phinx migrate
...
All Done. Took 0.0294s
查看创建的数据表 和 迁移记录 phinxlog
mysql> desc tb_blog;
+---------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| title | varchar(30) | NO | | NULL | |
| conment | text | NO | | NULL | |
+---------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
mysql> select * from phinxlog;
+----------------+----------------+---------------------+---------------------+------------+
| version | migration_name | start_time | end_time | breakpoint |
+----------------+----------------+---------------------+---------------------+------------+
| 20210318033125 | BlogMigration | 2021-03-18 03:52:30 | 2021-03-18 03:52:30 | 0 |
+----------------+----------------+---------------------+---------------------+------------+
1 row in set (0.00 sec)
添加一个字段
phinx create Blog02Migration
编辑文件 db/migrations/20210318053350_blog_02_migration.php
public function change(): void
{
$table = $this->table('tb_blog');
$table->addColumn('update_time', 'datatime')->save();
}
运行
phinx migrate
查看数据表
mysql> desc tb_blog;
+-------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| title | varchar(30) | NO | | NULL | |
| conment | text | NO | | NULL | |
| update_time | datetime | NO | | NULL | |
+-------------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)