Compare commits

...

2 Commits

Author SHA1 Message Date
Stéphan Sainléger
e06d750b74 [PERF] finalize_db: purge `ir.ui.view` images in a single pass
The purge ran one correlated ``NOT EXISTS`` scan of ``ir_ui_view`` per
candidate attachment (``ILIKE '%web/image/<id>%'`` on ``arch_db::text``),
taking hours on large databases. It now scans ``ir_ui_view`` once with
``regexp_matches``, builds a deduplicated set of referenced ids, and
deletes orphans via hash lookups.

This also fixes substring false positives: ``/web/image/12`` was kept
because it matched ``/web/image/123``. Integer comparison (``ref_id =
a.id``) now requires an exact match.
2026-08-13 11:25:51 +02:00
Stéphan Sainléger
0c096bb5ff [FIX] pre_upgrade: guard `theme_ir_ui_view` cleanup with to_regclass
The table ``theme_ir_ui_view`` only exists when the ``theme`` (website)
module was installed in the source database. If it was never installed,
the cleanup block crashes with "relation does not exist". Add a
``to_regclass`` guard to skip silently instead.
2026-08-11 22:50:51 +02:00
2 changed files with 63 additions and 10 deletions

View File

@@ -32,16 +32,24 @@ query_postgres_container "$CLEANUP_SQL" "$DB_NAME"
# ────────────────────────────────────────────────────────────
echo "Purging ir.ui.view images orphaned of their parent view and unreferenced in any arch_db..."
PURGE_VIEW_IMAGES_SQL=$(cat <<'EOF'
DELETE FROM ir_attachment a
WHERE a.res_model = 'ir.ui.view'
AND a.mimetype LIKE 'image/%'
AND a.store_fname IS NOT NULL
AND a.res_id NOT IN (SELECT id FROM ir_ui_view)
AND NOT EXISTS (
SELECT 1 FROM ir_ui_view v
WHERE v.arch_db::text ILIKE '%web/image/' || a.id || '%'
OR v.arch_db::text ILIKE '%web/content/' || a.id || '%'
);
DO $$
DECLARE purged int;
BEGIN
WITH refs AS (
SELECT DISTINCT (m[1])::bigint AS ref_id
FROM ir_ui_view v
CROSS JOIN LATERAL regexp_matches(
COALESCE(v.arch_db::text, ''), '/web/(?:image|content)/(\d+)', 'g') AS m
)
DELETE FROM ir_attachment a
WHERE a.res_model = 'ir.ui.view'
AND a.mimetype LIKE 'image/%'
AND a.store_fname IS NOT NULL
AND a.res_id NOT IN (SELECT id FROM ir_ui_view)
AND NOT EXISTS (SELECT 1 FROM refs r WHERE r.ref_id = a.id);
GET DIAGNOSTICS purged = ROW_COUNT;
RAISE NOTICE 'Purged % orphaned ir.ui.view image attachments', purged;
END $$;
EOF
)
query_postgres_container "$PURGE_VIEW_IMAGES_SQL" "$DB_NAME"

View File

@@ -132,6 +132,11 @@ DECLARE
deleted_imd INTEGER;
deleted_tpl INTEGER;
BEGIN
IF to_regclass('theme_ir_ui_view') IS NULL THEN
RAISE NOTICE 'Table theme_ir_ui_view does not exist, skipping stale view cleanup.';
RETURN;
END IF;
WITH targets AS (
SELECT id FROM theme_ir_ui_view
WHERE inherit_id LIKE 'ir.ui.view,%'
@@ -166,6 +171,46 @@ EOF
echo "SQL command = $PRE_MIGRATE_SQL_5"
query_postgres_container "$PRE_MIGRATE_SQL_5" ou17 || exit 1
# ────────────────────────────────────────────────────────────
# Drop mail.tracking.value rows referencing event.registration.mobile
#
# In Odoo 17 the field event.registration.mobile is removed (OpenUpgrade:
# "event / event.registration / mobile (char) : DEL"). During _process_end,
# Odoo unlinks the now-orphan ir.model.fields row. mail's ir_model_fields
# unlink() override iterates the mail.tracking.value rows pointing to it and
# calls event.registration._mail_track_get_field_sequence('mobile'), which
# does self._fields['mobile'] -> KeyError: 'mobile', aborting the registry:
#
# KeyError: 'mobile' (mail/models/models.py, _mail_track_get_field_sequence)
#
# The OpenUpgrade event script deletes the field but leaves its tracking
# values, so any DB that tracked this field crashes. We delete those tracking
# values beforehand so the field unlinks cleanly.
# Scoped strictly to event.registration.mobile. Idempotent.
# NOTE: this runs before -u all, so the FK column is still named 'field'
# (mail renames it to 'field_id' during the 16->17 upgrade).
# ────────────────────────────────────────────────────────────
PRE_MIGRATE_SQL_6=$(cat <<'EOF'
DO $$
DECLARE
deleted_count INTEGER;
BEGIN
WITH del AS (
DELETE FROM mail_tracking_value
WHERE field IN (
SELECT id FROM ir_model_fields
WHERE model = 'event.registration' AND name = 'mobile'
)
RETURNING id
)
SELECT count(*) INTO deleted_count FROM del;
RAISE NOTICE 'Removed % mail_tracking_value row(s) for event.registration.mobile', deleted_count;
END $$;
EOF
)
echo "SQL command = $PRE_MIGRATE_SQL_6"
query_postgres_container "$PRE_MIGRATE_SQL_6" ou17 || exit 1
# Copy filestores
copy_filestore ou16 ou16 ou17 ou17 || exit 1