Skip to content

Instantly share code, notes, and snippets.

@JGrubb
Created July 3, 2012 19:21
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save JGrubb/3042143 to your computer and use it in GitHub Desktop.
Save JGrubb/3042143 to your computer and use it in GitHub Desktop.
Migrating from Drupal to Rails blog
# This uses the Sequel gem to do the heavy lifting of connecting together the various
# DBs involved in this process. It was really difficult 2 weeks ago, and really easy yesterday.
# I love that.
require 'sequel'
require 'date'
# The Drupal DB
DB = Sequel.connect(
:adapter => 'mysql',
:host => 'localhost',
:user => 'root',
:password => 'root',
:database => 'ibd'
)
#The target Rails_dev DB
DB2 = Sequel.connect(
:adapter => 'mysql',
:host => 'localhost',
:user => 'root',
:password => 'root',
:database => 'blog_development'
)
#A copy of my currently live production Rails DB
DB3 = Sequel.connect(
:adapter => 'mysql',
:host => 'localhost',
:user => 'root',
:password => 'root',
:database => 'blog_production'
)
#datasets
node = DB[:node]
revisions = node.join(:node_revisions, :vid => :vid).where(:type => 'blog')
slugs = DB[:url_alias].where(:src.like('node%'))
posts = DB2[:posts]
new_posts = DB3[:posts]
#starting from a blank DB, so I grab User 1 - me - from the rails_production DB
DB2[:users].insert(DB3[:users].first)
# Go ahead and move existing production posts into the new, combined DB
new_posts.each do |post|
DB2[:posts].insert(:id => post[:id], :title => post[:title], :body => post[:body], :created_at => post[:created_at], :updated_at => post[:updated_at], :slug => post[:slug], :published => post[:published])
#puts post[:updated_at]
end
# I'm migrating a domain in Disqus, so I go ahead and set this up to create the
# migrate.csv file that disqus requires to move my comments.
File.open('migrate.csv', 'w') do |f|
# revisions table is where Drupal stores the actual content for your nodes
revisions.each do |item|
# get attributes
created_at = Time.at(item[:created]).to_datetime
updated_at = Time.at(item[:changed]).to_datetime
title = item[:title]
id = item[:nid]
body = item[:body]
published = item[:status]
# slug is hash/row from the Drupal DB. Trying make the move less painful.
slug = slugs.first(:src => "node/#{id}")
# actually inserting the posts. Adding 20 to the id to compensate for the
# other blog posts from the production DB
posts.insert(:id => "#{id.to_i + 20}", :title => title, :body => body, :published => published, :slug => slug[:dst][8..-1], :created_at => created_at, :updated_at => updated_at)
# some slugs come back blank, which fails. This is inserting the slugs into the
# friendly_id_slugs table so that the history option will work correctly.
# I shouldn't have to do this, but it was easier than trying to chase down the
# friendly_id bug.
if slug[:dst][8..-1]
DB2[:friendly_id_slugs].insert(:slug => slug[:dst][8..-1], :sluggable_id => "#{id.to_i + 20}", :sluggable_type => 'Post', :created_at => Time.now)
end
# write the line to the migrate.csv
f.puts "http://ignoredbydinosaurs.com/#{slug[:dst]}, http://www.johnnygrubb.com/posts/#{slug[:dst][8..-1]}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment