[Automated Suite] Unique constraint violation while attempting to delete an automated suite
Affected version : 11.0.0 -> 13
But after a study, the problem potentially exists since the v10.
I say potentially, because it is random and remained hidden till today.
Problem
We encountered an error while chaining individual deletions of automated suites in the campaign space.
Caused by: org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "uq_execution_execution_order_test_plan_item_id"
Detail: Key (execution_order, test_plan_item_id)=(2, 3) already exists.
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutor Impl.java:2734) ~[postgresql-42.7.7.jar!/:42.7.7]
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2421) ~[postgresql-42.7.7.jar!/:42.7.7]
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:372) ~[postgresql-42.7.7.jar!/:42.7.7]
at org.postgresql jdbc .PgStatement .executeInternal(PgStatement.java:518) ~[postgresql-42.7.7.jar!/:42.7.7]
at org.postgresql.jdbc.PgStatement .execute(PgStatement.java:435) ~[postgresql-42.7.7.jar!/:42.7.7]
at org.postgresql.jdbc.PgPreparedStatement .executeWithFlags(PgPreparedStatement.java:196) ~[postgresql-42.7.7.jar!/:42.7.7]
at org.postgresql jdbc.PgPreparedStatement .execute(PgPreparedStatement .java:182) ~[postgresql-42.7.7.jar!/:42.7.7]
at com.zaxxer.hikart.pool.ProxyPreparedStatement .execute(ProxyPreparedStatement.java:44) ~[HikariCP-6.3.1.jar!/:?]
at com.zaxxer.hikart.pool.HikariProxyPreparedStatement .execute(Hikar iProxyPreparedStatement.java) ~[HikariCP-6.3.1.jar!/:?]
at org.jooq.tools.jdbc .DefaultPreparedStatement .execute(DefaultPreparedStatement.java:219) ~[jooq-3.19.24.jar!/:?]
at org.jooq.impl.AbstractQuery.execute(AbstractQuery.java:458) ~[jooq-3.19.24.jar!/:?]
at org.jooq.impl.AbstractDMLQuery.execute(AbstractDMLQuery.java: 1099) ~[ jooq-3.19.24.jar!/:?]
at org.jooq.impl.AbstractQuery.execute(AbstractQuery.java:348) ~[jooq-3.19.24.jar!/:?]
212 more
[25-08-07 09:10:04.176] SquashTM - 47 ERROR [http-nio-8080-exec-10] [] --- org.apache.catalina.core.ContainerBase.[Tomcat ].[locathost].[/squash].
[dispatcherServlet]: Servlet.service() for servlet [dispatcherServlet] in context with path [/squash] threw exception [Request processing failed:
SQL [update "execution" set "execution_order" = (select (count(*) - ?) from "execution" as "e2" where ("e2"."test_plan_item_id" = "execution"."test_plan_item_id" and ("e2"."execution_order" < "execution"."execution_order" or ("e2"."execution_order" = "execution"."execution_order" and "e2"."execution_id" <= "execution"."execution_id")))) where "execution"."test_plan_item_id" in (?, ?, ?, ?, ?, ?, ?)];
The trouble comes from the fact that constraints in PostgreSQL and MariaDB are by default "NOT DEFERRABLE", which means that the update is made line by line and the unicity check controlled after each line.
The order of the update is random.
So if the future value given to a row meets the old value of another row not updated yet, then it bursts...