quantranhong1999 commented on code in PR #1849:
URL: https://github.com/apache/james-project/pull/1849#discussion_r1424869495


##########
server/data/data-postgres/src/main/java/org/apache/james/sieve/postgres/PostgresSieveModule.java:
##########
@@ -0,0 +1,62 @@
+/****************************************************************
+ * 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;
+
+import java.time.OffsetDateTime;
+
+import org.apache.james.backends.postgres.PostgresModule;
+import org.apache.james.backends.postgres.PostgresTable;
+import org.jooq.Field;
+import org.jooq.Record;
+import org.jooq.Table;
+import org.jooq.impl.DSL;
+import org.jooq.impl.SQLDataType;
+
+public interface PostgresSieveModule {
+    interface PostgresSieveScriptTable {
+        Table<Record> TABLE_NAME = DSL.table("sieve_scripts");
+
+        Field<String> USERNAME = DSL.field("username", 
SQLDataType.VARCHAR(255).notNull());
+        Field<String> SCRIPT_NAME = DSL.field("script_name", 
SQLDataType.VARCHAR.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());
+        Field<OffsetDateTime> ACTIVATION_DATE_TIME = 
DSL.field("activation_date_time", SQLDataType.OFFSETDATETIME);
+        String MAXIMUM_ONE_ACTIVE_SCRIPT_PER_USER_CONSTRAINT = 
String.format("ALTER TABLE %s ADD CONSTRAINT maximum_one_active_script_per_user 
EXCLUDE (%s WITH =) WHERE (%s = TRUE);",
+            TABLE_NAME.getName(), USERNAME.getName(), IS_ACTIVE.getName());
+
+        PostgresTable TABLE = PostgresTable.name(TABLE_NAME.getName())
+            .createTableStep(((dsl, tableName) -> 
dsl.createTableIfNotExists(tableName)
+                .column(USERNAME)
+                .column(SCRIPT_NAME)
+                .column(SCRIPT_SIZE)
+                .column(SCRIPT_CONTENT)
+                .column(IS_ACTIVE)
+                .column(ACTIVATION_DATE_TIME)
+                .primaryKey(USERNAME, SCRIPT_NAME)))
+            .disableRowLevelSecurity()
+            
.addAdditionalConstraints(MAXIMUM_ONE_ACTIVE_SCRIPT_PER_USER_CONSTRAINT)

Review Comment:
   I did not know that the unique index can enforce constraint.
   
   ```
           PostgresIndex MAXIMUM_ONE_ACTIVE_SCRIPT_PER_USER_INDEX = 
PostgresIndex.name("maximum_one_active_script_per_user")
               .createIndexStep(((dsl, indexName) -> 
dsl.createUniqueIndexIfNotExists(indexName)
                   .on(TABLE_NAME, USERNAME)
                   .where(IS_ACTIVE)));
   ```
   
   It worked. Thanks!
   
   Bear in mind that the unique index only makes constraint on the index itself 
and not the table. So it is not a really strong constraint as the tradditional 
constraint. e.g. if one day we want the indexes to be updated in asynchronous 
mode, then the unique index can not enforce constraint correctly.
   
   But I guess that won't be the case soon... 



##########
backends-common/postgres/src/main/java/org/apache/james/backends/postgres/PostgresTable.java:
##########
@@ -39,31 +41,60 @@ public interface CreateTableFunction {
 
     @FunctionalInterface
     public interface RequireRowLevelSecurity {
-        PostgresTable supportsRowLevelSecurity(boolean 
rowLevelSecurityEnabled);
+        FinalStage supportsRowLevelSecurity(boolean rowLevelSecurityEnabled);
 
-        default PostgresTable disableRowLevelSecurity() {
+        default FinalStage disableRowLevelSecurity() {
             return supportsRowLevelSecurity(false);
         }
 
-        default PostgresTable supportsRowLevelSecurity() {
+        default FinalStage supportsRowLevelSecurity() {
             return supportsRowLevelSecurity(true);
         }
     }
 
+    public static class FinalStage {
+        private final String tableName;
+        private final boolean supportsRowLevelSecurity;
+        private final Function<DSLContext, DDLQuery> createTableStepFunction;
+        private final ImmutableList.Builder<String> additionalConstraints;

Review Comment:
   Following the unique index adoption, I would split the 
`additionalAlterQueries` support into another PR. I think it still brings value 
as jOOQ DSL does not support all the cases.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to