I have the following code in my project:
all_fields = property |> Map.drop([...]) |> Map.to_list()
on_conflict =
from lhs in Property,
where: lhs.transaction_time < ^property.transaction_time,
update: [set: ^all_fields]
Repo.insert(property,
on_conflict: on_conflict,
stale_error_field: :upsert,
stale_error_message: "upsert condition not met",
returning: true
)
This will insert a property to the database or upsert it only if the transaction_time
field is more recent than the one already in the DB.
This works great, but I would like to do bulk load of these properties using Repo.insert_all
, but I can’t figure out how to adapt this code to use insert_all
instead of insert
.
Any suggestions?