diff --git a/persistence-modules/flyway-repair/README.MD b/persistence-modules/flyway-repair/README.MD new file mode 100644 index 0000000000..daeb9012b5 --- /dev/null +++ b/persistence-modules/flyway-repair/README.MD @@ -0,0 +1,3 @@ +### Relevant Articles: +- [Database Migrations with Flyway](http://www.baeldung.com/database-migrations-with-flyway) +- [A Guide to Flyway Callbacks](http://www.baeldung.com/flyway-callbacks) diff --git a/persistence-modules/flyway-repair/docker-compose/.env b/persistence-modules/flyway-repair/docker-compose/.env new file mode 100644 index 0000000000..52bd0d6510 --- /dev/null +++ b/persistence-modules/flyway-repair/docker-compose/.env @@ -0,0 +1,2 @@ +MYSQL_PORT=3316 +POSTGRES_PORT=5431 \ No newline at end of file diff --git a/persistence-modules/flyway-repair/docker-compose/docker-compose.yaml b/persistence-modules/flyway-repair/docker-compose/docker-compose.yaml new file mode 100644 index 0000000000..23270c043d --- /dev/null +++ b/persistence-modules/flyway-repair/docker-compose/docker-compose.yaml @@ -0,0 +1,23 @@ +version: '3.0' + +services: + mysql-test: + image: mysql:8.0.17 + ports: + - ${MYSQL_PORT}:3306 + env_file: + - mysql.env + networks: + - baeldung + + postgres-test: + image: postgres:11.5 + ports: + - ${POSTGRES_PORT}:5432 + env_file: postgres.env + networks: + - baeldung + +networks: + baeldung: + driver: bridge \ No newline at end of file diff --git a/persistence-modules/flyway-repair/docker-compose/mysql.env b/persistence-modules/flyway-repair/docker-compose/mysql.env new file mode 100644 index 0000000000..597344d88c --- /dev/null +++ b/persistence-modules/flyway-repair/docker-compose/mysql.env @@ -0,0 +1,5 @@ +MYSQL_ROOT_PASSWORD=password +MYSQL_RANDOM_ROOT_PASSWORD=yes +MYSQL_USER=testuser +MYSQL_PASSWORD=password +MYSQL_DATABASE=testdb \ No newline at end of file diff --git a/persistence-modules/flyway-repair/docker-compose/postgres.env b/persistence-modules/flyway-repair/docker-compose/postgres.env new file mode 100644 index 0000000000..1e7373fdf0 --- /dev/null +++ b/persistence-modules/flyway-repair/docker-compose/postgres.env @@ -0,0 +1,3 @@ +POSTGRES_USER=testuser +POSTGRES_PASSWORD=password +POSTGRES_DB=testdb diff --git a/persistence-modules/flyway-repair/pom.xml b/persistence-modules/flyway-repair/pom.xml new file mode 100644 index 0000000000..f9d62d5dad --- /dev/null +++ b/persistence-modules/flyway-repair/pom.xml @@ -0,0 +1,105 @@ + + + 4.0.0 + flyway + flyway-repair + jar + Flyway Repair Demo + + + com.baeldung + parent-boot-2 + 0.0.1-SNAPSHOT + ../../parent-boot-2 + + + + + org.flywaydb + flyway-core + ${flyway-core.version} + + + org.springframework.boot + spring-boot-starter-jdbc + + + + + + + org.flywaydb + flyway-maven-plugin + ${flyway-maven-plugin.version} + + + org.springframework.boot + spring-boot-maven-plugin + + + + + true + src/main/resources + + *.properties + db/**/*.sql + + + + + + + 6.5.0 + 6.5.0 + src/main/resources/application-${db.engine}.properties + + + + + + h2 + + h2 + + + + com.h2database + h2 + runtime + + + + + + mysql + + mysql + + + + mysql + mysql-connector-java + runtime + + + + + + postgres + + postgres + + + + org.postgresql + postgresql + runtime + + + + + + + diff --git a/persistence-modules/flyway-repair/src/main/java/com/baeldung/flywaycallbacks/FlywayApplication.java b/persistence-modules/flyway-repair/src/main/java/com/baeldung/flywaycallbacks/FlywayApplication.java new file mode 100644 index 0000000000..34d794f7d1 --- /dev/null +++ b/persistence-modules/flyway-repair/src/main/java/com/baeldung/flywaycallbacks/FlywayApplication.java @@ -0,0 +1,13 @@ +package com.baeldung.flywaycallbacks; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class FlywayApplication { + + public static void main(String[] args) { + SpringApplication.run(FlywayApplication.class, args); + } + +} diff --git a/persistence-modules/flyway-repair/src/main/resources/application-callbacks.properties b/persistence-modules/flyway-repair/src/main/resources/application-callbacks.properties new file mode 100644 index 0000000000..2c10fc711f --- /dev/null +++ b/persistence-modules/flyway-repair/src/main/resources/application-callbacks.properties @@ -0,0 +1 @@ +flyway.locations=db/migration,db/callbacks \ No newline at end of file diff --git a/persistence-modules/flyway-repair/src/main/resources/application-h2.properties b/persistence-modules/flyway-repair/src/main/resources/application-h2.properties new file mode 100644 index 0000000000..15bd482adf --- /dev/null +++ b/persistence-modules/flyway-repair/src/main/resources/application-h2.properties @@ -0,0 +1,3 @@ +flyway.url=jdbc:h2:file:./testdb;DB_CLOSE_ON_EXIT=FALSE;AUTO_RECONNECT=TRUE;MODE=MySQL;DATABASE_TO_UPPER=false; +flyway.user=testuser +flyway.password=password \ No newline at end of file diff --git a/persistence-modules/flyway-repair/src/main/resources/application-mysql.properties b/persistence-modules/flyway-repair/src/main/resources/application-mysql.properties new file mode 100644 index 0000000000..341f978068 --- /dev/null +++ b/persistence-modules/flyway-repair/src/main/resources/application-mysql.properties @@ -0,0 +1,3 @@ +flyway.url=jdbc:mysql://127.0.0.1:3316/testdb +flyway.user=testuser +flyway.password=password \ No newline at end of file diff --git a/persistence-modules/flyway-repair/src/main/resources/application-postgres.properties b/persistence-modules/flyway-repair/src/main/resources/application-postgres.properties new file mode 100644 index 0000000000..5afaca96b5 --- /dev/null +++ b/persistence-modules/flyway-repair/src/main/resources/application-postgres.properties @@ -0,0 +1,3 @@ +flyway.url=jdbc:postgresql://127.0.0.1:5431/testdb +flyway.user=testuser +flyway.password=password \ No newline at end of file diff --git a/persistence-modules/flyway-repair/src/main/resources/application.properties b/persistence-modules/flyway-repair/src/main/resources/application.properties new file mode 100644 index 0000000000..3fae835eb1 --- /dev/null +++ b/persistence-modules/flyway-repair/src/main/resources/application.properties @@ -0,0 +1,6 @@ +spring.profiles.include=@db.engine@ + +spring.datasource.url=${flyway.url} +spring.datasource.username=${flyway.user} +spring.datasource.password=${flyway.password} +spring.flyway.locations=${flyway.locations:db/migration} \ No newline at end of file diff --git a/persistence-modules/flyway-repair/src/main/resources/db/callbacks/afterMigrateError.sql b/persistence-modules/flyway-repair/src/main/resources/db/callbacks/afterMigrateError.sql new file mode 100644 index 0000000000..c975e85056 --- /dev/null +++ b/persistence-modules/flyway-repair/src/main/resources/db/callbacks/afterMigrateError.sql @@ -0,0 +1 @@ +DELETE FROM flyway_schema_history WHERE success=false; \ No newline at end of file diff --git a/persistence-modules/flyway-repair/src/main/resources/db/migration/V1_0__add_table_one.sql b/persistence-modules/flyway-repair/src/main/resources/db/migration/V1_0__add_table_one.sql new file mode 100644 index 0000000000..ec434dd5b2 --- /dev/null +++ b/persistence-modules/flyway-repair/src/main/resources/db/migration/V1_0__add_table_one.sql @@ -0,0 +1,3 @@ +create table table_one ( + id numeric primary key +); \ No newline at end of file diff --git a/persistence-modules/flyway-repair/src/main/resources/db/migration/V1_1__add_table_two.sql b/persistence-modules/flyway-repair/src/main/resources/db/migration/V1_1__add_table_two.sql new file mode 100644 index 0000000000..48720d59d6 --- /dev/null +++ b/persistence-modules/flyway-repair/src/main/resources/db/migration/V1_1__add_table_two.sql @@ -0,0 +1,3 @@ +create table table_two ( + id numeric primary key +); \ No newline at end of file diff --git a/persistence-modules/flyway-repair/src/main/resources/db/migration/V1_2__add_table_three.sql b/persistence-modules/flyway-repair/src/main/resources/db/migration/V1_2__add_table_three.sql new file mode 100644 index 0000000000..1917c88b9b --- /dev/null +++ b/persistence-modules/flyway-repair/src/main/resources/db/migration/V1_2__add_table_three.sql @@ -0,0 +1,3 @@ +create table table_three ( + id numeric primary key +); \ No newline at end of file diff --git a/persistence-modules/flyway-repair/src/main/resources/db/migration/V1_3__add_table.sql b/persistence-modules/flyway-repair/src/main/resources/db/migration/V1_3__add_table.sql new file mode 100644 index 0000000000..ec434dd5b2 --- /dev/null +++ b/persistence-modules/flyway-repair/src/main/resources/db/migration/V1_3__add_table.sql @@ -0,0 +1,3 @@ +create table table_one ( + id numeric primary key +); \ No newline at end of file diff --git a/persistence-modules/flyway-repair/src/main/resources/logback.xml b/persistence-modules/flyway-repair/src/main/resources/logback.xml new file mode 100644 index 0000000000..56af2d397e --- /dev/null +++ b/persistence-modules/flyway-repair/src/main/resources/logback.xml @@ -0,0 +1,19 @@ + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + + + + + + + \ No newline at end of file