简体   繁体   中英

Run liquibase with changelog xml as stream input

I've a requirement of creating liquibase changelog which has all the required changeset at runtime and run liquibase with that dynamically created changelog stream.

I know we can have static changelog xml files and feed them to run liquibase.

I wanted to know if there is a way in which I can load this dynamic changelog xml and run the liquibase?

I had the same requirement, I created a new resource accessor that loads a json string (is the same for 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);
    }
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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