From 6ab2df569844c31dd215ed32ed39bc3e99e1afbe Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Mon, 7 Jun 2021 20:40:16 -0700 Subject: [PATCH] Order DataSourceScriptDatabaseInitializer last Change the order of `DataSourceScriptDatabaseInitializerDetector` so that it always runs last. This update allows script initialization to be combined with a high-level migration tool such as Flyway. Closes gh-26692 --- .../init/DataSourceScriptDatabaseInitializerDetector.java | 8 ++++++++ .../src/main/resources/data.sql | 1 + .../smoketest/flyway/SampleFlywayApplicationTests.java | 2 +- 3 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-flyway/src/main/resources/data.sql diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/init/DataSourceScriptDatabaseInitializerDetector.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/init/DataSourceScriptDatabaseInitializerDetector.java index 48b2332d30f4..3091c8019d1e 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/init/DataSourceScriptDatabaseInitializerDetector.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/init/DataSourceScriptDatabaseInitializerDetector.java @@ -21,6 +21,7 @@ import org.springframework.boot.sql.init.dependency.AbstractBeansOfTypeDatabaseInitializerDetector; import org.springframework.boot.sql.init.dependency.DatabaseInitializerDetector; +import org.springframework.core.Ordered; /** * A {@link DatabaseInitializerDetector} for {@link DataSourceScriptDatabaseInitializer}. @@ -29,9 +30,16 @@ */ class DataSourceScriptDatabaseInitializerDetector extends AbstractBeansOfTypeDatabaseInitializerDetector { + static final int PRECEDENCE = Ordered.LOWEST_PRECEDENCE - 100; + @Override protected Set> getDatabaseInitializerBeanTypes() { return Collections.singleton(DataSourceScriptDatabaseInitializer.class); } + @Override + public int getOrder() { + return PRECEDENCE; + } + } diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-flyway/src/main/resources/data.sql b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-flyway/src/main/resources/data.sql new file mode 100644 index 000000000000..26b060694469 --- /dev/null +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-flyway/src/main/resources/data.sql @@ -0,0 +1 @@ +insert into PERSON (first_name, last_name) values ('Phillip', 'Webb'); diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-flyway/src/test/java/smoketest/flyway/SampleFlywayApplicationTests.java b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-flyway/src/test/java/smoketest/flyway/SampleFlywayApplicationTests.java index 6ee2598c6cc7..ddee31193009 100644 --- a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-flyway/src/test/java/smoketest/flyway/SampleFlywayApplicationTests.java +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-flyway/src/test/java/smoketest/flyway/SampleFlywayApplicationTests.java @@ -32,7 +32,7 @@ class SampleFlywayApplicationTests { @Test void testDefaultSettings() { - assertThat(this.template.queryForObject("SELECT COUNT(*) from PERSON", Integer.class)).isEqualTo(1); + assertThat(this.template.queryForObject("SELECT COUNT(*) from PERSON", Integer.class)).isEqualTo(2); } }