Move the formatResults func to mysql query
Refactored the upsert query for postgres to be atomic