繁体   English   中英

使用changelog xml作为流输入运行liquibase

[英]Run liquibase with changelog xml as stream input

我需要创建liquibase changelog,它在运行时具有所有必需的变更集,并使用该动态创建的changelog流运行liquibase。

我知道我们可以有静态的changelog xml文件,并提供它们以运行liquibase。

我想知道是否有一种方法可以加载此动态变更日志xml并运行liquibase?

我有相同的要求,我创建了一个新的资源访问器,该访问器加载了json字符串(对于xml来说是相同的)

public class Main {

private class StreamResourceAccessor extends AbstractResourceAccessor {
    private String json;
    public StreamResourceAccessor(String jsonString) {
        super();
        this.json = jsonString;
    }

    @Override
    public Set<InputStream> getResourcesAsStream(String name) throws IOException {
        InputStream stream = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8));

        Set<InputStream> returnSet = new HashSet<>();
        returnSet.add(stream);
        return returnSet;
    }

    @Override
    public Set<String> list(String arg0, String arg1, boolean arg2, boolean arg3, boolean arg4) throws IOException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public ClassLoader toClassLoader() {
        // TODO Auto-generated method stub
        return null;
    }

}

public static void main(String[] args) throws SQLException, LiquibaseException {
    String url = "jdbc:postgresql://localhost/postgres";
    Properties props = new Properties();
    props.setProperty("user","postgres");
    props.setProperty("password","");
    //props.setProperty("ssl","true");
    Connection connection = DriverManager.getConnection(url, props);

    Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection));
    String jsonText = "{\"databaseChangeLog\":[{\"preConditions\":[{\"runningAs\":{\"username\":\"postgres\"}}]},{\"changeSet\":{\"id\":\"1\",\"author\":\"nvoxland\",\"changes\":[{\"createTable\":{\"tableName\":\"person\",\"columns\":[{\"column\":{\"name\":\"id\",\"type\":\"int\",\"autoIncrement\":true,\"constraints\":{\"primaryKey\":true,\"nullable\":false},}},{\"column\":{\"name\":\"firstname\",\"type\":\"varchar(50)\"}},{\"column\":{\"name\":\"lastname\",\"type\":\"varchar(50)\",\"constraints\":{\"nullable\":false},}},{\"column\":{\"name\":\"state\",\"type\":\"char(2)\"}}]}}]}},{\"changeSet\":{\"id\":\"2\",\"author\":\"nvoxland\",\"changes\":[{\"addColumn\":{\"tableName\":\"person\",\"columns\":[{\"column\":{\"name\":\"username\",\"type\":\"varchar(8)\"}}]}}]}},{\"changeSet\":{\"id\":\"3\",\"author\":\"nvoxland\",\"changes\":[{\"addLookupTable\":{\"tableName\":\"person\",\"existingTableName\":\"person\",\"existingColumnName\":\"state\",\"newTableName\":\"state\",\"newColumnName\":\"id\",\"newColumnDataType\":\"char(2)\",}}]}}]}";

    Main main = new Main();
    ResourceAccessor resourceAccessor = main.new StreamResourceAccessor(jsonText);

    Liquibase liquibase = new Liquibase("hello.json", resourceAccessor, database);
    try {
        Contexts context = new Contexts();
        liquibase.update(context);
    } catch (Exception e) {
        System.out.println(e);
    }
}

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM