由于某种原因,没有关于在Java代码中运行liquibase的文档.我想为单元测试生成表.
我将如何直接在Java中运行它?
例如
Liquibase liquibase = new Liquibase()
liquibase.runUpdates() ?
Run Code Online (Sandbox Code Playgroud)
Nat*_*and 37
它应该是类似的(取自liquibase.integration.spring.SpringLiquibase源码):
java.sql.Connection c = YOUR_CONNECTION;
Liquibase liquibase = null;
try {
Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(c))
liquibase = new Liquibase(YOUR_CHANGELOG, new FileSystemResourceAccessor(), database);
liquibase.update();
} catch (SQLException e) {
throw new DatabaseException(e);
} finally {
if (c != null) {
try {
c.rollback();
c.close();
} catch (SQLException e) {
//nothing to do
}
}
}
Run Code Online (Sandbox Code Playgroud)
ResourceAccessor有多种实现方式,具体取决于应该如何找到更改日志文件.
我找到了一种方法,以实现建立使用数据库或者行家或Java。在上述的例子中FileSystemResourceAccessor(),不幸的是使得它如此,如果你部署这就需要建立从罐子本身数据库的应用程序,那么你最终不得不提取罐作为一个zip作为一种解决方法,因为只有存在这些liquibase文件在罐子里。这意味着您的 jar 最终是不可移植的,并且您必须拥有maven要设置数据库的任何位置。
使用这个结构:
src/main/resources/liquibase/db.changelog-master.xml
src/main/resources/liquibase/changelogs/...
您的数据库变更日志主文件可能如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">
<!-- <includeAll path="src/main/resources/liquibase/changelogs"/> -->
<include file="changelogs/my-date.1.sql" relativeToChangelogFile="true"/>
</databaseChangeLog>
Run Code Online (Sandbox Code Playgroud)
您可以将此部分用于您的pom.xml,以确保mvn install还将设置您的 liquibase 数据库。
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<changeLogFile>liquibase/db.changelog-master.xml</changeLogFile>
<driver>org.postgresql.Driver</driver>
<url>${jdbc.url}</url>
<username>${jdbc.username}</username>
<password>${jdbc.password}</password>
</configuration>
<executions>
<execution>
<phase>process-resources</phase>
<goals>
<goal>update</goal>
</goals>
</execution>
</executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)
使用ClassLoaderResourceAccessor()代替FileSystemResourceAccessor()。
public static void runLiquibase() {
Liquibase liquibase = null;
Connection c = null;
try {
c = DriverManager.getConnection(DataSources.PROPERTIES.getProperty("jdbc.url"),
DataSources.PROPERTIES.getProperty("jdbc.username"),
DataSources.PROPERTIES.getProperty("jdbc.password"));
Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(c));
log.info(DataSources.CHANGELOG_MASTER);
liquibase = new Liquibase(DataSources.CHANGELOG_MASTER, new ClassLoaderResourceAccessor(), database);
liquibase.update("main");
} catch (SQLException | LiquibaseException e) {
e.printStackTrace();
throw new NoSuchElementException(e.getMessage());
} finally {
if (c != null) {
try {
c.rollback();
c.close();
} catch (SQLException e) {
//nothing to do
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
21366 次 |
| 最近记录: |