Skip to content

Commit

Permalink
Don't duplicate Postgres databases
Browse files Browse the repository at this point in the history
PgVector is an extension for Postgres. Before this commit, we would add
a pgvector and a postgres Docker Compose service. The resulting app
doesn't start up, as there are now two JdbcConnectionDetails in the
context.

After that commit, we only add the pgvector Docker Compose service if
both Postgres and PgVector is selected.

Closes spring-iogh-1458
  • Loading branch information
mhalbritter authored and odrotbohm committed May 27, 2024
1 parent 545063a commit 8be3c6e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
* Configuration for generation of projects that depend on PgVector.
*
* @author Eddú Meléndez
* @author Moritz Halbritter
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnRequestedDependency("spring-ai-vectordb-pgvector")
Expand Down Expand Up @@ -71,13 +72,15 @@ ServiceConnectionsCustomizer pgvectorServiceConnectionsCustomizer(DockerServiceR
@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"))));
return (composeFile) -> serviceResolver.doWith("pgvector", (service) -> {
composeFile.services().remove("postgres");
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")));
});
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* 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.
Expand Down Expand Up @@ -47,12 +47,16 @@ ServiceConnectionsCustomizer postgresqlServiceConnectionsCustomizer(DockerServic
@Bean
@ConditionalOnRequestedDependency("docker-compose")
ComposeFileCustomizer postgresqlComposeFileCustomizer(DockerServiceResolver serviceResolver) {
return (composeFile) -> serviceResolver.doWith("postgres",
(service) -> composeFile.services()
.add("postgres",
service.andThen((builder) -> builder.environment("POSTGRES_USER", "myuser")
.environment("POSTGRES_DB", "mydatabase")
.environment("POSTGRES_PASSWORD", "secret"))));
return (composeFile) -> serviceResolver.doWith("postgres", (service) -> {
if (composeFile.services().has("pgvector")) {
return;
}
composeFile.services()
.add("postgres",
service.andThen((builder) -> builder.environment("POSTGRES_USER", "myuser")
.environment("POSTGRES_DB", "mydatabase")
.environment("POSTGRES_PASSWORD", "secret")));
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* Tests for {@link PgVectorProjectGenerationConfiguration}.
*
* @author Eddú Meléndez
* @author Moritz Halbritter
*/
class PgVectorProjectGenerationConfigurationTests extends AbstractExtensionTests {

Expand All @@ -45,4 +46,10 @@ void createsPostgresService() {
assertThat(composeFile(request)).hasSameContentAs(new ClassPathResource("compose/pgvector.yaml"));
}

@Test
void shouldOnlyHavePgVectorIfPostgresAndPgVectorIsSelected() {
ProjectRequest request = createProjectRequest("docker-compose", "postgresql", "spring-ai-vectordb-pgvector");
assertThat(composeFile(request)).hasSameContentAs(new ClassPathResource("compose/pgvector.yaml"));
}

}

0 comments on commit 8be3c6e

Please sign in to comment.