Make the database more compact.

Should probably switch to something more efficient some day.
Fixed: bcc636a broke the unit tests.
New: improve database save safety.

Without this fix, if Cappbot was killed/stopped/ran out of drive space in the middle of saving the database, it would leave a corrupted (half written JSON file) on disk.

The fix is to write to an alternative file first and then only overwrite the old database with the new in a single atomic mv. If something crashes both the old and .new will exist and data can be recovered.