From 1927bb1b460752245b26b441a637138e0eebaaa2 Mon Sep 17 00:00:00 2001 From: developerDiv <34768329+developerDiv@users.noreply.github.com> Date: Sun, 6 Sep 2020 16:27:59 +0100 Subject: [PATCH] BAEL-4287 - Rolling Back Migrations with Flyway (#9910) * First commit - Flyway Undo * Simplify migrations Move to own package --- .../db/undo/V1_0__create_book_table.sql | 6 +++ .../db/undo/V2_0__drop_table_book.sql | 1 + .../FlywayUndoMigrationIntegrationTest.java | 39 +++++++++++++++++++ .../flywayundo/FlywayUndoTestConfig.java | 21 ++++++++++ 4 files changed, 67 insertions(+) create mode 100644 persistence-modules/flyway/src/main/resources/db/undo/V1_0__create_book_table.sql create mode 100644 persistence-modules/flyway/src/main/resources/db/undo/V2_0__drop_table_book.sql create mode 100644 persistence-modules/flyway/src/test/java/com/baeldung/flywayundo/FlywayUndoMigrationIntegrationTest.java create mode 100644 persistence-modules/flyway/src/test/java/com/baeldung/flywayundo/FlywayUndoTestConfig.java diff --git a/persistence-modules/flyway/src/main/resources/db/undo/V1_0__create_book_table.sql b/persistence-modules/flyway/src/main/resources/db/undo/V1_0__create_book_table.sql new file mode 100644 index 0000000000..105da7a0c0 --- /dev/null +++ b/persistence-modules/flyway/src/main/resources/db/undo/V1_0__create_book_table.sql @@ -0,0 +1,6 @@ +create table book ( + id numeric, + title varchar(128), + author varchar(256), + constraint pk_book primary key (id) +); \ No newline at end of file diff --git a/persistence-modules/flyway/src/main/resources/db/undo/V2_0__drop_table_book.sql b/persistence-modules/flyway/src/main/resources/db/undo/V2_0__drop_table_book.sql new file mode 100644 index 0000000000..cd800b00b7 --- /dev/null +++ b/persistence-modules/flyway/src/main/resources/db/undo/V2_0__drop_table_book.sql @@ -0,0 +1 @@ +drop table book; \ No newline at end of file diff --git a/persistence-modules/flyway/src/test/java/com/baeldung/flywayundo/FlywayUndoMigrationIntegrationTest.java b/persistence-modules/flyway/src/test/java/com/baeldung/flywayundo/FlywayUndoMigrationIntegrationTest.java new file mode 100644 index 0000000000..03004baf60 --- /dev/null +++ b/persistence-modules/flyway/src/test/java/com/baeldung/flywayundo/FlywayUndoMigrationIntegrationTest.java @@ -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); + } + } +} diff --git a/persistence-modules/flyway/src/test/java/com/baeldung/flywayundo/FlywayUndoTestConfig.java b/persistence-modules/flyway/src/test/java/com/baeldung/flywayundo/FlywayUndoTestConfig.java new file mode 100644 index 0000000000..5f00626179 --- /dev/null +++ b/persistence-modules/flyway/src/test/java/com/baeldung/flywayundo/FlywayUndoTestConfig.java @@ -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(); + } + +}