2016-03-17 25 views
0

我需要創建liquibase changelog,它在運行時具有所有必需的changeset,並使用該動態創建的changelog流運行liquibase。用changelog xml作爲流輸入運行liquibase

我知道我們可以有靜態更新日誌xml文件並提供它們來運行liquibase。

我想知道是否有一種方法可以加載這個動態更改日誌xml並運行liquibase?

+0

什麼問題,你試圖用這樣的方法來解決新的資源訪問?可以實現自己的更新日誌讀取器和寫入器:https://liquibase.jira.com/wiki/display/CONTRIB/ChangeLogParser/ https://liquibase.jira.com/wiki/display/CONTRIB/ChangeLogSerializer,但是您的用例看起來很不尋常,可能以其他方式做。 – dbf

+0

我們的用例就像我們正在嘗試爲用戶特定的數據創建動態實體創建。我們將擁有一組將由主變更日誌xml文件處理的變更集。但是對於動態實體創建,我們希望以編程方式實現它,而無需重新啓動應用程序。在這裏,我們想創建changeset xml作爲流,並用它運行liquibase而不是物理文件。 –

回答

1

我有同樣的要求,我創建了一個加載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); 
    } 
} 

} 
相關問題