The migration would recreate the reminders table but by deferring the foreign keys, the entries in the reminder_services table were removed (ON DELETE CASCADE) so no reminder had a notification service selected. Fixed that by completely turning off and on the foreign key system instead of simply deferring them.
When importing a backup from a long time ago (more than one DB backup interval ago), a backup would be created the moment the import was done, regardless of when the previous backup was made (so even when that was minutes ago). Now the schedule is kept, by copying over the timestamp of the last DB backup from the current DB to the imported DB.
The changes made since startup, stored in the wal file, were carried over when importing a database. This was because the database file was replaced with the imported database file, but the changes were stored in the wal file. So now we merge all changes into the main database file before replacing them.
We needed the close_all_db function to close any remaining database connections on shutdown. But why were there any unclosed db connections anyway? The connections were closed after the thread was already marked as shut down so when exiting the thread wasn't done with closing the database yet and thus sometimes you had an improper shutdown. Now the threads are only marked as stopped when the database connection is also closed so we have guaranteed that they'll be closed when the server returns. So no more need to explicitly close any remaining afterwards.