Last active
August 29, 2015 14:16
-
-
Save dimroc/3a8bec16993f384c6398 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Datafixes::AddSupplierIdToGifts < Datafix | |
| def self.up | |
| execute(<<-SQL) | |
| UPDATE gifts SET supplier_id = products.selected_supplier | |
| FROM (SELECT gifts.id as gift_id, products.supplier_id as selected_supplier FROM gifts JOIN products | |
| ON gifts.selected_product_id = products.id | |
| WHERE stage = 'redeemed') as products | |
| WHERE gifts.id = products.gift_id | |
| SQL | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require "rails_helper" | |
| require Rails.root.join("db", "datafixes", "20141208102001_add_supplier_id_to_gifts") | |
| describe Datafixes::AddSupplierIdToGifts do | |
| describe ".up" do | |
| let!(:gift1) do | |
| gift = FactoryGirl.build(:redeemed_gift, supplier: nil) | |
| gift.save(validate: false) | |
| gift | |
| end | |
| let!(:gift2) do | |
| gift = FactoryGirl.build(:redeemed_gift, selected_product: gift1.selected_product, supplier: nil) | |
| gift.save(validate: false) | |
| gift | |
| end | |
| let!(:gift3) do | |
| gift = FactoryGirl.build(:redeemed_gift, supplier: nil) | |
| gift.save(validate: false) | |
| gift | |
| end | |
| let!(:excluded_gift1) { FactoryGirl.create(:sent_gift) } | |
| let!(:excluded_gift2) { FactoryGirl.create(:gift) } | |
| it "should populate the supplier" do | |
| Datafixes::AddSupplierIdToGifts.up | |
| expect(gift1.reload.supplier).to eq gift1.selected_product.supplier | |
| expect(gift2.reload.supplier).to eq gift1.selected_product.supplier | |
| expect(gift2.reload.supplier).to eq gift2.selected_product.supplier | |
| expect(gift3.reload.supplier).to eq gift3.selected_product.supplier | |
| expect(excluded_gift1.reload.supplier).to be_nil | |
| expect(excluded_gift2.reload.supplier).to be_nil | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment