Bulk Upsert with insert_all + Unique Index

9225
0

I stopped doing row-by-row imports once they started hammering the DB. In Migration (unique index), I added a real uniqueness guarantee on provider + uid, because bulk writes only stay correct if the database can enforce constraints. Then, in Bulk upsert, I build a rows array from the inbound payload, set created_at and updated_at explicitly using a single now, and hand everything to ExternalProfile.upsert_all with unique_by: %i[provider uid]. This gives me a single statement that inserts or updates deterministically, even when the same payload is replayed. The important detail is that the app code stays simple and the database owns truth, instead of sprinkling find_or_initialize_by all over the codebase.