[api] speed up migration

one big transaction to speed up history transaction.
INSERT DELAYED would be better here, but it seems not to be possible with
ActiveRecord?