Skip to content

Commit c1dc8a5

Browse files
committed
Improve JdbcChatMemoryRepositorySchemaInitializer
Make it extending from `PropertiesBasedDataSourceScriptDatabaseInitializer` which introduced by Spring Boot 4.0 to eliminate boilerplate codes. Signed-off-by: Yanming Zhou <[email protected]>
1 parent a7b837a commit c1dc8a5

File tree

3 files changed

+15
-79
lines changed

3 files changed

+15
-79
lines changed

auto-configurations/models/chat/memory/repository/spring-ai-autoconfigure-model-chat-memory-repository-jdbc/src/main/java/org/springframework/ai/model/chat/memory/repository/jdbc/autoconfigure/JdbcChatMemoryRepositoryProperties.java

Lines changed: 5 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package org.springframework.ai.model.chat.memory.repository.jdbc.autoconfigure;
1818

1919
import org.springframework.boot.context.properties.ConfigurationProperties;
20-
import org.springframework.boot.sql.init.DatabaseInitializationMode;
20+
import org.springframework.boot.jdbc.init.DatabaseInitializationProperties;
2121

2222
/**
2323
* @author Jonathan Leijendekker
@@ -26,52 +26,15 @@
2626
* @since 1.0.0
2727
*/
2828
@ConfigurationProperties(JdbcChatMemoryRepositoryProperties.CONFIG_PREFIX)
29-
public class JdbcChatMemoryRepositoryProperties {
29+
public class JdbcChatMemoryRepositoryProperties extends DatabaseInitializationProperties {
3030

3131
public static final String CONFIG_PREFIX = "spring.ai.chat.memory.repository.jdbc";
3232

3333
private static final String DEFAULT_SCHEMA_LOCATION = "classpath:org/springframework/ai/chat/memory/repository/jdbc/schema-@@platform@@.sql";
3434

35-
/**
36-
* Whether to initialize the schema on startup. Values: embedded, always, never.
37-
* Default is embedded.
38-
*/
39-
private DatabaseInitializationMode initializeSchema = DatabaseInitializationMode.EMBEDDED;
40-
41-
/**
42-
* Locations of schema (DDL) scripts. Supports comma-separated list. Default is
43-
* classpath:org/springframework/ai/chat/memory/repository/jdbc/schema-@@platform@@.sql
44-
*/
45-
private String schema = DEFAULT_SCHEMA_LOCATION;
46-
47-
/**
48-
* Platform to use in initialization scripts if the @@platform@@ placeholder is used.
49-
* Auto-detected by default.
50-
*/
51-
private String platform;
52-
53-
public DatabaseInitializationMode getInitializeSchema() {
54-
return this.initializeSchema;
55-
}
56-
57-
public void setInitializeSchema(DatabaseInitializationMode initializeSchema) {
58-
this.initializeSchema = initializeSchema;
59-
}
60-
61-
public String getPlatform() {
62-
return this.platform;
63-
}
64-
65-
public void setPlatform(String platform) {
66-
this.platform = platform;
67-
}
68-
69-
public String getSchema() {
70-
return this.schema;
71-
}
72-
73-
public void setSchema(String schema) {
74-
this.schema = schema;
35+
@Override
36+
public String getDefaultSchemaLocation() {
37+
return DEFAULT_SCHEMA_LOCATION;
7538
}
7639

7740
}

auto-configurations/models/chat/memory/repository/spring-ai-autoconfigure-model-chat-memory-repository-jdbc/src/main/java/org/springframework/ai/model/chat/memory/repository/jdbc/autoconfigure/JdbcChatMemoryRepositorySchemaInitializer.java

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,9 @@
1616

1717
package org.springframework.ai.model.chat.memory.repository.jdbc.autoconfigure;
1818

19-
import java.util.List;
20-
2119
import javax.sql.DataSource;
2220

23-
import org.springframework.boot.jdbc.init.DataSourceScriptDatabaseInitializer;
24-
import org.springframework.boot.jdbc.init.PlatformPlaceholderDatabaseDriverResolver;
25-
import org.springframework.boot.sql.init.DatabaseInitializationSettings;
26-
import org.springframework.util.StringUtils;
21+
import org.springframework.boot.jdbc.init.PropertiesBasedDataSourceScriptDatabaseInitializer;
2722

2823
/**
2924
* Performs database initialization for the JDBC Chat Memory Repository.
@@ -32,28 +27,11 @@
3227
* @author Yanming Zhou
3328
* @since 1.0.0
3429
*/
35-
class JdbcChatMemoryRepositorySchemaInitializer extends DataSourceScriptDatabaseInitializer {
30+
class JdbcChatMemoryRepositorySchemaInitializer
31+
extends PropertiesBasedDataSourceScriptDatabaseInitializer<JdbcChatMemoryRepositoryProperties> {
3632

3733
JdbcChatMemoryRepositorySchemaInitializer(DataSource dataSource, JdbcChatMemoryRepositoryProperties properties) {
38-
super(dataSource, getSettings(dataSource, properties));
39-
}
40-
41-
static DatabaseInitializationSettings getSettings(DataSource dataSource,
42-
JdbcChatMemoryRepositoryProperties properties) {
43-
var settings = new DatabaseInitializationSettings();
44-
settings.setSchemaLocations(resolveSchemaLocations(dataSource, properties));
45-
settings.setMode(properties.getInitializeSchema());
46-
settings.setContinueOnError(true);
47-
return settings;
48-
}
49-
50-
private static List<String> resolveSchemaLocations(DataSource dataSource,
51-
JdbcChatMemoryRepositoryProperties properties) {
52-
PlatformPlaceholderDatabaseDriverResolver platformResolver = new PlatformPlaceholderDatabaseDriverResolver();
53-
if (StringUtils.hasText(properties.getPlatform())) {
54-
return platformResolver.resolveAll(properties.getPlatform(), properties.getSchema());
55-
}
56-
return platformResolver.resolveAll(dataSource, properties.getSchema());
34+
super(dataSource, properties);
5735
}
5836

5937
}

auto-configurations/models/chat/memory/repository/spring-ai-autoconfigure-model-chat-memory-repository-jdbc/src/test/java/org/springframework/ai/model/chat/memory/repository/jdbc/autoconfigure/JdbcChatMemoryRepositorySchemaInitializerPostgresqlTests.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616

1717
package org.springframework.ai.model.chat.memory.repository.jdbc.autoconfigure;
1818

19-
import javax.sql.DataSource;
20-
19+
import org.assertj.core.api.InstanceOfAssertFactories;
2120
import org.junit.jupiter.api.Test;
2221
import org.testcontainers.containers.PostgreSQLContainer;
2322
import org.testcontainers.junit.jupiter.Container;
@@ -33,6 +32,7 @@
3332

3433
/**
3534
* @author Jonathan Leijendekker
35+
* @author Yanming Zhou
3636
*/
3737
@Testcontainers
3838
class JdbcChatMemoryRepositorySchemaInitializerPostgresqlTests {
@@ -55,15 +55,10 @@ class JdbcChatMemoryRepositorySchemaInitializerPostgresqlTests {
5555

5656
@Test
5757
void getSettings_shouldHaveSchemaLocations() {
58-
this.contextRunner.run(context -> {
59-
var dataSource = context.getBean(DataSource.class);
60-
// Use new signature: requires JdbcChatMemoryRepositoryProperties
61-
var settings = JdbcChatMemoryRepositorySchemaInitializer.getSettings(dataSource,
62-
new JdbcChatMemoryRepositoryProperties());
63-
64-
assertThat(settings.getSchemaLocations())
65-
.containsOnly("classpath:org/springframework/ai/chat/memory/repository/jdbc/schema-postgresql.sql");
66-
});
58+
this.contextRunner.run(context -> assertThat(context.getBean(JdbcChatMemoryRepositorySchemaInitializer.class))
59+
.extracting("settings.schemaLocations")
60+
.asInstanceOf(InstanceOfAssertFactories.LIST)
61+
.containsOnly("classpath:org/springframework/ai/chat/memory/repository/jdbc/schema-postgresql.sql"));
6762
}
6863

6964
}

0 commit comments

Comments
 (0)