This is a backport of the commit integrated as part of MDL-76055 where we needed the update_enrolments working to be able to test them. Summary:
- Fixes a typo in table name preventing it to be updated ever. - Fix outer join that was missing records. - Stop playing and mixing ids (local and remote). - Better control which enrolments have to be kept (previously they were being deleted immediately after creating them). - Improve the coding style of inserts. - Modernise the deletions to use sql helper and work win 0..n ids.