mirror of
https://github.com/hakanensari/frankfurter.git
synced 2024-11-22 02:52:49 +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
|
module Bank
|
||||||
def self.fetch_all_rates!
|
def self.fetch_all_rates!
|
||||||
Currency.db.transaction do
|
Currency.dataset.insert_conflict.multi_insert(Feed.historical.to_a)
|
||||||
Currency.dataset.delete
|
|
||||||
Currency.multi_insert(Feed.historical.to_a)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.fetch_current_rates!
|
def self.fetch_current_rates!
|
||||||
@ -18,4 +15,9 @@ module Bank
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.replace_all_rates!
|
||||||
|
Currency.dataset.delete
|
||||||
|
Currency.multi_insert(Feed.historical.to_a)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
@ -13,7 +13,6 @@ describe Bank do
|
|||||||
|
|
||||||
before do
|
before do
|
||||||
VCR.insert_cassette 'feed'
|
VCR.insert_cassette 'feed'
|
||||||
Currency.dataset.delete
|
|
||||||
end
|
end
|
||||||
|
|
||||||
after do
|
after do
|
||||||
@ -21,12 +20,26 @@ describe Bank do
|
|||||||
end
|
end
|
||||||
|
|
||||||
it 'fetches all rates' do
|
it 'fetches all rates' do
|
||||||
|
Currency.dataset.delete
|
||||||
Bank.fetch_all_rates!
|
Bank.fetch_all_rates!
|
||||||
Currency.count.must_be :positive?
|
Currency.count.must_be :positive?
|
||||||
end
|
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
|
it 'fetches current rates' do
|
||||||
|
Currency.dataset.delete
|
||||||
Bank.fetch_current_rates!
|
Bank.fetch_current_rates!
|
||||||
Currency.count.must_be :positive?
|
Currency.count.must_be :positive?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'replaces all rates' do
|
||||||
|
Currency.dataset.delete
|
||||||
|
Bank.fetch_all_rates!
|
||||||
|
Currency.count.must_be :positive?
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user