Skip to content
Snippets Groups Projects
Commit d0a1a7e2 authored by Erwan Rouchet's avatar Erwan Rouchet Committed by Bastien Abadie
Browse files

Fix intermittent failure on add_parent test

parent 9a34b7f8
No related branches found
No related tags found
1 merge request!2002Fix intermittent failure on add_parent test
......@@ -294,11 +294,13 @@ class Element(IndexableModel):
# and always tries to add a GROUP BY documents_elementpath.id, which returns
# wildly incorrect results.
# We use two CTEs here to make this already fast query twice as fast.
# The parent_paths CTE is sorted to avoid intermittent test failures.
cursor.execute(
"""
WITH parent_paths AS (
SELECT path FROM documents_elementpath
WHERE element_id = %(parent_id)s
ORDER BY path
), child_paths AS (
SELECT path FROM documents_elementpath
WHERE element_id = %(self_id)s
......
......@@ -98,7 +98,12 @@ class TestEditElementPath(FixtureTestCase):
'savepoint': f"s{_thread.get_ident()}_x{connections['default'].savepoint_state + 1}",
'A': elements['A'].id,
'B': elements['B'].id,
'X': elements['X'].id,
# Element A has two parents so it has two paths.
# add_parent will pick the first one to perform updates on the new child's paths,
# so it will be seen in the SQL queries. To avoid intermittent failures,
# add_parent sorts parent paths by `path`, so we apply the same sort here.
# The paths only contain one ID, X's or Y's.
'first_parent': elements['A'].paths.order_by('path').first().path[0],
}
):
elements['B'].add_parent(elements['A'])
......
......@@ -3,7 +3,8 @@ SAVEPOINT "{savepoint}";
WITH parent_paths AS
(SELECT path
FROM documents_elementpath
WHERE element_id = '{A}'::uuid ),
WHERE element_id = '{A}'::uuid
ORDER BY path),
child_paths AS
(SELECT path
FROM documents_elementpath
......@@ -38,16 +39,16 @@ SELECT uuid_generate_v4(),
1
FROM documents_elementpath
WHERE element_id = '{A}'::uuid
AND path <> ARRAY['{X}'::uuid];
AND path <> ARRAY['{first_parent}'::uuid];
UPDATE "documents_elementpath"
SET "path" = ARRAY['{X}'::uuid,
SET "path" = ARRAY['{first_parent}'::uuid,
'{A}'::uuid]::uuid[], "ordering" = 1
WHERE ("documents_elementpath"."element_id" = '{B}'::uuid
AND "documents_elementpath"."path" = (ARRAY[])::uuid[]);
UPDATE "documents_elementpath"
SET "path" = array_cat(ARRAY['{X}'::uuid, '{A}'::uuid], "documents_elementpath"."path")::uuid[]
SET "path" = array_cat(ARRAY['{first_parent}'::uuid, '{A}'::uuid], "documents_elementpath"."path")::uuid[]
WHERE "documents_elementpath"."path" && (ARRAY['{B}'::uuid])::uuid[];
INSERT INTO documents_elementpath (id, element_id, path, ordering)
......@@ -57,10 +58,10 @@ SELECT uuid_generate_v4(),
child_paths.ordering
FROM documents_elementpath child_paths,
documents_elementpath new_parent_paths
WHERE child_paths.path @ > ARRAY['{X}'::uuid,
WHERE child_paths.path @ > ARRAY['{first_parent}'::uuid,
'{A}'::uuid,
'{B}'::uuid]
AND new_parent_paths.element_id = '{A}'::uuid
AND new_parent_paths.path <> ARRAY['{X}'::uuid];
AND new_parent_paths.path <> ARRAY['{first_parent}'::uuid];
RELEASE SAVEPOINT "{savepoint}"
......@@ -3,7 +3,8 @@ SAVEPOINT "{savepoint}";
WITH parent_paths AS
(SELECT path
FROM documents_elementpath
WHERE element_id = '{A}'::uuid ),
WHERE element_id = '{A}'::uuid
ORDER BY path),
child_paths AS
(SELECT path
FROM documents_elementpath
......
......@@ -38,7 +38,8 @@ SAVEPOINT "{savepoints[1]}";
WITH parent_paths AS
(SELECT path
FROM documents_elementpath
WHERE element_id = '{destination_id}'::uuid ),
WHERE element_id = '{destination_id}'::uuid
ORDER BY path),
child_paths AS
(SELECT path
FROM documents_elementpath
......
......@@ -38,7 +38,8 @@ SAVEPOINT "{savepoints[1]}";
WITH parent_paths AS
(SELECT path
FROM documents_elementpath
WHERE element_id = '{destination_id}'::uuid ),
WHERE element_id = '{destination_id}'::uuid
ORDER BY path),
child_paths AS
(SELECT path
FROM documents_elementpath
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment