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 5da4c17336c080f71d35ccd544bbf14eb79ea434 Author: Rene Cordier <[email protected]> AuthorDate: Tue Nov 21 16:15:35 2023 +0700 JAMES-2586 DomainList Should throw when insert duplicate or delete not found domain --- .../domainlist/postgres/PostgresDomainList.java | 35 ++++++++++++++-------- .../domainlist/postgres/PostgresDomainModule.java | 2 +- 2 files changed, 23 insertions(+), 14 deletions(-) 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 6074b6babc..f4bfd90cee 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,6 +19,8 @@ package org.apache.james.domainlist.postgres; +import static org.apache.james.domainlist.postgres.PostgresDomainModule.PostgresDomainTable.DOMAIN; + import java.util.List; import java.util.Optional; @@ -46,34 +48,41 @@ public class PostgresDomainList extends AbstractDomainList { @Override public void addDomain(Domain domain) throws DomainListException { - postgresExecutor.executeVoid(dslContext -> Mono.from(dslContext.insertInto(PostgresDomainModule.PostgresDomainTable.TABLE_NAME, PostgresDomainModule.PostgresDomainTable.DOMAIN) - .values(domain.asString()))) - .onErrorMap(DataAccessException.class, e -> new DomainListException(domain.name() + " already exists.")) - .block(); - + try { + postgresExecutor.executeVoid(dslContext -> Mono.from(dslContext.insertInto(PostgresDomainModule.PostgresDomainTable.TABLE_NAME, DOMAIN) + .values(domain.asString()))) + .block(); + } catch (DataAccessException exception) { + throw new DomainListException(domain.name() + " already exists."); + } } @Override - protected List<Domain> getDomainListInternal() throws DomainListException { + protected List<Domain> getDomainListInternal() { return postgresExecutor.executeRows(dsl -> Flux.from(dsl.selectFrom(PostgresDomainModule.PostgresDomainTable.TABLE_NAME))) - .map(record -> Domain.of(record.get(PostgresDomainModule.PostgresDomainTable.DOMAIN))) + .map(record -> Domain.of(record.get(DOMAIN))) .collectList() .block(); } @Override - protected boolean containsDomainInternal(Domain domain) throws DomainListException { + protected boolean containsDomainInternal(Domain domain) { return postgresExecutor.executeRow(dsl -> Mono.from(dsl.selectFrom(PostgresDomainModule.PostgresDomainTable.TABLE_NAME) - .where(PostgresDomainModule.PostgresDomainTable.DOMAIN.eq(domain.asString())))) + .where(DOMAIN.eq(domain.asString())))) .blockOptional() .isPresent(); } @Override protected void doRemoveDomain(Domain domain) throws DomainListException { - postgresExecutor.executeVoid(dslContext -> Mono.from(dslContext.deleteFrom(PostgresDomainModule.PostgresDomainTable.TABLE_NAME) - .where(PostgresDomainModule.PostgresDomainTable.DOMAIN.eq(domain.asString())))) - .onErrorMap(DataAccessException.class, e -> new DomainListException(domain.name() + " was not found")) - .block(); + boolean executed = postgresExecutor.executeRow(dslContext -> Mono.from(dslContext.deleteFrom(PostgresDomainModule.PostgresDomainTable.TABLE_NAME) + .where(DOMAIN.eq(domain.asString())) + .returning(DOMAIN))) + .blockOptional() + .isPresent(); + + if (!executed) { + throw new DomainListException(domain.name() + " was not found"); + } } } diff --git a/server/data/data-postgres/src/main/java/org/apache/james/domainlist/postgres/PostgresDomainModule.java b/server/data/data-postgres/src/main/java/org/apache/james/domainlist/postgres/PostgresDomainModule.java index aa80839f9f..1d9fd110d0 100644 --- a/server/data/data-postgres/src/main/java/org/apache/james/domainlist/postgres/PostgresDomainModule.java +++ b/server/data/data-postgres/src/main/java/org/apache/james/domainlist/postgres/PostgresDomainModule.java @@ -36,7 +36,7 @@ public interface PostgresDomainModule { PostgresTable TABLE = PostgresTable.name(TABLE_NAME.getName()) .createTableStep(((dsl, tableName) -> dsl.createTableIfNotExists(tableName) .column(DOMAIN) - .constraint(DSL.primaryKey(DOMAIN)))) + .primaryKey(DOMAIN))) .disableRowLevelSecurity(); } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
