I suspect that might cause duplicate entry if any other db mode is used, after restore and then something that causes a change to the dialog - or even, not being deleted correctly since the DLG_FLAG_NEW flag is set.
Please have a look at the remove_dialog_from_db() function. This will return on dialog termination if the flag is set, without deleting from database.
You should probably only do this if: if(dlg_db_mode == DB_MODE_SHUTDOWN) {
But there may be need to set flags differently for other modes. It's important to test all modes that can be used when doing such a change.
If opening a pull request, please keep in mind clang-formating of the patch. Your commit will fail on this check.