From 3dbcf46981615e918a101bfbe75dbf89405b44aa Mon Sep 17 00:00:00 2001
From: Bastien Abadie <abadie@teklia.com>
Date: Wed, 28 Jun 2023 09:40:00 +0000
Subject: [PATCH] Update workflow creation date to respect
 process_started_after_created constraint

---
 .../process/migrations/0005_migrate_workflows.py    | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/arkindex/process/migrations/0005_migrate_workflows.py b/arkindex/process/migrations/0005_migrate_workflows.py
index 85d888a1ab..bd02626a26 100644
--- a/arkindex/process/migrations/0005_migrate_workflows.py
+++ b/arkindex/process/migrations/0005_migrate_workflows.py
@@ -14,6 +14,19 @@ class Migration(migrations.Migration):
     operations = [
         migrations.RunSQL(
             [
+                # In some rare cases, the workflow creation date is set a few milliseconds
+                # before the process creation date, breaking the process_started_after_created constraint
+                """
+                update ponos_workflow w
+                set created = data.created
+                from (
+                    select w.id, p.created
+                    from process_process p
+                    inner join ponos_workflow w on (w.id=p.workflow_id)
+                    where w.created < p.created
+                ) data
+                where data.id=w.id;
+                """,
                 # This query cannot be done with QuerySet.update, because Django does not allow joining during an update.
                 # This is why we use RunSQL here and not RunPython.
                 """
-- 
GitLab