Skip to content

Instantly share code, notes, and snippets.

@ouyangzhiping
Created June 14, 2012 01:32
Show Gist options
  • Save ouyangzhiping/2927606 to your computer and use it in GitHub Desktop.
Save ouyangzhiping/2927606 to your computer and use it in GitHub Desktop.
wordpress:migrate_wordpress
# -*- coding:utf-8 -*-
# Usage rake wordpress:migrate_wordpress[:src, :dst]
# rake wordpress:migrate_wordpress
# You will need gem "mysql" in your Gemfile
namespace :wordpress do
task :migrate_wordpress, [:src, :dst] => :environment do |cmd, args|
args.with_defaults(:src => "wordpress", :dst => "development")
config = Rails.configuration.database_configuration
src_db = config[args[:src]]
dst_db = config[args[:dst]]
conn = ActiveRecord::Base.establish_connection src_db
posts = conn.connection.execute "SELECT * FROM wp_posts where post_status='publish' and post_type='post'"
conn = ActiveRecord::Base.establish_connection dst_db
posts.each do |post|
title = post[5].force_encoding 'utf-8'
content = post[4].force_encoding 'utf-8'
created_at = post[2]
updated_at = post[15]
draft = post[8]
author = post[1]
excerpt =post[7]
slug = post[19]
if draft.end_with? "draft"
draft_flag = true
elsif draft.end_with? "publish"
draft_flag = false
end
p = Post.create! :title => title, :content => content, :slug => slug ,
:created_at => created_at, :updated_at => updated_at, :draft => draft_flag,
:author => author, :excerpt => excerpt
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment