官网:http://mybatis.org/migrations/
网络:http://blog.csdn.net/daquan198163/article/details/9284611
http://wenku.baidu.com/view/0b2e4801a8114431b90dd83b.html
https://code.google.com/p/mybatis/wiki/Migration
http://mybatis.org/migrations/installation.html
MyBatis Migrations is a Java tool, so you must have Java installedin order to proceed
安装比较简单有下面4个步骤,不一一说明。
1.下载-2.解压-3.JAVA-4.环境变量
任意位置建立一个db目录,如:D:\PERA\migration\db
D:\PERA\migration\db>migrate init
------------------------------------------------------------------------
MyBatis Migrations - init
------------------------------------------------------------------------
Initializing: .
Creating: environments
Creating: scripts
Creating: drivers
Creating: README
Creating: development.properties
Creating: bootstrap.sql
Creating: 20131009084544_create_changelog.sql
Creating: 20131009084545_first_migration.sql
Done!
------------------------------------------------------------------------
MyBatis Migrations SUCCESS
Total time: 2s
Finished at: Wed Oct 09 16:45:45 CST 2013
Final Memory:2M/483M
D:\PERA\migration\db>migratestatus
------------------------------------------------------------------------
--MyBatis Migrations - status
------------------------------------------------------------------------
ID Applied At Description
================================================================================
20131009084544 ...pending... create changelog
20131009084545 ...pending... first migration
20131010010742 ...pending... tan add table aa
20131010100011 ...pending... tanqr add table pera
------------------------------------------------------------------------
--MyBatis Migrations SUCCESS
--Total time: 0s
--Finished at: Thu Oct 10 10:00:42 CST 2013
-- Final Memory: 21M/483M
D:\PERA\migration\db>migrate new "pera add tabletan6"
------------------------------------------------------------------------
-- MyBatis Migrations - new
------------------------------------------------------------------------
Your migrations configuration did not find your customtemplate. Using the d
Done!
------------------------------------------------------------------------
-- MyBatis Migrations SUCCESS
-- Total time: 1s
-- Finished at: Thu Oct 10 13:30:52 CST 2013
-- Final Memory: 2M/483M
------------------------------------------------------------------------
会生成脚本20131010133052_pera_add_table_tan6.sql
修改20131010133052_pera_add_table_tan6.sql
--//tanqr add table pera
--Migration SQL that makes the change goes here.
createtable tan6 ( id number(30));
insertinto tan6 values(1011);
commit;
--//@UNDO
--SQL to undo the change goes here.
droptable tan6;
D:\PERA\migration\db>migrateup
------------------------------------------------------------------------
--MyBatis Migrations - up
------------------------------------------------------------------------
==========Applying: 20131010133052_pera_add_table_tan6.sql ================
-- tanqr add table pera
--Migration SQL that makes the change goes here.
createtable tan6 ( id number(30))
insertinto tan6 values(1011)
commit
------------------------------------------------------------------------
--MyBatis Migrations SUCCESS
--Total time: 0s
--Finished at: Thu Oct 10 13:31:16 CST 2013
--Final Memory: 24M/483M
------------------------------------------------------------------------
可以将脚本回滚至上一步
D:\PERA\migration\db>migrate down
------------------------------------------------------------------------
-- MyBatis Migrations - down
------------------------------------------------------------------------
========== Undoing:20131010133052_pera_add_table_tan6.sql ===============
-- @UNDO
-- SQL to undo the change goes here.
drop table tan6
------------------------------------------------------------------------
-- MyBatis Migrations SUCCESS
-- Total time: 0s
-- Finished at: Thu Oct 10 13:32:16 CST2013
-- Final Memory: 24M/483M
------------------------------------------------------------------------
可以回滚至执行过的任一时间
可以将脚本打包
D:\PERA\migration\db>migratescript 20131010010742 20131010103007>update20131010.sql
Script后跟的是两个时间点,可以通过migrate status查看。
updata20131010.sql时自己启的名字,包括了这个时间段的所有内容。
完成后会在db根目录下生成该脚本。
D:\PERA\migration\dbqing>migratebootstrap
可以此命令初始化数据库
在多人处理时,可能会因为自己脚本创建的早,但是提交的迟,出现这种情况
D:\PERA\migration\user1>migratenew "611 erm create test3 tqr"
D:\PERA\migration\user1>migratestatus
------------------------------------------------------------------------
-- MyBatisMigrations - status
------------------------------------------------------------------------
ID Applied At Description
================================================================================
201310100827392013-10-10 17:23:49 create changelog
201310100827402013-10-10 17:23:49 first migration
201310110910242013-10-11 09:24:31 611 pera peraq add table qform
201310111358012013-10-11 14:00:47 611 erm dml test1
201310111359062013-10-11 14:01:17 611 erm create table test1 tqr
201310111359422013-10-11 14:01:17 611 erm dml test1 tqr
201310111415362013-10-11 15:30:42 611 erm create table test2 tqr
20131011153334 ...pending... 611 erm create test3 tqr
201310111534432013-10-11 15:35:04 611 erm create test4 tqr
此时migrateup则不会去执行
D:\PERA\migration\user1>migrateup
------------------------------------------------------------------------
-- MyBatisMigrations - up
------------------------------------------------------------------------
------------------------------------------------------------------------
-- MyBatisMigrations SUCCESS
-- Total time: 0s
-- Finished at: FriOct 11 15:36:26 CST 2013
-- Final Memory: 24M/483M
需要使用migratepending来处理
D:\PERA\migration\user1>migrate pending
------------------------------------------------------------------------
-- MyBatisMigrations - pending
------------------------------------------------------------------------
WARNING: Runningpending migrations out of order can create unexpected results.
==========Applying: 20131011153334_611_erm_create_test3_tqr.sql ===============
-- 611 erm create test3 tqr
-- Migration SQLthat makes the change goes here.
------------------------------------------------------------------------
-- MyBatisMigrations SUCCESS
-- Total time: 1s
-- Finished at: FriOct 11 15:37:03 CST 2013
-- Final Memory:24M/483M
------------------------------------------------------------------------
再看状态
D:\PERA\migration\user1>migratestatus
-----------------------------------------------------------------------
-- MyBatisMigrations - status
-----------------------------------------------------------------------
ID Applied At Description
=======================================================================
201310100827392013-10-10 17:23:49 create changelog
201310100827402013-10-10 17:23:49 first migration
201310110910242013-10-11 09:24:31 611 pera peraq add table qform
201310111358012013-10-11 14:00:47 611 erm dml test1
201310111359062013-10-11 14:01:17 611 erm create table test1 tqr
201310111359422013-10-11 14:01:17 611 erm dml test1 tqr
201310111415362013-10-11 15:30:42 611 erm create table test2 tqr
201310111533342013-10-11 15:37:03 611 erm create test3 tqr
201310111534432013-10-11 15:35:04 611 erm create test4 tqr
-----------------------------------------------------------------------
-- MyBatisMigrations SUCCESS
-- Total time: 0s
-- Finished at: FriOct 11 15:37:08 CST 2013
-- Final Memory:24M/483M
-----------------------------------------------------------------------
1. Migrate up成功的脚本,修改后,再次migrateup时不会去执行。但migrate down时会执行修改后的脚本。所以建议migrate up成功后不要对脚本再进行修改,如有变更用新脚本。详见《脚本修改migrate up down演示》
1.Migrate up没有成功的脚本,再次migrate up 时还会去执行。
2.当migrate up时,脚本中间有误时,会执行前段正确的,然后报错,后面的部分不会再执行。但migrate down时不会滚,却会回滚上一次操作,详见演示《错误1:事物性错误》
基于以上问题,建议1个脚本不要执行过多的DDL操作,如果是DML操作,中间不要有commit;
如果执行了migrate up,将不能再次执行。
Migrate Bootstrap –force
以bootstrap执行时,即使中间有错,抛出错误后仍然能继续执行后面的脚本。
1. 关于db路径
虽然多个项目,多个用户可以放到1个目录下,可以通过添加—path –env的方式可以区分,但是脚本会混在一起,可能有以下潜在问题。
1) 管理比较混乱。
2) 而且对于具体的某个开发人员,他可能更新的模块数据有限,甚至只有1个,自己的目录下不需要别的模块、别的项目。
3) 开发人员权限过大,有可能会误操作别的模块,别的项目。
建议1个项目(1个库)的一个schema建1个db路径。例:如有2个库,每个库3个用户的话,需要建6个db路径。
2. 脚本内容
对DDL操作,最好1个脚本1条操作;
对于DML操作,中间不要有commit,到整个操作完成再加commit;
脚本中不能有像以下的内容。
IF unix > win THEN
datadir := SUBSTR (uname, 1, unix);
ELSE
datadir := SUBSTR (wname, 1, win);
END IF;
dbms_output.put_line ('datadir = ' || datadir);
END;
/
手写3个命名时间精确到秒的脚本
注意:这3个脚本未实际执行,也非migrate new新建,是纯手工新建。
--// pera1
-- Migration SQL that makes the change goeshere.
crate table pera1 (id number(30));
--//@UNDO
-- SQL to undo the change goes here.
drop table pera1
------------------------------------------------------------------------
-- MyBatis Migrations - script
------------------------------------------------------------------------
-- 20131010135226_pera2.sql
-- pera2
-- Migration SQL that makes the change goeshere.
crate table pera2 (id number(30));
INSERT INTO CHANGELOG (ID, APPLIED_AT,DESCRIPTION) VALUES (20131010135226, '2013-10-10 14:43:01', 'pera2');
-- 20131010143333_pera3.sql
-- pera3
-- Migration SQL that makes the change goeshere.
crate table pera3 (id number(30));
INSERT INTO CHANGELOG (ID, APPLIED_AT,DESCRIPTION) VALUES (20131010143333, '2013-10-10 14:43:01', 'pera3');
------------------------------------------------------------------------
-- MyBatis Migrations SUCCESS
-- Total time: 0s
-- Finished at: Thu Oct 10 14:43:01 CST2013
-- FinalMemory: 2M/483M
可见脚本能够正常合并。
合并后的脚本可以在另一台机器的bootstrap.sql执行
内容类似于下面这样:
create table pera11 (id number(30));
D:\PERA\migration\db>migrate statu
------------------------------------------------------------------------
-- MyBatis Migrations - statu
------------------------------------------------------------------------
ID Applied At Description
================================================================================
20131009084544 ...pending... create changelog
20131009084545 ...pending... first migration
20131010151000 ...pending... pera11
20131010152000 ...pending... pera12
20131010153000 ...pending... pera13
------------------------------------------------------------------------
-- MyBatis Migrations SUCCESS
-- Total time: 0s
-- Finished at: Thu Oct 10 15:40:49 CST2013
-- Final Memory: 24M/483M
------------------------------------------------------------------------
D:\PERA\migration\db>migrate script20131009084545 20131010153000 > release2.sql
内容:
------------------------------------------------------------------------
-- MyBatis Migrations - script
------------------------------------------------------------------------
-- 20131010151000_pera11.sql
create table pera11 (id number(30));
INSERT INTO CHANGELOG (ID, APPLIED_AT,DESCRIPTION) VALUES (20131010151000, '2013-10-10 15:41:44', 'pera11');
-- 20131010152000_pera12.sql
create table pera12 (id number(30));
INSERT INTO CHANGELOG (ID, APPLIED_AT, DESCRIPTION)VALUES (20131010152000, '2013-10-10 15:41:44', 'pera12');
-- 20131010153000_pera13.sql
create table pera11 (id number(30));
INSERT INTO CHANGELOG (ID, APPLIED_AT,DESCRIPTION) VALUES (20131010153000, '2013-10-10 15:41:44', 'pera13');
------------------------------------------------------------------------
-- MyBatis Migrations SUCCESS
-- Total time: 0s
-- Finished at: Thu Oct 10 15:41:44 CST2013
-- Final Memory: 2M/483M
------------------------------------------------------------------------
不建议DBA使用version迁移开发库到指定版本,该命令更多的是用在数据库回滚,如果开发人员写的回滚命令有问题,则会出错。如果如生成较早的脚本,建议使用script去发布生成。
也不建议开发人员使用version回滚,如果version中间有别人的更新,有可能一起回滚了。
开发人员可以使用up回滚当前操作,但是一定要检查回滚命令的正确性。
如果脚本up时出错,但是脚本中在出错的SQL前有正确的SQL应用到了库,此时migrations认为该脚本没有执行,状态为pending,无法使用migratedown回滚,只能通过在数据库直接写SQL的方法回退。