This is an automated email from the ASF dual-hosted git repository. rcordier pushed a commit to branch postgresql in repository https://gitbox.apache.org/repos/asf/james-project.git
commit 09dacea43bf52135858586d8007ed47d097d9338 Author: Rene Cordier <[email protected]> AuthorDate: Tue Nov 28 16:12:22 2023 +0700 JAMES-2586 Fix Guice bindings between PostgresDomainList and PostgresTableManager Need to initialize the postgresql db and tables before returning the default PostgresExecutor to not have the domain list configuration being played before the domains table exists. --- .../backends/postgres/PostgresTableManager.java | 24 +++++++++++++++------- .../james/modules/data/PostgresCommonModule.java | 23 ++++++--------------- .../domainlist/postgres/PostgresDomainList.java | 8 ++++---- .../postgres/PostgresDomainListTest.java | 7 +------ 4 files changed, 28 insertions(+), 34 deletions(-) diff --git a/backends-common/postgres/src/main/java/org/apache/james/backends/postgres/PostgresTableManager.java b/backends-common/postgres/src/main/java/org/apache/james/backends/postgres/PostgresTableManager.java index a7277dc414..38e51da1b7 100644 --- a/backends-common/postgres/src/main/java/org/apache/james/backends/postgres/PostgresTableManager.java +++ b/backends-common/postgres/src/main/java/org/apache/james/backends/postgres/PostgresTableManager.java @@ -19,13 +19,10 @@ package org.apache.james.backends.postgres; -import static org.apache.james.backends.postgres.utils.PostgresExecutor.DEFAULT_INJECT; - import javax.inject.Inject; -import javax.inject.Named; +import javax.inject.Provider; import org.apache.james.backends.postgres.utils.PostgresExecutor; -import org.apache.james.lifecycle.api.Startable; import org.jooq.exception.DataAccessException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -36,19 +33,20 @@ import io.r2dbc.spi.Result; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; -public class PostgresTableManager implements Startable { +public class PostgresTableManager implements Provider<PostgresExecutor> { private static final Logger LOGGER = LoggerFactory.getLogger(PostgresTableManager.class); private final PostgresExecutor postgresExecutor; private final PostgresModule module; private final boolean rowLevelSecurityEnabled; @Inject - public PostgresTableManager(@Named(DEFAULT_INJECT) PostgresExecutor postgresExecutor, + public PostgresTableManager(PostgresExecutor.Factory factory, PostgresModule module, PostgresConfiguration postgresConfiguration) { - this.postgresExecutor = postgresExecutor; + this.postgresExecutor = factory.create(); this.module = module; this.rowLevelSecurityEnabled = postgresConfiguration.rowLevelSecurityEnabled(); + initPostgres(); } @VisibleForTesting @@ -58,6 +56,13 @@ public class PostgresTableManager implements Startable { this.rowLevelSecurityEnabled = rowLevelSecurityEnabled; } + private void initPostgres() { + initializePostgresExtension() + .then(initializeTables()) + .then(initializeTableIndexes()) + .block(); + } + public Mono<Void> initializePostgresExtension() { return postgresExecutor.connection() .flatMapMany(connection -> connection.createStatement("CREATE EXTENSION IF NOT EXISTS hstore") @@ -131,4 +136,9 @@ public class PostgresTableManager implements Startable { LOGGER.error("Error while creating index {}", index.getName(), e); return Mono.error(e); } + + @Override + public PostgresExecutor get() { + return postgresExecutor; + } } diff --git a/server/container/guice/postgres-common/src/main/java/org/apache/james/modules/data/PostgresCommonModule.java b/server/container/guice/postgres-common/src/main/java/org/apache/james/modules/data/PostgresCommonModule.java index 30dcf74a09..82366095ae 100644 --- a/server/container/guice/postgres-common/src/main/java/org/apache/james/modules/data/PostgresCommonModule.java +++ b/server/container/guice/postgres-common/src/main/java/org/apache/james/modules/data/PostgresCommonModule.java @@ -31,8 +31,6 @@ import org.apache.james.backends.postgres.utils.DomainImplPostgresConnectionFact import org.apache.james.backends.postgres.utils.JamesPostgresConnectionFactory; import org.apache.james.backends.postgres.utils.PostgresExecutor; import org.apache.james.backends.postgres.utils.SinglePostgresConnectionFactory; -import org.apache.james.utils.InitializationOperation; -import org.apache.james.utils.InitilizationOperationBuilder; import org.apache.james.utils.PropertiesProvider; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -42,7 +40,6 @@ import com.google.inject.Provides; import com.google.inject.Scopes; import com.google.inject.Singleton; import com.google.inject.multibindings.Multibinder; -import com.google.inject.multibindings.ProvidesIntoSet; import com.google.inject.name.Named; import io.r2dbc.postgresql.PostgresqlConnectionConfiguration; @@ -57,6 +54,8 @@ public class PostgresCommonModule extends AbstractModule { public void configure() { Multibinder.newSetBinder(binder(), PostgresModule.class); bind(PostgresExecutor.Factory.class).in(Scopes.SINGLETON); + + bind(PostgresExecutor.class).toProvider(PostgresTableManager.class); } @Provides @@ -98,26 +97,16 @@ public class PostgresCommonModule extends AbstractModule { @Provides @Singleton - PostgresTableManager postgresTableManager(@Named(DEFAULT_INJECT) PostgresExecutor defaultPostgresExecutor, + PostgresTableManager postgresTableManager(PostgresExecutor.Factory factory, PostgresModule postgresModule, PostgresConfiguration postgresConfiguration) { - return new PostgresTableManager(defaultPostgresExecutor, postgresModule, postgresConfiguration); + return new PostgresTableManager(factory, postgresModule, postgresConfiguration); } @Provides @Named(DEFAULT_INJECT) @Singleton - PostgresExecutor defaultPostgresExecutor(PostgresExecutor.Factory factory) { - return factory.create(); - } - - @ProvidesIntoSet - InitializationOperation provisionPostgresTablesAndIndexes(PostgresTableManager postgresTableManager) { - return InitilizationOperationBuilder - .forClass(PostgresTableManager.class) - .init(() -> postgresTableManager.initializePostgresExtension() - .then(postgresTableManager.initializeTables()) - .then(postgresTableManager.initializeTableIndexes()) - .block()); + PostgresExecutor defaultPostgresExecutor(PostgresTableManager postgresTableManager) { + return postgresTableManager.get(); } } diff --git a/server/data/data-postgres/src/main/java/org/apache/james/domainlist/postgres/PostgresDomainList.java b/server/data/data-postgres/src/main/java/org/apache/james/domainlist/postgres/PostgresDomainList.java index f4bfd90cee..9defb6ef2a 100644 --- a/server/data/data-postgres/src/main/java/org/apache/james/domainlist/postgres/PostgresDomainList.java +++ b/server/data/data-postgres/src/main/java/org/apache/james/domainlist/postgres/PostgresDomainList.java @@ -19,14 +19,14 @@ package org.apache.james.domainlist.postgres; +import static org.apache.james.backends.postgres.utils.PostgresExecutor.DEFAULT_INJECT; import static org.apache.james.domainlist.postgres.PostgresDomainModule.PostgresDomainTable.DOMAIN; import java.util.List; -import java.util.Optional; import javax.inject.Inject; +import javax.inject.Named; -import org.apache.james.backends.postgres.utils.JamesPostgresConnectionFactory; import org.apache.james.backends.postgres.utils.PostgresExecutor; import org.apache.james.core.Domain; import org.apache.james.dnsservice.api.DNSService; @@ -41,9 +41,9 @@ public class PostgresDomainList extends AbstractDomainList { private final PostgresExecutor postgresExecutor; @Inject - public PostgresDomainList(DNSService dnsService, JamesPostgresConnectionFactory postgresConnectionFactory) { + public PostgresDomainList(DNSService dnsService, @Named(DEFAULT_INJECT) PostgresExecutor postgresExecutor) { super(dnsService); - this.postgresExecutor = new PostgresExecutor(postgresConnectionFactory.getConnection(Optional.empty()));; + this.postgresExecutor = postgresExecutor; } @Override diff --git a/server/data/data-postgres/src/test/java/org/apache/james/domainlist/postgres/PostgresDomainListTest.java b/server/data/data-postgres/src/test/java/org/apache/james/domainlist/postgres/PostgresDomainListTest.java index a3b969b338..fc7ba81049 100644 --- a/server/data/data-postgres/src/test/java/org/apache/james/domainlist/postgres/PostgresDomainListTest.java +++ b/server/data/data-postgres/src/test/java/org/apache/james/domainlist/postgres/PostgresDomainListTest.java @@ -20,16 +20,12 @@ package org.apache.james.domainlist.postgres; import org.apache.james.backends.postgres.PostgresExtension; -import org.apache.james.backends.postgres.utils.SinglePostgresConnectionFactory; import org.apache.james.domainlist.api.DomainList; import org.apache.james.domainlist.lib.DomainListConfiguration; import org.apache.james.domainlist.lib.DomainListContract; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.extension.RegisterExtension; -import io.r2dbc.spi.Connection; -import reactor.core.publisher.Mono; - public class PostgresDomainListTest implements DomainListContract { @RegisterExtension static PostgresExtension postgresExtension = PostgresExtension.withoutRowLevelSecurity(PostgresDomainModule.MODULE); @@ -38,8 +34,7 @@ public class PostgresDomainListTest implements DomainListContract { @BeforeEach public void setup() throws Exception { - Connection connection = Mono.from(postgresExtension.getConnectionFactory().create()).block(); - domainList = new PostgresDomainList(getDNSServer("localhost"), new SinglePostgresConnectionFactory(connection)); + domainList = new PostgresDomainList(getDNSServer("localhost"), postgresExtension.getPostgresExecutor()); domainList.configure(DomainListConfiguration.builder() .autoDetect(false) .autoDetectIp(false) --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
