You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by bt...@apache.org on 2023/12/19 14:59:34 UTC

(james-project) branch postgresql updated: JAMES-2586 Add an Id for SieveScript (#1863)

This is an automated email from the ASF dual-hosted git repository.

btellier pushed a commit to branch postgresql
in repository https://gitbox.apache.org/repos/asf/james-project.git


The following commit(s) were added to refs/heads/postgresql by this push:
     new 23b3efa252 JAMES-2586 Add an Id for SieveScript (#1863)
23b3efa252 is described below

commit 23b3efa252812397d5ce014632adf62a12714c2f
Author: Benoit TELLIER <bt...@linagora.com>
AuthorDate: Tue Dec 19 15:59:28 2023 +0100

    JAMES-2586 Add an Id for SieveScript (#1863)
    
    While not needed now this extra field would
    significantly ease a future JMAP Sieve
    implementation and avoid a migration.
---
 .../james/sieve/postgres/PostgresSieveModule.java  |  6 +++-
 .../sieve/postgres/PostgresSieveRepository.java    |  2 ++
 .../sieve/postgres/PostgresSieveScriptDAO.java     |  4 +++
 .../sieve/postgres/model/PostgresSieveScript.java  | 18 ++++++++--
 .../postgres/model/PostgresSieveScriptId.java      | 38 ++++++++++++++++++++++
 5 files changed, 65 insertions(+), 3 deletions(-)

diff --git a/server/data/data-postgres/src/main/java/org/apache/james/sieve/postgres/PostgresSieveModule.java b/server/data/data-postgres/src/main/java/org/apache/james/sieve/postgres/PostgresSieveModule.java
index a7f7e018e5..74759c8183 100644
--- a/server/data/data-postgres/src/main/java/org/apache/james/sieve/postgres/PostgresSieveModule.java
+++ b/server/data/data-postgres/src/main/java/org/apache/james/sieve/postgres/PostgresSieveModule.java
@@ -20,6 +20,7 @@
 package org.apache.james.sieve.postgres;
 
 import java.time.OffsetDateTime;
+import java.util.UUID;
 
 import org.apache.james.backends.postgres.PostgresIndex;
 import org.apache.james.backends.postgres.PostgresModule;
@@ -36,6 +37,7 @@ public interface PostgresSieveModule {
 
         Field<String> USERNAME = DSL.field("username", SQLDataType.VARCHAR(255).notNull());
         Field<String> SCRIPT_NAME = DSL.field("script_name", SQLDataType.VARCHAR.notNull());
+        Field<UUID> SCRIPT_ID = DSL.field("script_id", SQLDataType.UUID.notNull());
         Field<Long> SCRIPT_SIZE = DSL.field("script_size", SQLDataType.BIGINT.notNull());
         Field<String> SCRIPT_CONTENT = DSL.field("script_content", SQLDataType.VARCHAR.notNull());
         Field<Boolean> IS_ACTIVE = DSL.field("is_active", SQLDataType.BOOLEAN.notNull());
@@ -43,13 +45,15 @@ public interface PostgresSieveModule {
 
         PostgresTable TABLE = PostgresTable.name(TABLE_NAME.getName())
             .createTableStep(((dsl, tableName) -> dsl.createTableIfNotExists(tableName)
+                .column(SCRIPT_ID)
                 .column(USERNAME)
                 .column(SCRIPT_NAME)
                 .column(SCRIPT_SIZE)
                 .column(SCRIPT_CONTENT)
                 .column(IS_ACTIVE)
                 .column(ACTIVATION_DATE_TIME)
-                .primaryKey(USERNAME, SCRIPT_NAME)))
+                .primaryKey(SCRIPT_ID)
+                .constraint(DSL.unique(USERNAME, SCRIPT_NAME))))
             .disableRowLevelSecurity()
             .build();
 
diff --git a/server/data/data-postgres/src/main/java/org/apache/james/sieve/postgres/PostgresSieveRepository.java b/server/data/data-postgres/src/main/java/org/apache/james/sieve/postgres/PostgresSieveRepository.java
index 662915c523..f9b09e8eab 100644
--- a/server/data/data-postgres/src/main/java/org/apache/james/sieve/postgres/PostgresSieveRepository.java
+++ b/server/data/data-postgres/src/main/java/org/apache/james/sieve/postgres/PostgresSieveRepository.java
@@ -34,6 +34,7 @@ import org.apache.james.core.Username;
 import org.apache.james.core.quota.QuotaSizeLimit;
 import org.apache.james.core.quota.QuotaSizeUsage;
 import org.apache.james.sieve.postgres.model.PostgresSieveScript;
+import org.apache.james.sieve.postgres.model.PostgresSieveScriptId;
 import org.apache.james.sieverepository.api.ScriptContent;
 import org.apache.james.sieverepository.api.ScriptName;
 import org.apache.james.sieverepository.api.ScriptSummary;
@@ -74,6 +75,7 @@ public class PostgresSieveRepository implements SieveRepository {
                 .scriptContent(content.getValue())
                 .scriptSize(content.length())
                 .isActive(false)
+                .id(PostgresSieveScriptId.generate())
                 .build())
             .flatMap(upsertedScripts -> {
                 if (upsertedScripts > 0) {
diff --git a/server/data/data-postgres/src/main/java/org/apache/james/sieve/postgres/PostgresSieveScriptDAO.java b/server/data/data-postgres/src/main/java/org/apache/james/sieve/postgres/PostgresSieveScriptDAO.java
index b87778db9f..a1d8b93b49 100644
--- a/server/data/data-postgres/src/main/java/org/apache/james/sieve/postgres/PostgresSieveScriptDAO.java
+++ b/server/data/data-postgres/src/main/java/org/apache/james/sieve/postgres/PostgresSieveScriptDAO.java
@@ -23,6 +23,7 @@ import static org.apache.james.backends.postgres.utils.PostgresExecutor.DEFAULT_
 import static org.apache.james.sieve.postgres.PostgresSieveModule.PostgresSieveScriptTable.ACTIVATION_DATE_TIME;
 import static org.apache.james.sieve.postgres.PostgresSieveModule.PostgresSieveScriptTable.IS_ACTIVE;
 import static org.apache.james.sieve.postgres.PostgresSieveModule.PostgresSieveScriptTable.SCRIPT_CONTENT;
+import static org.apache.james.sieve.postgres.PostgresSieveModule.PostgresSieveScriptTable.SCRIPT_ID;
 import static org.apache.james.sieve.postgres.PostgresSieveModule.PostgresSieveScriptTable.SCRIPT_NAME;
 import static org.apache.james.sieve.postgres.PostgresSieveModule.PostgresSieveScriptTable.SCRIPT_SIZE;
 import static org.apache.james.sieve.postgres.PostgresSieveModule.PostgresSieveScriptTable.TABLE_NAME;
@@ -37,6 +38,7 @@ import javax.inject.Named;
 import org.apache.james.backends.postgres.utils.PostgresExecutor;
 import org.apache.james.core.Username;
 import org.apache.james.sieve.postgres.model.PostgresSieveScript;
+import org.apache.james.sieve.postgres.model.PostgresSieveScriptId;
 import org.apache.james.sieverepository.api.ScriptName;
 import org.jooq.Record;
 
@@ -53,6 +55,7 @@ public class PostgresSieveScriptDAO {
 
     public Mono<Long> upsertScript(PostgresSieveScript sieveScript) {
         return postgresExecutor.executeReturnAffectedRowsCount(dslContext -> Mono.from(dslContext.insertInto(TABLE_NAME)
+            .set(SCRIPT_ID, sieveScript.getId().getValue())
             .set(USERNAME, sieveScript.getUsername())
             .set(SCRIPT_NAME, sieveScript.getScriptName())
             .set(SCRIPT_SIZE, sieveScript.getScriptSize())
@@ -147,6 +150,7 @@ public class PostgresSieveScriptDAO {
             .scriptSize(record.get(SCRIPT_SIZE))
             .isActive(record.get(IS_ACTIVE))
             .activationDateTime(record.get(ACTIVATION_DATE_TIME))
+            .id(new PostgresSieveScriptId(record.get(SCRIPT_ID)))
             .build();
     }
 }
diff --git a/server/data/data-postgres/src/main/java/org/apache/james/sieve/postgres/model/PostgresSieveScript.java b/server/data/data-postgres/src/main/java/org/apache/james/sieve/postgres/model/PostgresSieveScript.java
index d5831d5464..f1a29812cc 100644
--- a/server/data/data-postgres/src/main/java/org/apache/james/sieve/postgres/model/PostgresSieveScript.java
+++ b/server/data/data-postgres/src/main/java/org/apache/james/sieve/postgres/model/PostgresSieveScript.java
@@ -41,6 +41,7 @@ public class PostgresSieveScript {
         private long scriptSize;
         private boolean isActive;
         private OffsetDateTime activationDateTime;
+        private PostgresSieveScriptId id;
 
         public Builder username(String username) {
             Preconditions.checkNotNull(username);
@@ -64,6 +65,11 @@ public class PostgresSieveScript {
             return this;
         }
 
+        public Builder id(PostgresSieveScriptId id) {
+            this.id = id;
+            return this;
+        }
+
         public Builder isActive(boolean isActive) {
             this.isActive = isActive;
             return this;
@@ -77,11 +83,13 @@ public class PostgresSieveScript {
         public PostgresSieveScript build() {
             Preconditions.checkState(StringUtils.isNotBlank(username), "'username' is mandatory");
             Preconditions.checkState(StringUtils.isNotBlank(scriptName), "'scriptName' is mandatory");
+            Preconditions.checkState(id != null, "'id' is mandatory");
 
-            return new PostgresSieveScript(username, scriptName, scriptContent, scriptSize, isActive, activationDateTime);
+            return new PostgresSieveScript(id, username, scriptName, scriptContent, scriptSize, isActive, activationDateTime);
         }
     }
 
+    private final PostgresSieveScriptId id;
     private final String username;
     private final String scriptName;
     private final String scriptContent;
@@ -89,7 +97,9 @@ public class PostgresSieveScript {
     private final boolean isActive;
     private final OffsetDateTime activationDateTime;
 
-    private PostgresSieveScript(String username, String scriptName, String scriptContent, long scriptSize, boolean isActive, OffsetDateTime activationDateTime) {
+    private PostgresSieveScript(PostgresSieveScriptId id, String username, String scriptName, String scriptContent,
+                                long scriptSize, boolean isActive, OffsetDateTime activationDateTime) {
+        this.id = id;
         this.username = username;
         this.scriptName = scriptName;
         this.scriptContent = scriptContent;
@@ -98,6 +108,10 @@ public class PostgresSieveScript {
         this.activationDateTime = activationDateTime;
     }
 
+    public PostgresSieveScriptId getId() {
+        return id;
+    }
+
     public String getUsername() {
         return username;
     }
diff --git a/server/data/data-postgres/src/main/java/org/apache/james/sieve/postgres/model/PostgresSieveScriptId.java b/server/data/data-postgres/src/main/java/org/apache/james/sieve/postgres/model/PostgresSieveScriptId.java
new file mode 100644
index 0000000000..adb0778f1f
--- /dev/null
+++ b/server/data/data-postgres/src/main/java/org/apache/james/sieve/postgres/model/PostgresSieveScriptId.java
@@ -0,0 +1,38 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you 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   *
+ *                                                              *
+ *   http://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 org.apache.james.sieve.postgres.model;
+
+import java.util.UUID;
+
+public class PostgresSieveScriptId {
+    public static PostgresSieveScriptId generate() {
+        return new PostgresSieveScriptId(UUID.randomUUID());
+    }
+
+    private final UUID value;
+
+    public PostgresSieveScriptId(UUID value) {
+        this.value = value;
+    }
+
+    public UUID getValue() {
+        return value;
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org