This issue was originally created by @hughns at matrix-org/matrix-authentication-service#2268.
Currently we have a hangover from an incremental migration mode that was never completed.
Instead we should be doing a single transaction and only committing it on success.