-
Notifications
You must be signed in to change notification settings - Fork 899
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate test application with pgvector and spring-ai if pgvector is …
…selected See gh-1420
- Loading branch information
1 parent
9fde8e4
commit 852ffbd
Showing
7 changed files
with
162 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...ng/start/site/extension/dependency/postgresql/PgVectorProjectGenerationConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright 2012-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.spring.start.site.extension.dependency.postgresql; | ||
|
||
import java.util.Map; | ||
|
||
import io.spring.initializr.generator.condition.ConditionalOnRequestedDependency; | ||
import io.spring.initializr.generator.project.ProjectDescription; | ||
import io.spring.initializr.generator.version.Version; | ||
import io.spring.initializr.generator.version.VersionParser; | ||
import io.spring.initializr.generator.version.VersionRange; | ||
import io.spring.initializr.versionresolver.MavenVersionResolver; | ||
import io.spring.start.site.container.ComposeFileCustomizer; | ||
import io.spring.start.site.container.DockerServiceResolver; | ||
import io.spring.start.site.container.ServiceConnections.ServiceConnection; | ||
import io.spring.start.site.container.ServiceConnectionsCustomizer; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
/** | ||
* Configuration for generation of projects that depend on PgVector. | ||
* | ||
* @author Eddú Meléndez | ||
*/ | ||
@Configuration(proxyBeanMethods = false) | ||
@ConditionalOnRequestedDependency("spring-ai-vectordb-pgvector") | ||
class PgVectorProjectGenerationConfiguration { | ||
|
||
private static final String TESTCONTAINERS_CLASS_NAME = "org.testcontainers.containers.PostgreSQLContainer"; | ||
|
||
private static final VersionRange TESTCONTAINERS_1_19_7_OR_LATER = VersionParser.DEFAULT.parseRange("1.19.7"); | ||
|
||
private final MavenVersionResolver versionResolver; | ||
|
||
private final ProjectDescription description; | ||
|
||
PgVectorProjectGenerationConfiguration(MavenVersionResolver versionResolver, ProjectDescription description) { | ||
this.versionResolver = versionResolver; | ||
this.description = description; | ||
} | ||
|
||
@Bean | ||
@ConditionalOnRequestedDependency("testcontainers") | ||
ServiceConnectionsCustomizer pgvectorServiceConnectionsCustomizer(DockerServiceResolver serviceResolver) { | ||
Map<String, String> resolve = this.versionResolver.resolveDependencies("org.springframework.boot", | ||
"spring-boot-dependencies", this.description.getPlatformVersion().toString()); | ||
String testcontainersVersion = resolve.get("org.testcontainers:testcontainers"); | ||
|
||
return (serviceConnections) -> { | ||
if (TESTCONTAINERS_1_19_7_OR_LATER.match(Version.parse(testcontainersVersion))) { | ||
serviceResolver.doWith("pgvector", (service) -> serviceConnections.addServiceConnection( | ||
ServiceConnection.ofContainer("pgvector", service, TESTCONTAINERS_CLASS_NAME))); | ||
} | ||
}; | ||
} | ||
|
||
@Bean | ||
@ConditionalOnRequestedDependency("docker-compose") | ||
ComposeFileCustomizer pgvectorComposeFileCustomizer(DockerServiceResolver serviceResolver) { | ||
return (composeFile) -> serviceResolver.doWith("pgvector", | ||
(service) -> composeFile.services() | ||
.add("pgvector", | ||
service.andThen((builder) -> builder.environment("POSTGRES_USER", "myuser") | ||
.environment("POSTGRES_DB", "mydatabase") | ||
.environment("POSTGRES_PASSWORD", "secret") | ||
.label("org.springframework.boot.service-connection", "postgres")))); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...art/site/extension/dependency/postgresql/PgVectorProjectGenerationConfigurationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright 2012-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.spring.start.site.extension.dependency.postgresql; | ||
|
||
import io.spring.initializr.generator.test.project.ProjectStructure; | ||
import io.spring.initializr.web.project.ProjectRequest; | ||
import io.spring.start.site.extension.AbstractExtensionTests; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import org.springframework.core.io.ClassPathResource; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Tests for {@link PgVectorProjectGenerationConfiguration}. | ||
* | ||
* @author Eddú Meléndez | ||
*/ | ||
class PgVectorProjectGenerationConfigurationTests extends AbstractExtensionTests { | ||
|
||
@Test | ||
void doesNothingWithoutDockerCompose() { | ||
ProjectRequest request = createProjectRequest("web", "spring-ai-vectordb-pgvector"); | ||
ProjectStructure structure = generateProject(request); | ||
assertThat(structure.getProjectDirectory().resolve("compose.yaml")).doesNotExist(); | ||
} | ||
|
||
@Test | ||
void createsPostgresService() { | ||
ProjectRequest request = createProjectRequest("docker-compose", "spring-ai-vectordb-pgvector"); | ||
assertThat(composeFile(request)).hasSameContentAs(new ClassPathResource("compose/pgvector.yaml")); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
services: | ||
pgvector: | ||
image: 'pgvector/pgvector:pg16' | ||
environment: | ||
- 'POSTGRES_DB=mydatabase' | ||
- 'POSTGRES_PASSWORD=secret' | ||
- 'POSTGRES_USER=myuser' | ||
labels: | ||
- "org.springframework.boot.service-connection=postgres" | ||
ports: | ||
- '5432' |