Skip to content

Instantly share code, notes, and snippets.

@oscarsan
Created June 11, 2016 16:37
Show Gist options
  • Save oscarsan/ec3d8364a61e9b6e172c9f40f2742df1 to your computer and use it in GitHub Desktop.
Save oscarsan/ec3d8364a61e9b6e172c9f40f2742df1 to your computer and use it in GitHub Desktop.
rake to dump woocommerce products onto mongodb
require 'Mysql2'
require 'json'
require 'mongo'
namespace :db do
task :backup do
client = Mysql2::Client.new(:host => "localhost", :socket => '/Applications/MAMP/tmp/mysql/mysql.sock', :username => "root", :database => "elrinconecologico_db", :password => "root")
mongo_client = Mongo::Client.new('mongodb://127.0.0.1:27017/database')
products = client.query("SELECT * FROM wp_posts where post_type='product' and post_name <> ''")
products.each do |product|
product_meta = client.query("SELECT * FROM wp_postmeta where post_id=#{product['ID']}")
temp_hash = Hash.new
product_meta.each do |meta|
temp_hash[meta['meta_key']] = meta['meta_value']
end
product['post_meta'] = temp_hash
mongo_client[:products].insert_one(product['post_meta'])
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment