diff --git a/alembic/versions/c8d9e0f1a2b3_rename_multilingual_glossaries.py b/alembic/versions/c8d9e0f1a2b3_rename_multilingual_glossaries.py index cb102aa..5af8dbf 100644 --- a/alembic/versions/c8d9e0f1a2b3_rename_multilingual_glossaries.py +++ b/alembic/versions/c8d9e0f1a2b3_rename_multilingual_glossaries.py @@ -1,12 +1,12 @@ -"""Rename multilingual glossaries from 'Anglais' to 'Multilingue' +"""Clean up glossaries: delete old FR→EN imports, rename multilingual ones Revision ID: c8d9e0f1a2b3 Revises: b7c8d9e0f1a2 Create Date: 2026-05-31 -Previous migration b7c8d9e0f1a2 set target_language='multi' but the rename -part was added after the migration was already applied on the server. -This migration handles the rename separately. +Old glossaries imported before multilingual enrichment have target_language='en' +and no translations. They are stale duplicates of the enriched templates. +This migration deletes them so users re-import the enriched versions. """ from typing import Sequence, Union @@ -22,7 +22,59 @@ depends_on: Union[str, Sequence[str], None] = None def upgrade() -> None: - # Rename glossaries that are multilingual but still have "Anglais" in name + # 1. Delete old FR→EN glossaries that don't have multilingual translations + # (imported before enrichment, they are stale duplicates) + op.execute(""" + DELETE FROM glossary_terms + WHERE glossary_id IN ( + SELECT g.id + FROM glossaries g + WHERE g.target_language = 'en' + AND g.source_language = 'fr' + AND g.id IN ( + SELECT g2.id + FROM glossaries g2 + JOIN glossary_terms gt ON gt.glossary_id = g2.id + WHERE gt.translations IS NULL + OR jsonb_typeof(gt.translations) = 'null' + OR (SELECT count(*) FROM jsonb_object_keys(COALESCE(gt.translations, '{}'))) = 0 + LIMIT 1 + ) + ) + """) + + op.execute(""" + DELETE FROM glossaries + WHERE target_language = 'en' + AND source_language = 'fr' + AND id IN ( + SELECT g.id + FROM glossaries g + LEFT JOIN glossary_terms gt ON gt.glossary_id = g.id + WHERE gt.translations IS NULL + OR jsonb_typeof(COALESCE(gt.translations, 'null')) IN ('null', 'null') + LIMIT 1 + ) + """) + + # 2. Rename remaining multilingual glossaries from 'Anglais' to 'Multilingue' + op.execute(""" + UPDATE glossaries + SET name = REPLACE(name, 'Anglais', 'Multilingue') + WHERE target_language = 'multi' + AND name LIKE '%%Anglais%%' + """) + + # 3. Also set any remaining FR glossaries with target_language='en' to 'multi' + # (they might have been imported from enriched templates but migration missed them) + op.execute(""" + UPDATE glossaries + SET target_language = 'multi' + WHERE source_language = 'fr' + AND target_language = 'en' + """) + + # 4. Rename those too op.execute(""" UPDATE glossaries SET name = REPLACE(name, 'Anglais', 'Multilingue') @@ -32,9 +84,14 @@ def upgrade() -> None: def downgrade() -> None: + # Cannot restore deleted glossaries, just revert names and target_language op.execute(""" UPDATE glossaries SET name = REPLACE(name, 'Multilingue', 'Anglais') - WHERE target_language = 'multi' - AND name LIKE '%%Multilingue%%' + WHERE name LIKE '%%Multilingue%%' + """) + op.execute(""" + UPDATE glossaries + SET target_language = 'en' + WHERE target_language = 'multi' """)