Java代码实现Liquiba的基本使⽤⽅法
前不久公司要求使⽤Liquiba替代Flyway对数据库版本进⾏管理,要求项⽬启动时进⾏数据库升级,但在⽹上搜索了⼀⼤圈,也不知道为啥,⼏乎没有关于使⽤Java代码操作Liquiba的教程,都是通过命令⾏进⾏管理。费了些劲,整理了Java操作 Liquiba 的⼏种常⽤⽅法,在此记录⼀下。
update
update 通常⽤于将更改⽇志⽂件中指定的数据库更改应⽤到您的数据库。
/**
* 更新数据库
*/
melt
public void update(String tag)throws SQLException {
Connection connection =null;
try{
connection = Connection((String)yaml().get("url"),(String)yaml().get("urname"),(String)yaml().get("password"));
Databa databa = Instance().findCorrectDatabaImplementation(new JdbcConnection(connection));
Liquiba liquiba =new Liquiba(changeLogFile,new ClassLoaderResourceAccessor(), databa);
//命令⾏输出⽇志
/*StringWriter writer = new StringWriter();
liquiba.update("", writer);
logger.info('\n'+String());*/
//将即将要执⾏的执⾏⽇志写⼊⽂件,不更新数据库
/*String migrationSqlOutputFile = (String) yaml().get("migrationSqlOutputFile");
PrintWriter printWriter = new PrintWriter(migrationSqlOutputFile);
liquiba.update("", printWriter);
ice age
logger.info("记录已写⼊" + migrationSqlOutputFile);*/
liquiba.update("");//更新数据库
liquiba.tag(tag);//打标签
河外星系logger.info("<<<<<<<<<<;数据库已更新,新标签为 "+ tag +" >>>>>>>>>>");
}catch(Exception e){
e.printStackTrace();
}finally{
设计专业考研if(connection !=null){
connection.clo();
}
}
}
rollback
rollback 通常在您想要恢复数据库中的更改时使⽤。共有三种rollback模式:
/**
* 回滚
*/
public void rollback()throws SQLException {
Connection connection =null;
try{
connection = Connection((String)yaml().get("url"),(String)yaml().get("urname"),(String)yaml().get("password"));
Databa databa = Instance().findCorrectDatabaImplementation(new JdbcConnection(connection));
Liquiba liquiba =new Liquiba(changeLogFile,new ClassLoaderResourceAccessor(), databa);
//PrintStream printStream = new PrintStream("src/main/resources/db_l");
/
/llback(2,"");
}catch(Exception e){
e.printStackTrace();
}finally{
if(connection !=null){
connection.clo();
}
}
}
diff
diff 允许您将相同类型或不同类型的两个数据库相互⽐较,并⽣成相应的差异⽂件。
/**
* ⽣成两数据库对⽐差异
*/
public void diffChangeLog(String diffChangeLogFile, String referenceUrl, String referenceUrname, String referencePassword,
String targetUrl, String targetUrname, String targetPassword)throws SQLException {
String outputFile =(String)yaml().get("outputFile");
Connection refConnection = Connection(referenceUrl,referenceUrname,referencePassword);
Connection tarConnection = Connection(targetUrl,targetUrname,targetPassword);
怎样保护皮肤
初二化学上册知识点
try{
Databa targetDataba = Instance().findCorrectDatabaImplementation(new JdbcConnection(tarConnection));
Databa referenceDataba = Instance().findCorrectDatabaImplementation(new JdbcConnection(refConnection));
Liquiba liquiba =new Liquiba("",new ClassLoaderResourceAccessor(), referenceDataba);
//详细对⽐⽂档
CommandLineUtils.doDiff(referenceDataba, targetDataba, imToNull((String)yaml().get("diffTypes")),null,new PrintStream(output File));
//⽣成差异⽂档数据
CommandLineUtils.doDiffToChangeLog(diffChangeLogFile, referenceDataba, targetDataba,
new DiffOutputControl().tIncludeCatalog(fal).tIncludeSchema(fal),null,
logger.info("<<<<<<<<<<;数据库差异已写⼊ "+ diffChangeLogFile +" >>>>>>>>>>");
}catch(Exception e){
e.printStackTrace();
}finally{
if(refConnection !=null){
refConnection.clo();
tarConnection.clo();
}
}
}
其中:
reference 代表源数据库
target 代表⽬标数据库
changeLogSync
changelogSync 更新DATABASECHANGELOG表以将所有未应⽤于数据库的更改标记为已执⾏,通常在您想要为新的数据库环境建⽴基线时使⽤。
/**
* 同步数据库版本号 changeLogSync
*/
public void changeLogSync(String tag)throws SQLException {
Connection connection =null;
try{
connection = Connection((String)yaml().get("targetUrl"),(String)yaml().get("targetUrname"),(String)yaml().get("targetPassw ord"));
Databa databa = Instance().findCorrectDatabaImplementation(new JdbcConnection(connection));
Liquiba liquiba =new Liquiba(changeLogFile,new ClassLoaderResourceAccessor(), databa);
liquiba.changeLogSync("");
logger.info("<<<<<<<<<< 已同步 >>>>>>>>>>");
liquiba.tag(tag);
logger.info("<<<<<<<<<<;标签为 "+ tag +" >>>>>>>>>>");
}catch(Exception e){
e.printStackTrace();
}finally{
上海翻译公司报价
if(connection !=null){
connection.clo();
}
}
}
sb google com
respectivegenerateChangeLog
generateChangeLog 通常⽤于捕获数据库的当前状态,然后将这些更改应⽤于任意数量的数据库。这通常仅在项⽬具有现有数据库但之前未使⽤过Liquiba 时才进⾏。
/**
* ⾃动⽣成数据库执⾏脚本,全量导出
*/
public void GenerateChangeLog(String outputChangeLogFile)throws SQLException {
final Set<Class<?extends DatabaObject>> compareTypes =new HashSet<>();
compareTypes.add(Column.class);
compareTypes.add(Data.class);
compareTypes.add(ForeignKey.class);
compareTypes.add(Index.class);
compareTypes.add(PrimaryKey.class);
compareTypes.add(Sequence.class);
compareTypes.add(Table.class);
compareTypes.add(UniqueConstraint.class);
compareTypes.add(View.class);
Connection connection =null;
try{
connection = Connection((String)yaml().get("targetUrl"),(String)yaml().get("referenceUrname"),(String)yaml().get("referenc ePassword"));
Databa databa = Instance().findCorrectDatabaImplementation(new JdbcConnection(connection));
Liquiba liquiba =new Liquiba(outputChangeLogFile,new ClassLoaderResourceAccessor(), databa);
DiffToChangeLog diffToChangeLog =new DiffToChangeLog(new DiffOutputControl());
//ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PrintStream printStream =new PrintStream(outputChangeLogFile);
proven
logger.info("<<<<<<<<<< 开始获取 >>>>>>>>>>");
diffToChangeLog.print(printStream);
logger.info("<<<<<<<<<< 获取完成 >>>>>>>>>>");
}catch(Exception e){
e.printStackTrace();
}finally{
if(connection !=null){
connection.clo();
}
}
}
关于代码中的获取yaml数据的⽅法,请移步