liquibase 有没有办法创建 java 代码更改集(即提供一个 java 类,它将接收 JDBC 连接并在数据库中执行一些更改)?
(我知道flyway有这样的功能)
是的,有这样的功能。您可以创建一个customChange
:
<customChange class="my.java.Class">
<param name="id" value="2" />
</customChange>
类必须实现liquibase.change.custom.CustomTaskChange
接口。
@Override
public void execute(final Database arg0) throws CustomChangeException {
JdbcConnection dbConn = (JdbcConnection) arg0.getConnection();
try {
... do funny stuff ...
} catch (Exception e) {
// swallow the exception !
}
}
一个完整的例子看起来像这样
创建一个实现 CustomTaskChange 或 CustomSqlChange 的类。
package com.example;
import liquibase.change.custom.CustomTaskChange;
import liquibase.database.Database;
import liquibase.database.jvm.JdbcConnection;
import liquibase.exception.CustomChangeException;
import liquibase.exception.SetupException;
import liquibase.exception.ValidationErrors;
import liquibase.logging.LogFactory;
import liquibase.resource.ResourceAccessor;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class DataLoaderTask implements CustomTaskChange {
//to hold the parameter value
private String file;
private ResourceAccessor resourceAccessor;
public String getFile() {
return file;
}
public void setFile(String file) {
this.file = file;
}
@Override
public void execute(Database database) throws CustomChangeException {
JdbcConnection databaseConnection = (JdbcConnection) database.getConnection();
try {
//Opening my data file
BufferedReader in = new BufferedReader(
new InputStreamReader(resourceAccessor.getResourceAsStream(file)));
//Ignore header
String str = in.readLine();
while ((str = in.readLine()) != null && !str.trim().equals("")) {
LogFactory.getLogger().info("Processing line "+ str);
//Do whatever is necessary
}
in.close();
} catch (Exception e) {
throw new CustomChangeException(e);
}
}
@Override
public String getConfirmationMessage() {
return null;
}
@Override
public void setUp() throws SetupException {
}
@Override
public void setFileOpener(ResourceAccessor resourceAccessor) {
this.resourceAccessor = resourceAccessor;
}
@Override
public ValidationErrors validate(Database database) {
return null;
}
}
在变更集 xml 中,您可以使用如下类
<changeSet id="1" author="murali" runAlways="false" failOnError="true" >
<customChange class="com.example.DataLoaderTask">
<param name="file" value="/com/example/data/user.csv" />
</customChange>
</changeSet>
对我来说,数据文件位于 src/main/resources/com/example/data 目录中
希望这可以帮助