Add Liquibase rollback file support

Add a `liquibase.rollback-file` property which can be used to write
a rollback file when migrations occur.

Fixes gh-1826
Closes gh-5535
pull/5614/head
Mohamed Gazal 9 years ago committed by Phillip Webb
parent f43123f2fa
commit 3623eeda0c

@ -105,6 +105,7 @@ public class LiquibaseAutoConfiguration {
liquibase.setShouldRun(this.properties.isEnabled());
liquibase.setLabels(this.properties.getLabels());
liquibase.setChangeLogParameters(this.properties.getParameters());
liquibase.setRollbackFile(this.properties.getRollbackFile());
return liquibase;
}

@ -16,6 +16,7 @@
package org.springframework.boot.autoconfigure.liquibase;
import java.io.File;
import java.util.Map;
import javax.validation.constraints.NotNull;
@ -90,6 +91,11 @@ public class LiquibaseProperties {
*/
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() {
return this.changeLog;
}
@ -178,4 +184,12 @@ public class LiquibaseProperties {
this.parameters = parameters;
}
public File getRollbackFile() {
return this.rollbackFile;
}
public void setRollbackFile(File rollbackFile) {
this.rollbackFile = rollbackFile;
}
}

@ -16,6 +16,7 @@
package org.springframework.boot.autoconfigure.liquibase;
import java.io.File;
import java.util.Map;
import liquibase.integration.spring.SpringLiquibase;
@ -24,6 +25,7 @@ import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
@ -32,6 +34,7 @@ import org.springframework.boot.liquibase.CommonsLoggingLiquibaseLogger;
import org.springframework.boot.test.util.EnvironmentTestUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.util.FileCopyUtils;
import static org.assertj.core.api.Assertions.assertThat;
@ -45,6 +48,9 @@ public class LiquibaseAutoConfigurationTests {
@Rule
public ExpectedException expected = ExpectedException.none();
@Rule
public TemporaryFolder temp = new TemporaryFolder();
private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
@Before
@ -192,4 +198,20 @@ public class LiquibaseAutoConfigurationTests {
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;");
}
}

Loading…
Cancel
Save