mirror of
https://github.com/hakanensari/frankfurter.git
synced 2024-10-29 23:32:35 +01:00
Use ON CONFLICT when fetching all records
This commit is contained in:
parent
9389566ab0
commit
a0a981e897
10
lib/bank.rb
10
lib/bank.rb
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user