Skip to content

Commit 84274a2

Browse files
committed
Polish 'Introduce a shared abstraction for database initialization'
See gh-46213
1 parent 001230c commit 84274a2

File tree

10 files changed

+46
-25
lines changed

10 files changed

+46
-25
lines changed

module/spring-boot-batch-jdbc/src/main/java/org/springframework/boot/batch/jdbc/autoconfigure/BatchDataSourceScriptDatabaseInitializer.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import org.springframework.boot.jdbc.init.DataSourceScriptDatabaseInitializer;
2222
import org.springframework.boot.jdbc.init.PropertiesBasedDataSourceScriptDatabaseInitializer;
23+
import org.springframework.boot.sql.init.DatabaseInitializationSettings;
2324

2425
/**
2526
* {@link DataSourceScriptDatabaseInitializer} for the Spring Batch database. May be
@@ -44,4 +45,9 @@ public BatchDataSourceScriptDatabaseInitializer(DataSource dataSource, BatchJdbc
4445
super(dataSource, properties);
4546
}
4647

48+
@Override
49+
protected DatabaseInitializationSettings getSettings() {
50+
return super.getSettings();
51+
}
52+
4753
}

module/spring-boot-batch-jdbc/src/test/java/org/springframework/boot/batch/jdbc/autoconfigure/BatchDataSourceScriptDatabaseInitializerTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ void getSettingsWithPlatformDoesNotTouchDataSource() {
5454
DataSource dataSource = mock(DataSource.class);
5555
BatchJdbcProperties properties = new BatchJdbcProperties();
5656
properties.setPlatform("test");
57-
DatabaseInitializationSettings settings = BatchDataSourceScriptDatabaseInitializer.getSettings(dataSource,
58-
properties);
57+
DatabaseInitializationSettings settings = new BatchDataSourceScriptDatabaseInitializer(dataSource, properties)
58+
.getSettings();
5959
assertThat(settings.getSchemaLocations())
6060
.containsOnly("classpath:org/springframework/batch/core/schema-test.sql");
6161
then(dataSource).shouldHaveNoInteractions();
@@ -74,8 +74,8 @@ void batchSchemaCanBeLocated(DatabaseDriver driver) throws SQLException {
7474
given(connection.getMetaData()).willReturn(metadata);
7575
String productName = (String) ReflectionTestUtils.getField(driver, "productName");
7676
given(metadata.getDatabaseProductName()).willReturn(productName);
77-
DatabaseInitializationSettings settings = BatchDataSourceScriptDatabaseInitializer.getSettings(dataSource,
78-
properties);
77+
DatabaseInitializationSettings settings = new BatchDataSourceScriptDatabaseInitializer(dataSource, properties)
78+
.getSettings();
7979
List<String> schemaLocations = settings.getSchemaLocations();
8080
assertThat(schemaLocations).isNotEmpty()
8181
.allSatisfy((location) -> assertThat(resourceLoader.getResource(location).exists()).isTrue());

module/spring-boot-integration/src/main/java/org/springframework/boot/integration/autoconfigure/IntegrationDataSourceScriptDatabaseInitializer.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.boot.jdbc.DatabaseDriver;
2424
import org.springframework.boot.jdbc.init.DataSourceScriptDatabaseInitializer;
2525
import org.springframework.boot.jdbc.init.PropertiesBasedDataSourceScriptDatabaseInitializer;
26+
import org.springframework.boot.sql.init.DatabaseInitializationSettings;
2627

2728
/**
2829
* {@link DataSourceScriptDatabaseInitializer} for the Spring Integration database. May be
@@ -46,4 +47,9 @@ public IntegrationDataSourceScriptDatabaseInitializer(DataSource dataSource, Int
4647
super(dataSource, properties, Map.of(DatabaseDriver.MARIADB, "mysql"));
4748
}
4849

50+
@Override
51+
protected DatabaseInitializationSettings getSettings() {
52+
return super.getSettings();
53+
}
54+
4955
}

module/spring-boot-integration/src/test/java/org/springframework/boot/integration/autoconfigure/IntegrationDataSourceScriptDatabaseInitializerTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ void getSettingsWithPlatformDoesNotTouchDataSource() {
3838
DataSource dataSource = mock(DataSource.class);
3939
IntegrationJdbcProperties properties = new IntegrationJdbcProperties();
4040
properties.setPlatform("test");
41-
DatabaseInitializationSettings settings = IntegrationDataSourceScriptDatabaseInitializer.getSettings(dataSource,
42-
properties);
41+
DatabaseInitializationSettings settings = new IntegrationDataSourceScriptDatabaseInitializer(dataSource,
42+
properties)
43+
.getSettings();
4344
assertThat(settings.getSchemaLocations())
4445
.containsOnly("classpath:org/springframework/integration/jdbc/schema-test.sql");
4546
then(dataSource).shouldHaveNoInteractions();

module/spring-boot-jdbc/src/main/java/org/springframework/boot/jdbc/init/PropertiesBasedDataSourceScriptDatabaseInitializer.java

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,6 @@ public PropertiesBasedDataSourceScriptDatabaseInitializer(DataSource dataSource,
5959
super(dataSource, getSettings(dataSource, properties, driverMappings));
6060
}
6161

62-
/**
63-
* Adapts {@link DatabaseInitializationProperties configuration properties} to
64-
* {@link DatabaseInitializationSettings} replacing any {@literal @@platform@@}
65-
* placeholders.
66-
* @param dataSource the data source
67-
* @param properties the configuration properties
68-
* @param <T> the {@link DatabaseInitializationProperties} type being used
69-
* @return a new {@link DatabaseInitializationSettings} instance
70-
*/
71-
public static <T extends DatabaseInitializationProperties> DatabaseInitializationSettings getSettings(
72-
DataSource dataSource, T properties) {
73-
return getSettings(dataSource, properties, Collections.emptyMap());
74-
}
75-
7662
/**
7763
* Adapts {@link DatabaseInitializationProperties configuration properties} to
7864
* {@link DatabaseInitializationSettings} replacing any {@literal @@platform@@}
@@ -83,7 +69,7 @@ public static <T extends DatabaseInitializationProperties> DatabaseInitializatio
8369
* @param <T> the {@link DatabaseInitializationProperties} type being used
8470
* @return a new {@link DatabaseInitializationSettings} instance
8571
*/
86-
public static <T extends DatabaseInitializationProperties> DatabaseInitializationSettings getSettings(
72+
private static <T extends DatabaseInitializationProperties> DatabaseInitializationSettings getSettings(
8773
DataSource dataSource, T properties, Map<DatabaseDriver, String> driverMappings) {
8874
DatabaseInitializationSettings settings = new DatabaseInitializationSettings();
8975
settings.setSchemaLocations(resolveSchemaLocations(dataSource, properties, driverMappings));

module/spring-boot-quartz/src/main/java/org/springframework/boot/quartz/autoconfigure/QuartzDataSourceScriptDatabaseInitializer.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.boot.jdbc.DatabaseDriver;
2727
import org.springframework.boot.jdbc.init.DataSourceScriptDatabaseInitializer;
2828
import org.springframework.boot.jdbc.init.PropertiesBasedDataSourceScriptDatabaseInitializer;
29+
import org.springframework.boot.sql.init.DatabaseInitializationSettings;
2930
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
3031
import org.springframework.util.ObjectUtils;
3132

@@ -57,6 +58,11 @@ public QuartzDataSourceScriptDatabaseInitializer(DataSource dataSource, QuartzJd
5758
this.commentPrefixes = properties.getCommentPrefix();
5859
}
5960

61+
@Override
62+
protected DatabaseInitializationSettings getSettings() {
63+
return super.getSettings();
64+
}
65+
6066
@Override
6167
protected void customize(ResourceDatabasePopulator populator) {
6268
if (!ObjectUtils.isEmpty(this.commentPrefixes)) {

module/spring-boot-quartz/src/test/java/org/springframework/boot/quartz/autoconfigure/QuartzDataSourceScriptDatabaseInitializerTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ void getSettingsWithPlatformDoesNotTouchDataSource() {
4141
DataSource dataSource = mock(DataSource.class);
4242
QuartzJdbcProperties properties = new QuartzJdbcProperties();
4343
properties.setPlatform("test");
44-
DatabaseInitializationSettings settings = QuartzDataSourceScriptDatabaseInitializer.getSettings(dataSource,
45-
properties);
44+
DatabaseInitializationSettings settings = new QuartzDataSourceScriptDatabaseInitializer(dataSource, properties)
45+
.getSettings();
4646
assertThat(settings.getSchemaLocations())
4747
.containsOnly("classpath:org/quartz/impl/jdbcjobstore/tables_test.sql");
4848
then(dataSource).shouldHaveNoInteractions();

module/spring-boot-session-jdbc/src/main/java/org/springframework/boot/session/jdbc/autoconfigure/JdbcSessionDataSourceScriptDatabaseInitializer.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.boot.jdbc.DatabaseDriver;
2424
import org.springframework.boot.jdbc.init.DataSourceScriptDatabaseInitializer;
2525
import org.springframework.boot.jdbc.init.PropertiesBasedDataSourceScriptDatabaseInitializer;
26+
import org.springframework.boot.sql.init.DatabaseInitializationSettings;
2627

2728
/**
2829
* {@link DataSourceScriptDatabaseInitializer} for the Spring Session JDBC database. May
@@ -48,4 +49,9 @@ public JdbcSessionDataSourceScriptDatabaseInitializer(DataSource dataSource, Jdb
4849
super(dataSource, properties, Map.of(DatabaseDriver.MARIADB, "mysql"));
4950
}
5051

52+
@Override
53+
protected DatabaseInitializationSettings getSettings() {
54+
return super.getSettings();
55+
}
56+
5157
}

module/spring-boot-session-jdbc/src/test/java/org/springframework/boot/session/jdbc/autoconfigure/JdbcSessionDataSourceScriptDatabaseInitializerTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ void getSettingsWithPlatformDoesNotTouchDataSource() {
3838
DataSource dataSource = mock(DataSource.class);
3939
JdbcSessionProperties properties = new JdbcSessionProperties();
4040
properties.setPlatform("test");
41-
DatabaseInitializationSettings settings = JdbcSessionDataSourceScriptDatabaseInitializer.getSettings(dataSource,
42-
properties);
41+
DatabaseInitializationSettings settings = new JdbcSessionDataSourceScriptDatabaseInitializer(dataSource,
42+
properties)
43+
.getSettings();
4344
assertThat(settings.getSchemaLocations())
4445
.containsOnly("classpath:org/springframework/session/jdbc/schema-test.sql");
4546
then(dataSource).shouldHaveNoInteractions();

module/spring-boot-sql/src/main/java/org/springframework/boot/sql/init/AbstractScriptDatabaseInitializer.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ protected AbstractScriptDatabaseInitializer(DatabaseInitializationSettings setti
5959
this.settings = settings;
6060
}
6161

62+
/**
63+
* Return the {@link DatabaseInitializationSettings} being used by the initializer.
64+
* @return the settings being used
65+
* @since 4.0.0
66+
*/
67+
protected DatabaseInitializationSettings getSettings() {
68+
return this.settings;
69+
}
70+
6271
@Override
6372
public void setResourceLoader(ResourceLoader resourceLoader) {
6473
this.resourceLoader = resourceLoader;

0 commit comments

Comments
 (0)