commit
189f74220c
|
|
@ -25,7 +25,6 @@ import java.util.stream.Collectors;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
import liquibase.changelog.ChangeLogHistoryService;
|
|
||||||
import liquibase.changelog.ChangeSet.ExecType;
|
import liquibase.changelog.ChangeSet.ExecType;
|
||||||
import liquibase.changelog.RanChangeSet;
|
import liquibase.changelog.RanChangeSet;
|
||||||
import liquibase.changelog.StandardChangeLogHistoryService;
|
import liquibase.changelog.StandardChangeLogHistoryService;
|
||||||
|
|
@ -63,9 +62,8 @@ public class LiquibaseEndpoint {
|
||||||
while (target != null) {
|
while (target != null) {
|
||||||
Map<String, LiquibaseBean> liquibaseBeans = new HashMap<>();
|
Map<String, LiquibaseBean> liquibaseBeans = new HashMap<>();
|
||||||
DatabaseFactory factory = DatabaseFactory.getInstance();
|
DatabaseFactory factory = DatabaseFactory.getInstance();
|
||||||
StandardChangeLogHistoryService service = new StandardChangeLogHistoryService();
|
|
||||||
this.context.getBeansOfType(SpringLiquibase.class)
|
this.context.getBeansOfType(SpringLiquibase.class)
|
||||||
.forEach((name, liquibase) -> liquibaseBeans.put(name, createReport(liquibase, service, factory)));
|
.forEach((name, liquibase) -> liquibaseBeans.put(name, createReport(liquibase, factory)));
|
||||||
ApplicationContext parent = target.getParent();
|
ApplicationContext parent = target.getParent();
|
||||||
contextBeans.put(target.getId(),
|
contextBeans.put(target.getId(),
|
||||||
new ContextLiquibaseBeans(liquibaseBeans, (parent != null) ? parent.getId() : null));
|
new ContextLiquibaseBeans(liquibaseBeans, (parent != null) ? parent.getId() : null));
|
||||||
|
|
@ -74,8 +72,7 @@ public class LiquibaseEndpoint {
|
||||||
return new ApplicationLiquibaseBeans(contextBeans);
|
return new ApplicationLiquibaseBeans(contextBeans);
|
||||||
}
|
}
|
||||||
|
|
||||||
private LiquibaseBean createReport(SpringLiquibase liquibase, ChangeLogHistoryService service,
|
private LiquibaseBean createReport(SpringLiquibase liquibase, DatabaseFactory factory) {
|
||||||
DatabaseFactory factory) {
|
|
||||||
try {
|
try {
|
||||||
DataSource dataSource = liquibase.getDataSource();
|
DataSource dataSource = liquibase.getDataSource();
|
||||||
JdbcConnection connection = new JdbcConnection(dataSource.getConnection());
|
JdbcConnection connection = new JdbcConnection(dataSource.getConnection());
|
||||||
|
|
@ -88,6 +85,7 @@ public class LiquibaseEndpoint {
|
||||||
}
|
}
|
||||||
database.setDatabaseChangeLogTableName(liquibase.getDatabaseChangeLogTable());
|
database.setDatabaseChangeLogTableName(liquibase.getDatabaseChangeLogTable());
|
||||||
database.setDatabaseChangeLogLockTableName(liquibase.getDatabaseChangeLogLockTable());
|
database.setDatabaseChangeLogLockTableName(liquibase.getDatabaseChangeLogLockTable());
|
||||||
|
StandardChangeLogHistoryService service = new StandardChangeLogHistoryService();
|
||||||
service.setDatabase(database);
|
service.setDatabase(database);
|
||||||
return new LiquibaseBean(
|
return new LiquibaseBean(
|
||||||
service.getRanChangeSets().stream().map(ChangeSet::new).collect(Collectors.toList()));
|
service.getRanChangeSets().stream().map(ChangeSet::new).collect(Collectors.toList()));
|
||||||
|
|
|
||||||
|
|
@ -22,16 +22,19 @@ import java.util.Map;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
import liquibase.integration.spring.SpringLiquibase;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import org.springframework.boot.actuate.liquibase.LiquibaseEndpoint.LiquibaseBean;
|
import org.springframework.boot.actuate.liquibase.LiquibaseEndpoint.LiquibaseBean;
|
||||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||||
import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration;
|
import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration;
|
||||||
|
import org.springframework.boot.jdbc.EmbeddedDatabaseConnection;
|
||||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
|
@ -41,6 +44,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||||
* @author Eddú Meléndez
|
* @author Eddú Meléndez
|
||||||
* @author Andy Wilkinson
|
* @author Andy Wilkinson
|
||||||
* @author Stephane Nicoll
|
* @author Stephane Nicoll
|
||||||
|
* @author Leo Li
|
||||||
*/
|
*/
|
||||||
class LiquibaseEndpointTests {
|
class LiquibaseEndpointTests {
|
||||||
|
|
||||||
|
|
@ -92,6 +96,21 @@ class LiquibaseEndpointTests {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenMultipleLiquibaseBeansArePresentChangeSetsAreCorrectlyReportedForEachBean() {
|
||||||
|
this.contextRunner.withUserConfiguration(Config.class, MultipleDataSourceLiquibaseConfiguration.class)
|
||||||
|
.run((context) -> {
|
||||||
|
Map<String, LiquibaseBean> liquibaseBeans = context.getBean(LiquibaseEndpoint.class)
|
||||||
|
.liquibaseBeans().getContexts().get(context.getId()).getLiquibaseBeans();
|
||||||
|
assertThat(liquibaseBeans.get("liquibase").getChangeSets()).hasSize(1);
|
||||||
|
assertThat(liquibaseBeans.get("liquibase").getChangeSets().get(0).getChangeLog())
|
||||||
|
.isEqualTo("classpath:/db/changelog/db.changelog-master.yaml");
|
||||||
|
assertThat(liquibaseBeans.get("liquibaseBackup").getChangeSets()).hasSize(1);
|
||||||
|
assertThat(liquibaseBeans.get("liquibaseBackup").getChangeSets().get(0).getChangeLog())
|
||||||
|
.isEqualTo("classpath:/db/changelog/db.changelog-master-backup.yaml");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
private boolean getAutoCommit(DataSource dataSource) throws SQLException {
|
private boolean getAutoCommit(DataSource dataSource) throws SQLException {
|
||||||
try (Connection connection = dataSource.getConnection()) {
|
try (Connection connection = dataSource.getConnection()) {
|
||||||
return connection.getAutoCommit();
|
return connection.getAutoCommit();
|
||||||
|
|
@ -108,4 +127,42 @@ class LiquibaseEndpointTests {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Configuration(proxyBeanMethods = false)
|
||||||
|
static class MultipleDataSourceLiquibaseConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
DataSource dataSource() {
|
||||||
|
return createEmbeddedDatabase();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
DataSource dataSourceBackup() {
|
||||||
|
return createEmbeddedDatabase();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
SpringLiquibase liquibase(DataSource dataSource) {
|
||||||
|
return createSpringLiquibase("db.changelog-master.yaml", dataSource);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
SpringLiquibase liquibaseBackup(DataSource dataSourceBackup) {
|
||||||
|
return createSpringLiquibase("db.changelog-master-backup.yaml", dataSourceBackup);
|
||||||
|
}
|
||||||
|
|
||||||
|
private DataSource createEmbeddedDatabase() {
|
||||||
|
return new EmbeddedDatabaseBuilder().generateUniqueName(true)
|
||||||
|
.setType(EmbeddedDatabaseConnection.HSQL.getType()).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
private SpringLiquibase createSpringLiquibase(String changeLog, DataSource dataSource) {
|
||||||
|
SpringLiquibase liquibase = new SpringLiquibase();
|
||||||
|
liquibase.setChangeLog("classpath:/db/changelog/" + changeLog);
|
||||||
|
liquibase.setShouldRun(true);
|
||||||
|
liquibase.setDataSource(dataSource);
|
||||||
|
return liquibase;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,20 @@
|
||||||
|
databaseChangeLog:
|
||||||
|
- changeSet:
|
||||||
|
id: 1
|
||||||
|
author: leoli
|
||||||
|
changes:
|
||||||
|
- createTable:
|
||||||
|
tableName: customerbackup
|
||||||
|
columns:
|
||||||
|
- column:
|
||||||
|
name: id
|
||||||
|
type: int
|
||||||
|
autoIncrement: true
|
||||||
|
constraints:
|
||||||
|
primaryKey: true
|
||||||
|
nullable: false
|
||||||
|
- column:
|
||||||
|
name: name
|
||||||
|
type: varchar(50)
|
||||||
|
constraints:
|
||||||
|
nullable: false
|
||||||
Loading…
Reference in New Issue