diff --git a/openupgrade_scripts/scripts/event/19.0.1.9/pre-migration.py b/openupgrade_scripts/scripts/event/19.0.1.9/pre-migration.py index e0b39b55a5c8..61065e8ace51 100644 --- a/openupgrade_scripts/scripts/event/19.0.1.9/pre-migration.py +++ b/openupgrade_scripts/scripts/event/19.0.1.9/pre-migration.py @@ -37,8 +37,51 @@ def event_event_badge_format(env): ) +def remap_cancelled_stage(env): + """ + 19.0 drops the Cancelled stage in favour of kanban_state='cancel'. + event.event.stage_id is ondelete='restrict', so remap cancelled events + onto the surviving Ended stage (flagged cancelled) before removing the + stage and its ir_model_data row. Pure SQL: event's models are not in + the registry during its own pre-migration. + """ + env.cr.execute( + """ + SELECT name, res_id FROM ir_model_data + WHERE module = 'event' AND model = 'event.stage' + AND name IN ('event_stage_cancelled', 'event_stage_done') + """ + ) + stages = dict(env.cr.fetchall()) + cancelled = stages.get("event_stage_cancelled") + done = stages.get("event_stage_done") + if not cancelled: + return + if done: + openupgrade.logged_query( + env.cr, + """ + UPDATE event_event + SET stage_id = %s, kanban_state = 'cancel' + WHERE stage_id = %s + """, + (done, cancelled), + ) + openupgrade.logged_query( + env.cr, "DELETE FROM event_stage WHERE id = %s", (cancelled,) + ) + openupgrade.logged_query( + env.cr, + """ + DELETE FROM ir_model_data + WHERE module = 'event' AND name = 'event_stage_cancelled' + """, + ) + + @openupgrade.migrate() def migrate(env, version): openupgrade.copy_columns(env.cr, _copy_columns) openupgrade.add_fields(env, _added_fields) event_event_badge_format(env) + remap_cancelled_stage(env)