Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate test application with pgvector and spring-ai if pgvector is selected #1420

Closed
wants to merge 11 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public SimpleDockerServiceResolver() {
this.dockerServices.put("mysql", mysql());
this.dockerServices.put("oracleFree", oracleFree());
this.dockerServices.put("oracleXe", oracleXe());
this.dockerServices.put("pgvector", pgvector());
this.dockerServices.put("postgres", postgres());
this.dockerServices.put("pulsar", pulsar());
this.dockerServices.put("rabbit", rabbit());
Expand Down Expand Up @@ -105,6 +106,13 @@ private static DockerService oracleXe() {
.build();
}

private static DockerService pgvector() {
return DockerService.withImageAndTag("pgvector/pgvector:pg16")
.website("https://hub.docker.com/r/pgvector/pgvector")
.ports(5432)
.build();
}

private static DockerService postgres() {
return DockerService.withImageAndTag("postgres")
.website("https://hub.docker.com/_/postgres")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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.condition.ConditionalOnPlatformVersion;
import io.spring.initializr.generator.condition.ConditionalOnRequestedDependency;
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";

@Bean
@ConditionalOnPlatformVersion("3.3.0-M3")
@ConditionalOnRequestedDependency("testcontainers")
ServiceConnectionsCustomizer pgvectorServiceConnectionsCustomizer(DockerServiceResolver serviceResolver) {
return (serviceConnections) -> serviceResolver
.doWith("pgvector", (service) -> serviceConnections.addServiceConnection(
ServiceConnection.ofContainer("pgvector", service, TESTCONTAINERS_CLASS_NAME)));
}
eddumelendez marked this conversation as resolved.
Show resolved Hide resolved

@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"))));
}

}
1 change: 1 addition & 0 deletions start-site/src/main/resources/META-INF/spring.factories
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ io.spring.start.site.extension.dependency.mysql.MysqlProjectGenerationConfigurat
io.spring.start.site.extension.dependency.observability.ObservabilityProjectGenerationConfiguration,\
io.spring.start.site.extension.dependency.oracle.OracleProjectGenerationConfiguration,\
io.spring.start.site.extension.dependency.picocli.PicocliProjectGenerationConfiguration,\
io.spring.start.site.extension.dependency.postgresql.PgVectorProjectGenerationConfiguration,\
io.spring.start.site.extension.dependency.postgresql.PostgresqlProjectGenerationConfiguration,\
io.spring.start.site.extension.dependency.redis.RedisProjectGenerationConfiguration,\
io.spring.start.site.extension.dependency.solace.SolaceProjectGenerationConfiguration,\
Expand Down
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"));
}

}
9 changes: 9 additions & 0 deletions start-site/src/test/resources/compose/pgvector.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
services:
pgvector:
image: 'pgvector/pgvector:pg16'
environment:
- 'POSTGRES_DB=mydatabase'
- 'POSTGRES_PASSWORD=secret'
- 'POSTGRES_USER=myuser'
ports:
- '5432'
Loading