这是spring bean配置:
@Bean
public SpringLiquibase liquibase() {
SpringLiquibase liquibase = new SpringLiquibase();
liquibase.setChangeLog("classpath:db-changelog.xml");
liquibase.setDataSource(dataSource());
return liquibase;
}
和来自 [liquibase - 网站][1] 的 xml 配置
<bean id="liquibase" class="liquibase.integration.spring.SpringLiquibase">
<property name="dataSource" ref="myDataSource" />
<property name="changeLog" value="classpath:db-changelog.xml" />
<!--
contexts specifies the runtime contexts to use.
-->
<property name="contexts" value="test, production" />
来源:SpringLiquibase - 这是代码执行更新,它是默认行为。
protected void performUpdate(Liquibase liquibase) throws LiquibaseException{
if (tag != null) {
liquibase.update(tag, new Contexts(getContexts()), new LabelExpression(getLabels()));
} else {
liquibase.update(new Contexts(getContexts()), new LabelExpression(getLabels()));
}
}
Liquibase 类有方法
public DiffResult diff(Database referenceDatabase,
Database targetDatabase,
CompareControl compareControl)
因此,您可以创建自定义 SpringLiquibase 并使用 diff 而不是 update
public class MyDiffSpringLiquibase extends SpringLiquibase {
@Override
protected void performUpdate(Liquibase liquibase) throws LiquibaseException {
Database referenceDatabase = new MySQLDatabase();
referenceDatabase.setConnection();
Database targetDatabase = new MySQLDatabase();
targetDatabase.setConnection();
CatalogAndSchema catalogAndSchemaReference = new CatalogAndSchema();
CatalogAndSchema catalogAndSchemacomparison = new CatalogAndSchema();
Set<Class<? extends DatabaseObject>> finalCompareTypes = null;
Class<? extends DatabaseObject>[] snapshotTypes = new Class[]{Table.class ,View.class......};
if (snapshotTypes != null && snapshotTypes.length > 0) {
finalCompareTypes = new HashSet<Class<? extends DatabaseObject>>(Arrays.asList(snapshotTypes));
}
CompareControl compareControl = new CompareControl(new CompareControl.SchemaComparison[]{new CompareControl.SchemaComparison(catalogAndSchemaReference, catalogAndSchemacomparison)}, finalCompareTypes);
liquibase.diff(referenceDatabase, targetDatabase, compareControl);
}
}
并将其注册为bean
@Bean
public SpringLiquibase liquibase() {
SpringLiquibase liquibase = new MyDiffSpringLiquibase ();
liquibase.setChangeLog("classpath:db-changelog.xml");
liquibase.setDataSource(dataSource());
return liquibase;
}