From 0ab843540620c701dc685895cbaba932fd389e85 Mon Sep 17 00:00:00 2001 From: Jake Low Date: Mon, 16 Nov 2026 10:17:56 -0800 Subject: [PATCH] Fix task upsert failing on prod database Production has a unique index on tasks of (parent_id, md5(lower(name))), not the (parent_id, lower(name)) that the evolutions in this repo create. I'm not sure why they differ, probably someone modified the database by hand in the past. The difference means the ON CONFLICT added in #1300 does not work in prod, even though it worked in local testing. This change fixes the ON CONFLICT to work with the index that's currently live, and adds an evolution to fix the index in local dev which is a no-op when run on prod. --- app/org/maproulette/models/dal/TaskDAL.scala | 2 +- conf/evolutions/default/132.sql | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 conf/evolutions/default/132.sql diff --git a/app/org/maproulette/models/dal/TaskDAL.scala b/app/org/maproulette/models/dal/TaskDAL.scala index 0e8ebc02..002ba0f3 100644 --- a/app/org/maproulette/models/dal/TaskDAL.scala +++ b/app/org/maproulette/models/dal/TaskDAL.scala @@ -468,7 +468,7 @@ class TaskDAL @Inject() ( INSERT INTO tasks AS t (name, parent_id, status, instruction, priority, geojson, location, geom, cooperative_work_json) SELECT name, {parentId}, 0, instruction, priority, geojson, ST_Centroid(geom), geom, cooperative_work FROM input WHERE geom IS NOT NULL - ON CONFLICT (parent_id, lower(name)) DO UPDATE SET + ON CONFLICT (parent_id, md5(lower(name))) DO UPDATE SET name = EXCLUDED.name, instruction = EXCLUDED.instruction, priority = EXCLUDED.priority, changeset_id = -1, geojson = EXCLUDED.geojson, cooperative_work_json = EXCLUDED.cooperative_work_json, diff --git a/conf/evolutions/default/132.sql b/conf/evolutions/default/132.sql new file mode 100644 index 00000000..0dd5224f --- /dev/null +++ b/conf/evolutions/default/132.sql @@ -0,0 +1,20 @@ +# --- MapRoulette Scheme + +# --- !Ups + +-- Hash the task 'name' (unique key) to ensure indexed values are below the btree size +-- entry limit. Prod already has this index (created by hand at some point?) so this +-- is meant to be a no-op if this index already exists with the correct type. +DO $$ +BEGIN + IF NOT EXISTS ( + SELECT 1 FROM pg_indexes + WHERE indexname = 'idx_tasks_parent_id_name' AND indexdef LIKE '%md5(lower(%' + ) THEN + DROP INDEX IF EXISTS idx_tasks_parent_id_name;; + CREATE UNIQUE INDEX idx_tasks_parent_id_name ON tasks (parent_id, md5(lower(name)));; + END IF;; +END +$$;; + +# --- !Downs