Skip to content

Commit 35b43aa

Browse files
committed
[FIX] util/fields: clean sort in filters
In the remove_field, sort is not cleaned up when filters are cleaned. It sometimes can lead to post upgrade issues if someone uses a removed field for their custom filter. closes #52 Signed-off-by: Christophe Simonis (chs) <[email protected]>
1 parent 3c7205a commit 35b43aa

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/util/fields.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,28 @@ def clean_context(context):
154154
if changed:
155155
add_to_migration_reports(("ir.filters", id_, name), "Filters/Dashboards")
156156

157+
if column_exists(cr, "ir_filters", "sort"):
158+
cr.execute(
159+
"""
160+
WITH to_update AS (
161+
SELECT f.id,
162+
COALESCE(ARRAY_TO_JSON(ARRAY_AGG(s.sort_item ORDER BY rn) filter (WHERE s.sort_item not in %s)), '[]') AS sort
163+
FROM ir_filters f
164+
JOIN LATERAL JSONB_ARRAY_ELEMENTS_TEXT(f.sort::jsonb)
165+
WITH ORDINALITY AS s(sort_item, rn)
166+
ON true
167+
WHERE f.model_id = %s
168+
AND f.sort ~ %s
169+
GROUP BY id
170+
)
171+
UPDATE ir_filters f
172+
SET sort = t.sort
173+
FROM to_update t
174+
WHERE f.id = t.id
175+
""",
176+
[(fieldname, fieldname + " desc"), model, r"\y{}\y".format(fieldname)],
177+
)
178+
157179
def adapter(leaf, is_or, negated):
158180
# replace by TRUE_LEAF, unless negated or in a OR operation but not negated
159181
if is_or ^ negated:

0 commit comments

Comments
 (0)