Use ON CONFLICT when fetching all records

This commit is contained in:
Hakan Ensari 2018-03-22 08:49:32 +00:00
parent 9389566ab0
commit a0a981e897
2 changed files with 20 additions and 5 deletions

View File

@ -5,10 +5,7 @@ require 'bank/feed'
module Bank
def self.fetch_all_rates!
Currency.db.transaction do
Currency.dataset.delete
Currency.multi_insert(Feed.historical.to_a)
end
Currency.dataset.insert_conflict.multi_insert(Feed.historical.to_a)
end
def self.fetch_current_rates!
@ -18,4 +15,9 @@ module Bank
end
end
end
def self.replace_all_rates!
Currency.dataset.delete
Currency.multi_insert(Feed.historical.to_a)
end
end

View File

@ -13,7 +13,6 @@ describe Bank do
before do
VCR.insert_cassette 'feed'
Currency.dataset.delete
end
after do
@ -21,12 +20,26 @@ describe Bank do
end
it 'fetches all rates' do
Currency.dataset.delete
Bank.fetch_all_rates!
Currency.count.must_be :positive?
end
it 'skips existing records when fetching all rates' do
Currency.where { date < '2012-01-01' }.delete
Bank.fetch_all_rates!
Currency.where { date < '2012-01-01' }.count.must_be :positive?
end
it 'fetches current rates' do
Currency.dataset.delete
Bank.fetch_current_rates!
Currency.count.must_be :positive?
end
it 'replaces all rates' do
Currency.dataset.delete
Bank.fetch_all_rates!
Currency.count.must_be :positive?
end
end