Skip to content

Instantly share code, notes, and snippets.

@robinjfisher
Created November 22, 2014 20:18
Show Gist options
  • Save robinjfisher/a6a2541cca07020e5e98 to your computer and use it in GitHub Desktop.
Save robinjfisher/a6a2541cca07020e5e98 to your computer and use it in GitHub Desktop.
Transition from MongoDB to MySQL
class NewContact < ActiveRecord::Base
file = File.open("#{Rails.root}" + "/config/database.yml")
dbconfig = YAML::load(file)
establish_connection(dbconfig["development"])
self.table_name = "contacts"
end
class NewDocument < ActiveRecord::Base
file = File.open("#{Rails.root}" + "/config/database.yml")
dbconfig = YAML::load(file)
establish_connection(dbconfig["development"])
self.table_name = "documents"
end
class NewImage < ActiveRecord::Base
file = File.open("#{Rails.root}" + "/config/database.yml")
dbconfig = YAML::load(file)
establish_connection(dbconfig["development"])
self.table_name = "images"
end
class NewNews < ActiveRecord::Base
file = File.open("#{Rails.root}" + "/config/database.yml")
dbconfig = YAML::load(file)
establish_connection(dbconfig["development"])
self.table_name = "news"
end
class NewProductGroup < ActiveRecord::Base
file = File.open("#{Rails.root}" + "/config/database.yml")
dbconfig = YAML::load(file)
establish_connection(dbconfig["development"])
self.table_name = "product_groups"
end
class NewProduct < ActiveRecord::Base
file = File.open("#{Rails.root}" + "/config/database.yml")
dbconfig = YAML::load(file)
establish_connection(dbconfig["development"])
self.table_name = "products"
end
class NewSpecification < ActiveRecord::Base
file = File.open("#{Rails.root}" + "/config/database.yml")
dbconfig = YAML::load(file)
establish_connection(dbconfig["development"])
self.table_name = "specifications"
end
class NewWarranty < ActiveRecord::Base
serialize :buying_factors
file = File.open("#{Rails.root}" + "/config/database.yml")
dbconfig = YAML::load(file)
establish_connection(dbconfig["development"])
self.table_name = "warranties"
end
class NewCollection < ActiveRecord::Base
file = File.open("#{Rails.root}" + "/config/database.yml")
dbconfig = YAML::load(file)
establish_connection(dbconfig["development"])
self.table_name = "collections"
end
begin
NewContact.all.map(&:delete)
contacts = Contact.all
attributes = %w(name email company phone created_at updated_at)
contacts.each do |c|
new_contact = NewContact.new
attributes.each do |a|
value = c.send("#{a}")
if (value.class == String) && value.length < 256
new_contact.update_attribute("#{a}",value)
end
end
new_contact.save
end
rescue StandardError => e
puts e
end
NewWarranty.all.map(&:delete)
warranties = Warranty.all
puts warranties.count
attributes = %w(product_name date_of_purchase date_of_installation purchased_from who_purchased easy_instructions buying_factors contact_us contact_method buyer_name buyer_address_one buyer_address_two town postcode email warranty_number)
warranties.each do |w|
new_warranty = NewWarranty.new
attributes.each do |a|
value = w.send("#{a}")
if (value.class == String) && value.length < 256
new_warranty.update_attribute("#{a}",value)
end
end
new_warranty.save
end
NewCollection.all.map(&:destroy)
collections = ['showering','commercial','heating and plumbing']
collections.each do |c|
NewCollection.create(name: c)
end
NewNews.all.map(&:delete)
news = News.unscoped.all
attributes = %w(title body active slug created_at updated_at)
news.each do |n|
puts "Processing news id #{n.id.to_s}"
new_news = NewNews.new
attributes.each do |a|
value = n.send("#{a}")
new_news.update_attribute("#{a}",value)
end
new_news.save
end
NewProductGroup.all.map(&:destroy)
NewProduct.all.map(&:destroy)
NewSpecification.all.map(&:destroy)
collections = {"Shower" => "showering", "Commercial" => "commercial", "Heating" => "heating and plumbing"}
product_groups = ProductGroup.all
product_groups.each do |pg|
pg_attributes = %w(name description slug updated_at created_at)
new_product_group = NewProductGroup.new
pg_attributes.each do |pg_attr|
value = pg.send("#{pg_attr}")
new_product_group.update_attribute("#{pg_attr}",value)
end
new_product_group.old_id = pg.id.to_s
collection = NewCollection.find_by_name(collections["#{pg.attributes['collection']}"])
new_product_group_id = ActiveRecord::Base.connection.quote("#{new_product_group.id}")
collection_id = ActiveRecord::Base.connection.quote("#{collection.id}")
query = "INSERT INTO collections_product_groups (collection_id,product_group_id) VALUES (#{collection_id},#{new_product_group_id})"
ActiveRecord::Base.connection.execute(query)
puts "Working master product list"
pg.products.where(:variation_of => nil).each do |product|
puts "Working product #{product.id.to_s}"
product_attributes = %w(description code slug blurb tmv2 tmv3 wras antiscald ce_approved ce_approvals featured created_at updated_at accessories)
new_product = NewProduct.new
product_attributes.each do |product_attr|
value = product.send("#{product_attr}")
new_product.update_attribute("#{product_attr}",value)
end
new_product.old_id = product.id.to_s # Track old id
new_product.product_group_id = new_product_group.id # Assign to correct product group
new_product.save
if !product.specification.nil?
product.specification.attributes.reject{|a| ['_id'].include?(a)}.each do |k,v|
NewSpecification.create(key: k, value: v, product_id: new_product.id)
end
end
end
puts "\n Working variations"
pg.products.each do |product|
puts "Working product #{product.id.to_s}"
product_attributes = %w(description code slug blurb tmv2 tmv3 wras antiscald ce_approved ce_approvals featured created_at updated_at accessories)
if NewProduct.find_by_slug(product.slug)
puts "Breaking..."
else
new_product = NewProduct.new
product_attributes.each do |product_attr|
value = product.send("#{product_attr}")
new_product.update_attribute("#{product_attr}",value)
end
new_product.old_id = product.id.to_s # Track old id
new_product.product_group_id = new_product_group.id # Assign to correct product group
new_product.save
p1 = NewProduct.find_by_old_id(product.variation_of)
unless p1.nil?
new_product.variation_of = p1.id
end
new_product.save
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment