Skip to content

Instantly share code, notes, and snippets.

@seapy
Created June 23, 2012 17:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seapy/2979190 to your computer and use it in GitHub Desktop.
Save seapy/2979190 to your computer and use it in GitHub Desktop.
Wordpress(MySQL) To Rails Blog(PostgreSQL) Migration Rake task edit
# encoding: UTF-8
require 'php_serialize'
# MySQL 접속 정보를 가지는 ActiveRecord 클래스 생성
class SeapyBlog < ActiveRecord::Base
end
# 기존 MySQL DB 접속 정보 설정
SeapyBlog.establish_connection(
:adapter => "mysql2",
:host => ENV['SEAPY_CAFE24_HOST'],
:username => ENV['SEAPY_CAFE24_USERNAME'],
:password => ENV['SEAPY_CAFE24_PASSWORD'],
:database => ENV['SEAPY_CAFE24_DATABASE'],
:encoding => "utf8"
)
@amazon_s3_img_base_url = 'https://s3-ap-northeast-1.amazonaws.com/seapy-blog/uploads/'
@seapy_img_base_url = 'http://seapy.com/wp-content/uploads/'
namespace :blog do
def get_post(post_id)
# 입력된 Post 정보 조회
post = SeapyBlog.connection.select_one "select * from wp_posts where post_type = 'post' and ID = #{post_id}"
{
:id => post['ID'],
:title => post['post_title'],
:content => post['post_content'].gsub(@seapy_img_base_url, @amazon_s3_img_base_url),
:date => post['post_date_gmt']
}
end
def get_all_post_id
# 전체 post 조회
SeapyBlog.connection.select_rows "select ID from wp_posts where post_type = 'post'"
end
desc "Migrate Post"
task :post_migrate, [:post_id] => [:environment] do |t, args|
#post_id = 1;
#입력된 post_id의 포스트 정보 가져오기
#포스트에 연결된 첨부파일 목록 가져오기
#기존 HTML 코드에서 첨부파일 링크를 S3 링크로 변경
# Post 마이그레이션 실행후 blog:bump_post_id 를 꼭 실행해야 한다. Post.id 의 시퀀스를 최대값으로 변경해주는 쿼리
post_dic = get_post(args[:post_id])
Post.new do |post|
post.id = post_dic[:id]
post.title = post_dic[:title]
post.content_html = post_dic[:content]
post.updated_at = post_dic[:date]
post.created_at = post_dic[:date]
post.save!
end
end
desc "Migrate Posts"
task :posts_migrate => :environment do
get_all_post_id.each do |post_id|
Rake::Task["blog:post_migrate"].reenable
Rake::Task["blog:post_migrate"].invoke(post_id[0])
end
Rake::Task["blog:bump_post_id"].invoke()
end
desc "Update Posts Id Sequence"
task :bump_post_id => :environment do
puts "Bump Post Id"
Post.connection.update_sql "SELECT setval('posts_id_seq', (SELECT MAX(id) FROM posts)+1)"
end
def get_comments(post_id)
# 입력된 Post의 댓글 정보 조회
SeapyBlog.connection.select_all "SELECT comment_author, comment_author_email, comment_author_url, comment_date_gmt, comment_content, comment_type FROM wp_comments WHERE comment_approved = '1' AND comment_post_ID = #{post_id}"
end
desc "Migrate Comment By Post"
task :comment_migrate, [:post_id] => [:environment] do |t, args|
comments = get_comments(args[:post_id])
comments.each do |old_comment|
Comment.new do |comment|
comment.post_id = args[:post_id]
comment.commenter = old_comment['comment_author']
comment.commenter_email = old_comment['comment_author_email']
comment.commenter_url = old_comment['comment_author_url']
comment.updated_at = old_comment['comment_date_gmt']
comment.created_at = old_comment['comment_date_gmt']
comment.body = old_comment['comment_content']
comment.comment_type = old_comment['comment_type']
comment.save!
end
end
end
desc "Migrate All Comments"
task :comments_migrate => :environment do
get_all_post_id.each do |post_id|
Rake::Task["blog:comment_migrate"].reenable
Rake::Task["blog:comment_migrate"].invoke(post_id[0])
end
end
desc "Update Post updated_at sync created_at"
task :sync_updated_at => :environment do
Post.all.each do |post|
post.updated_at = post.created_at
post.save!
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment