Commit 40e51021 authored by Phillip Webb's avatar Phillip Webb

Merge pull request #5535 from gazal-k/gh-1826

* gh-1826:
  Add Liquibase rollback file support
parents f43123f2 3623eeda
...@@ -105,6 +105,7 @@ public class LiquibaseAutoConfiguration { ...@@ -105,6 +105,7 @@ public class LiquibaseAutoConfiguration {
liquibase.setShouldRun(this.properties.isEnabled()); liquibase.setShouldRun(this.properties.isEnabled());
liquibase.setLabels(this.properties.getLabels()); liquibase.setLabels(this.properties.getLabels());
liquibase.setChangeLogParameters(this.properties.getParameters()); liquibase.setChangeLogParameters(this.properties.getParameters());
liquibase.setRollbackFile(this.properties.getRollbackFile());
return liquibase; return liquibase;
} }
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
package org.springframework.boot.autoconfigure.liquibase; package org.springframework.boot.autoconfigure.liquibase;
import java.io.File;
import java.util.Map; import java.util.Map;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
...@@ -90,6 +91,11 @@ public class LiquibaseProperties { ...@@ -90,6 +91,11 @@ public class LiquibaseProperties {
*/ */
private Map<String, String> parameters; private Map<String, String> parameters;
/**
* The file to which rollback SQL will be written when an update is performed.
*/
private File rollbackFile;
public String getChangeLog() { public String getChangeLog() {
return this.changeLog; return this.changeLog;
} }
...@@ -178,4 +184,12 @@ public class LiquibaseProperties { ...@@ -178,4 +184,12 @@ public class LiquibaseProperties {
this.parameters = parameters; this.parameters = parameters;
} }
public File getRollbackFile() {
return this.rollbackFile;
}
public void setRollbackFile(File rollbackFile) {
this.rollbackFile = rollbackFile;
}
} }
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
package org.springframework.boot.autoconfigure.liquibase; package org.springframework.boot.autoconfigure.liquibase;
import java.io.File;
import java.util.Map; import java.util.Map;
import liquibase.integration.spring.SpringLiquibase; import liquibase.integration.spring.SpringLiquibase;
...@@ -24,6 +25,7 @@ import org.junit.Before; ...@@ -24,6 +25,7 @@ import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException; import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.BeanCreationException;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
...@@ -32,6 +34,7 @@ import org.springframework.boot.liquibase.CommonsLoggingLiquibaseLogger; ...@@ -32,6 +34,7 @@ import org.springframework.boot.liquibase.CommonsLoggingLiquibaseLogger;
import org.springframework.boot.test.util.EnvironmentTestUtils; import org.springframework.boot.test.util.EnvironmentTestUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.test.util.ReflectionTestUtils; import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.util.FileCopyUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
...@@ -45,6 +48,9 @@ public class LiquibaseAutoConfigurationTests { ...@@ -45,6 +48,9 @@ public class LiquibaseAutoConfigurationTests {
@Rule @Rule
public ExpectedException expected = ExpectedException.none(); public ExpectedException expected = ExpectedException.none();
@Rule
public TemporaryFolder temp = new TemporaryFolder();
private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
@Before @Before
...@@ -192,4 +198,20 @@ public class LiquibaseAutoConfigurationTests { ...@@ -192,4 +198,20 @@ public class LiquibaseAutoConfigurationTests {
assertThat(parameters.get("foo")).isEqualTo("bar"); assertThat(parameters.get("foo")).isEqualTo("bar");
} }
@Test
public void testRollbackFile() throws Exception {
File file = this.temp.newFile("rollback-file.sql");
EnvironmentTestUtils.addEnvironment(this.context,
"liquibase.rollbackFile:" + file.getAbsolutePath());
this.context.register(EmbeddedDataSourceConfiguration.class,
LiquibaseAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
SpringLiquibase liquibase = this.context.getBean(SpringLiquibase.class);
File actualFile = (File) ReflectionTestUtils.getField(liquibase, "rollbackFile");
assertThat(actualFile).isEqualTo(file).exists();
String content = new String(FileCopyUtils.copyToByteArray(file));
assertThat(content).contains("DROP TABLE PUBLIC.customer;");
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment