BAEL-4287 - Rolling Back Migrations with Flyway (#9910)

* First commit - Flyway Undo

* Simplify migrations
Move to own package
This commit is contained in:
developerDiv 2020-09-06 16:27:59 +01:00 committed by GitHub
parent 3ddc6fd882
commit 1927bb1b46
4 changed files with 67 additions and 0 deletions

View File

@ -0,0 +1,6 @@
create table book (
id numeric,
title varchar(128),
author varchar(256),
constraint pk_book primary key (id)
);

View File

@ -0,0 +1 @@
drop table book;

View File

@ -0,0 +1,39 @@
package com.baeldung.flywayundo;
import org.flywaydb.core.Flyway;
import org.flywaydb.core.api.MigrationInfo;
import org.flywaydb.core.api.MigrationState;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import javax.sql.DataSource;
import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = FlywayUndoTestConfig.class)
@SpringBootTest
public class FlywayUndoMigrationIntegrationTest {
@Autowired
private DataSource dataSource;
@Test
public void givenMigrationsExist_whenApplyMigrations_migrationsAreSuccessful() {
Flyway flyway = Flyway.configure()
.dataSource(dataSource)
.schemas("undo")
.locations("db/undo")
.load();
flyway.migrate();
for (MigrationInfo info : flyway.info().all()) {
assertThat(info.getState()).isEqualTo(MigrationState.SUCCESS);
}
}
}

View File

@ -0,0 +1,21 @@
package com.baeldung.flywayundo;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import javax.sql.DataSource;
@Configuration
public class FlywayUndoTestConfig {
@Bean
public DataSource createDatasource() {
EmbeddedDatabaseBuilder dbBuilder = new EmbeddedDatabaseBuilder();
return dbBuilder.setType(EmbeddedDatabaseType.H2)
.setName("DATABASE")
.build();
}
}