Skip to content

Commit 506cfa7

Browse files
committed
Introduce H2ChatMemoryRepositoryDialect
It seems that H2 is more populate than HSQLDB. Signed-off-by: Yanming Zhou <[email protected]>
1 parent b219c21 commit 506cfa7

File tree

5 files changed

+90
-3
lines changed

5 files changed

+90
-3
lines changed

memory/repository/spring-ai-model-chat-memory-repository-jdbc/pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,14 @@
7777
</dependency>
7878

7979
<!-- TESTING -->
80+
81+
<dependency>
82+
<groupId>com.h2database</groupId>
83+
<artifactId>h2</artifactId>
84+
<scope>test</scope>
85+
<optional>true</optional>
86+
</dependency>
87+
8088
<dependency>
8189
<groupId>org.springframework.boot</groupId>
8290
<artifactId>spring-boot-starter-test</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2024-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.ai.chat.memory.repository.jdbc;
18+
19+
/**
20+
* H2-specific SQL dialect for chat memory repository.
21+
* <p>
22+
* Currently it's compatible with HSQLDB, we could override methods for H2-specific syntax
23+
* in the future.
24+
*
25+
* @author Yanming Zhou
26+
*/
27+
public class H2ChatMemoryRepositoryDialect extends HsqldbChatMemoryRepositoryDialect {
28+
29+
}

memory/repository/spring-ai-model-chat-memory-repository-jdbc/src/main/java/org/springframework/ai/chat/memory/repository/jdbc/JdbcChatMemoryRepositoryDialect.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,14 @@ static JdbcChatMemoryRepositoryDialect from(DataSource dataSource) {
5757
String url = dataSource.getConnection().getMetaData().getURL().toLowerCase();
5858
if (url.contains("postgresql"))
5959
return new PostgresChatMemoryRepositoryDialect();
60-
if (url.contains("mysql"))
61-
return new MysqlChatMemoryRepositoryDialect();
62-
if (url.contains("mariadb"))
60+
if (url.contains("mysql") || url.contains("mariadb"))
6361
return new MysqlChatMemoryRepositoryDialect();
6462
if (url.contains("sqlserver"))
6563
return new SqlServerChatMemoryRepositoryDialect();
6664
if (url.contains("hsqldb"))
6765
return new HsqldbChatMemoryRepositoryDialect();
66+
if (url.contains("h2"))
67+
return new H2ChatMemoryRepositoryDialect();
6868
// Add more as needed
6969
}
7070
catch (Exception ignored) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CREATE TABLE SPRING_AI_CHAT_MEMORY (
2+
conversation_id VARCHAR(36) NOT NULL,
3+
content LONGVARCHAR NOT NULL,
4+
type VARCHAR(10) NOT NULL,
5+
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
6+
);
7+
8+
CREATE INDEX SPRING_AI_CHAT_MEMORY_CONVERSATION_ID_TIMESTAMP_IDX ON SPRING_AI_CHAT_MEMORY(conversation_id, timestamp DESC);
9+
10+
ALTER TABLE SPRING_AI_CHAT_MEMORY ADD CONSTRAINT TYPE_CHECK CHECK (type IN ('USER', 'ASSISTANT', 'SYSTEM', 'TOOL'));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2023-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.ai.chat.memory.repository.jdbc;
18+
19+
import org.springframework.boot.SpringBootConfiguration;
20+
import org.springframework.boot.test.context.SpringBootTest;
21+
import org.springframework.test.context.TestPropertySource;
22+
import org.springframework.test.context.jdbc.Sql;
23+
24+
/**
25+
* Integration tests for {@link JdbcChatMemoryRepository} with H2.
26+
*
27+
* @author Yanming Zhou
28+
*/
29+
@SpringBootTest(classes = JdbcChatMemoryRepositoryH2IT.TestConfiguration.class)
30+
@TestPropertySource(properties = { "spring.datasource.url=jdbc:h2:mem:mydb" })
31+
@Sql(scripts = "classpath:org/springframework/ai/chat/memory/repository/jdbc/schema-h2.sql",
32+
executionPhase = Sql.ExecutionPhase.BEFORE_TEST_CLASS)
33+
class JdbcChatMemoryRepositoryH2IT extends AbstractJdbcChatMemoryRepositoryIT {
34+
35+
@SpringBootConfiguration
36+
static class TestConfiguration extends BaseTestConfiguration {
37+
38+
}
39+
40+
}

0 commit comments

Comments
 (0)