English 中文(简体)
Java守则中的立基
原标题:Running liquibase within Java code

出于某种原因,没有在 Java守则内建立立基的文件。 我想为单位测试制作表格。

我如何在 Java直接管理?

e.g.

Liquibase liquibase = new Liquibase()
liquibase.runUpdates() ?
最佳回答

它应当像(从利quibase.integration中吸收)。

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
        }
    }
}

视如何找到您的换文档案,资源接收器有多种用途。

问题回答

我找到了一种办法,利用eithermaven或Java建立数据库。 以上例子使用<代码> 文件文件SystemResourceAccessor(,不幸地使其得以部署,如果你需要从杰尔本身建立一个数据库,那么你最终必须达到extract<>。 由于这些立基文件只在杰尔才存在,因此作为工作方的杰尔语。 这就是说,你最后是 is,必须有<条码>。 凡是想要建立数据库的人,都会这样做。

利用这一结构:

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>

您可以将这一节用于您的<代码>pom.xml,以确保mvnstal还将设立你的数据库。

<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>

使用<代码>ClassLoaderResourceAccessor()代替

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
            }
        }
    }
}

检测中可使用单-数据基(“db/changelog.xml”是主要/资源/db/changelog.xml):

import liquibase.Contexts;
import liquibase.Liquibase;
import liquibase.database.Database;
import liquibase.database.DatabaseFactory;
import liquibase.database.jvm.JdbcConnection;
import liquibase.exception.LiquibaseException;
import liquibase.resource.ClassLoaderResourceAccessor;
import org.junit.jupiter.api.Test;

import java.sql.DriverManager;
import java.sql.SQLException;

public class LiquidBaseTest {

    @Test
    public void testExecuteLiquidBaseScripts() throws SQLException, LiquibaseException {
        java.sql.Connection connection = DriverManager.getConnection("jdbc:h2:mem:");

        try {
            Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection));
            Liquibase liquibase = new Liquibase("db/changelog.xml", new ClassLoaderResourceAccessor(), database);
            liquibase.update(new Contexts());
        } finally {
            if (connection != null) {
                connection.rollback();
                connection.close();
            }
        }
    }

}
public static void runLiquibase() throws Exception {
    Map<String, Object> config = new HashMap<>();

    Scope.child(config, () -> {
        try {
            Connection connection = DriverManager.getConnection("your database connection url");
            Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection));
            Liquibase liquibase = new liquibase.Liquibase("database/db.changelog-main.xml", new ClassLoaderResourceAccessor(), database);
            liquibase.update(new Contexts(), new LabelExpression());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    });

}

该守则将有助于你根据该版本管理多个变更意见,而且你可以提供主要文件参考。

我遵循的结构如下。

  • src/main/resources/database/db.changelog-main.xml
  • src/main/resources/database/changelogs/db.changelog-v-1.0.0.xml
  • src/main/resources/database/changelogs/db.changelog-v-1.0.1.xml

参考链接 rel=“nofollow noretinger”>https://docs.liquibase.com/workflows/liquibase- community/using-liquibase-java-api.html





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...