history: Use sqlite transactions
This speed up sqlite inserts/updates quite much. In tests, if migrations test is disabled, it results in 10x speed (the migration tests shall be slow as it creates several files and shall be as slow as disk I/O speed)