Skip to content

Instantly share code, notes, and snippets.

@tonkla
Created November 1, 2011 16:32
Show Gist options
  • Save tonkla/1331026 to your computer and use it in GitHub Desktop.
Save tonkla/1331026 to your computer and use it in GitHub Desktop.
Thor script is used to migrate blog posts from Drupal to Rails
#! /usr/bin/env thor
require 'mysql2'
class Tonkla < Thor
desc 'migrate', 'migrate tonkla.com\'s blog posts from Drupal to Rails'
def migrate
db_from = Mysql2::Client.new(host: 'localhost', username: 'root', database: 'tonkla_com')
db_to = Mysql2::Client.new(host: 'localhost', username: 'root', database: 'klass_blog_development')
result = db_from.query('SELECT r.title, r.body, n.created AS created_at, r.timestamp AS updated_at
FROM dp_node n JOIN dp_node_revisions r
ON n.nid = r.nid
ORDER BY n.created')
result.each do |r|
r.update({
'published' => true,
'published_at' => Time.now,
'user_id' => 1,
'created_at' => Time.at(r['created_at']),
'updated_at' => Time.at(r['updated_at'])
})
db_to.query("INSERT INTO posts (title, body, published, published_at, user_id, created_at, updated_at)
VALUES ('%s', '%s', %s, '%s', %d, '%s', '%s')" %
[db_to.escape(r['title']), db_to.escape(r['body']), r['published'],
r['published_at'], r['user_id'], r['created_at'], r['updated_at']])
end
db_from.close
db_to.close
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment