把需要修改的数据库脚本写在up函数中:
把对应修改修改所做的回滚操作卸载down函数中
注意:在生产环境下建议只做数据库的向上变迁,不做down操作,避免用户有用数据丢失.
执行php migrate.php list 返回结果:
WARNING: Migration numbers may not be in order due to interleaving.
# Timestamp
========================================================================
version createtime active current note
1371546856 2013-06-18 17:14:16 1 0 v1
1380427212 2013-09-29 12:00:12 1 1 v1
1387349445 2013-12-18 14:50:45 0 0 test
Page 1 of 1, 3 migrations in all.
cd config 目录
cat db_config.php
了解该程序基本结构后,我们来开始使用一下它:
cd mysqlMigrations
php migrate.php add test2
在dbscript下生成 2013_12_18_15_06_14_test2.php
执行命令:php migrate.php list
可以看到版本结果:
# Timestamp
========================================================================
version createtime active current note
1371546856 2013-06-18 17:14:16 1 0 v1
1380427212 2013-09-29 12:00:12 1 1 v1
1387349445 2013-12-18 14:50:45 0 0 test
1387350374 2013-12-18 15:06:14 0 0 test2
Page 1 of 1, 4 migrations in all.
说明:
version 每次迁移的版本号
createtime 创建时间
active 是否已经激活生效
current 数据库当前所在版本标志
note 迁移的注释
执行命令:php migrate.php up 1387349445 可以把数据版本从 1380427212 迁移到 1387349445
# Timestamp
========================================================================
version createtime active current note
1371546856 2013-06-18 17:14:16 1 0 v1
1380427212 2013-09-29 12:00:12 1 0 v1 1387349445 2013-12-18 14:50:45 1 1 test
1387350374 2013-12-18 15:06:14 0 0 test2
Page 1 of 1, 4 migrations in all.
执行命令:php migrate.php down 1380427212 可以把数据版本从 1387349445 回滚到 1380427212
执行php migrate.php list查看数据库版本回滚结果
# Timestamp
========================================================================
version createtime active current note
1371546856 2013-06-18 17:14:16 1 0 v1 1380427212 2013-09-29 12:00:12 1 1 v1
1387349445 2013-12-18 14:50:45 0 0 test
1387350374 2013-12-18 15:06:14 0 0 test2
Page 1 of 1, 4 migrations in all.
如果要迁移到数据库最大版本可以执行一下命令:
php migrate.php up max_version
返回结果:
Migrating to 2013-12-18 15:06:14 (ID 1387350374)...
Performing UP migration 2013-12-18 14:50:45 (ID 1387349445)... done.
Performing UP migration 2013-12-18 15:06:14 (ID 1387350374)... done.
*******************************************************************************
查看php migrate.php list
# Timestamp
========================================================================
version createtime active current note
1371546856 2013-06-18 17:14:16 1 0 v1
1380427212 2013-09-29 12:00:12 1 0 v1
1387349445 2013-12-18 14:50:45 1 0 test 1387350374 2013-12-18 15:06:14 1 1 test2
Page 1 of 1, 4 migrations in all.
执行回滚:
php migrate.php down 1380427212
返回一下结果:
Migrating to 2013-09-29 12:00:12 (ID 1380427212)...
Performing DOWN migration 2013-12-18 15:06:14 (ID 1387350374)... done.
Performing DOWN migration 2013-12-18 14:50:45 (ID 1387349445)... done.
# Timestamp
========================================================================
version createtime active current note
1371546856 2013-06-18 17:14:16 1 0 v1
1380427212 2013-09-29 12:00:12 1 1 v1
1387349445 2013-12-18 14:50:45 0 0 test
1387350374 2013-12-18 15:06:14 0 0 test2
Page 1 of 1, 4 migrations in all.
通过执行一下操作,查看数据库中数据变化