From 4308f7d09c4949ac1ee9de640e195567d19637f7 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Wed, 22 Oct 2014 09:43:10 +1100 Subject: [PATCH 1/2] Revert "use sql for migration" This reverts commit fec8f08966b4695a51a17867599c0c371fcbd685. --- ...140927005043_enterprise_config_refactor.rb | 53 +++++++++---------- db/schema.rb | 2 +- 2 files changed, 26 insertions(+), 29 deletions(-) diff --git a/db/migrate/20140927005043_enterprise_config_refactor.rb b/db/migrate/20140927005043_enterprise_config_refactor.rb index f497e62f10..f9e8c936ce 100644 --- a/db/migrate/20140927005043_enterprise_config_refactor.rb +++ b/db/migrate/20140927005043_enterprise_config_refactor.rb @@ -4,19 +4,8 @@ class EnterpriseConfigRefactor < ActiveRecord::Migration add_index :enterprises, :sells add_index :enterprises, [:is_primary_producer, :sells] - # Combine is_distributor and type into sells. - db.select_values("SELECT id FROM enterprises").each do |enterprise_id| - distributor = db.select_values("SELECT is_distributor FROM enterprises WHERE id = #{db.quote(enterprise_id)}") - primary_producer = db.select_value("SELECT is_distributor FROM enterprises WHERE id = #{db.quote(enterprise_id)}") - type = db.select_value("SELECT type FROM enterprises WHERE id = #{db.quote(enterprise_id)}") - if type == "single" && (distributor || primary_producer) - sells = "own" - elsif !distributor || type == "profile" - sells = "none" - else - sells = "any" - end - db.update("UPDATE enterprises SET sells = #{db.quote(sells)} WHERE id = #{db.quote(enterprise_id)}") + Enterprise.all.each do |enterprise| + enterprise.update_attributes!({:sells => sells_what?(enterprise)}) end remove_column :enterprises, :type @@ -28,25 +17,33 @@ class EnterpriseConfigRefactor < ActiveRecord::Migration add_column :enterprises, :type, :string, null: false, default: 'profile' add_column :enterprises, :is_distributor, :boolean - # Combine is_distributor and type into sells. - db.select_values("SELECT id FROM enterprises").each do |enterprise_id| - sells = db.select_value("SELECT sells FROM enterprises WHERE id = #{db.quote(enterprise_id)}") - case sells - when "own" - type = "single" - when "any" - type = "full" - else - type = "profile" - end - distributor = sells != "none" - db.update("UPDATE enterprises SET type = #{db.quote(type)}, is_distributor = #{db.quote(distributor)} WHERE id = #{db.quote(enterprise_id)}") + Enterprise.all.each do |enterprise| + enterprise.update_attributes!({ + :type => type?(enterprise), + :is_distributor => distributes?(enterprise) + }) end remove_column :enterprises, :sells end - def db - ActiveRecord::Base.connection + def sells_what?(enterprise) + is_distributor = enterprise.read_attribute(:is_distributor) + is_primary_producer = enterprise.read_attribute(:is_primary_producer) + type = enterprise.read_attribute(:type) + return "own" if type == "single" && (is_distributor || is_primary_producer) + return "none" if !is_distributor || type == "profile" + return "any" + end + + def distributes?(enterprise) + enterprise.read_attribute(:sells) != "none" + end + + def type?(enterprise) + sells = enterprise.read_attribute(:sells) + return "profile" if sells == "none" + return "single" if sells == "own" + return "full" end end diff --git a/db/schema.rb b/db/schema.rb index 114dcd6b73..974dc8ac9b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -264,11 +264,11 @@ ActiveRecord::Schema.define(:version => 20141010043405) do t.string "instagram" t.string "linkedin" t.integer "owner_id", :null => false - t.string "sells", :default => "none", :null => false t.string "confirmation_token" t.datetime "confirmed_at" t.datetime "confirmation_sent_at" t.string "unconfirmed_email" + t.string "sells", :default => "none", :null => false end add_index "enterprises", ["address_id"], :name => "index_enterprises_on_address_id" From b75af8d9fff0955c9f1f8fc0c18d22f12b278562 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Wed, 22 Oct 2014 10:49:23 +1100 Subject: [PATCH 2/2] Fix sells column coming out all 'none' --- db/migrate/20140927005043_enterprise_config_refactor.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/db/migrate/20140927005043_enterprise_config_refactor.rb b/db/migrate/20140927005043_enterprise_config_refactor.rb index f9e8c936ce..6b51ac6e50 100644 --- a/db/migrate/20140927005043_enterprise_config_refactor.rb +++ b/db/migrate/20140927005043_enterprise_config_refactor.rb @@ -1,9 +1,15 @@ class EnterpriseConfigRefactor < ActiveRecord::Migration + class Enterprise < ActiveRecord::Base + self.inheritance_column = nil + end + def up add_column :enterprises, :sells, :string, null: false, default: 'none' add_index :enterprises, :sells add_index :enterprises, [:is_primary_producer, :sells] + Enterprise.reset_column_information + Enterprise.all.each do |enterprise| enterprise.update_attributes!({:sells => sells_what?(enterprise)}) end @@ -17,6 +23,8 @@ class EnterpriseConfigRefactor < ActiveRecord::Migration add_column :enterprises, :type, :string, null: false, default: 'profile' add_column :enterprises, :is_distributor, :boolean + Enterprise.reset_column_information + Enterprise.all.each do |enterprise| enterprise.update_attributes!({ :type => type?(enterprise),