Migrating from Typo to Simplelog
Posted by Craig Ambrose on May 17, 2007 at 11:19 PM
I’ve moved this blog from Typo, to Simplelog, both rails based blogging systems. Normally I don’t do this sort of meta-blogging, but I though I’d share a few tips about the migration, and some comments on Simplelog.
Installation
Like the other rails blogs, simplelog has some installation instructions for how to get the simplelog files on your sever, and like all rails developers, I promptly ignored them, and setup a copy of simplelog in a subversion repository of my own and deployed to my server using capistrano. This makes deployment and upgrading much easier, as I can test new simplelog versions on my local machine first, and then roll out updates using cap deploy as normal.
Migrating your Data
There don’t seem to be any nice scripts for migrating data from Typo to Simplelog. There are some scripts for exporting typo in data into Moveable Type or word press format, and you could probably hook together two or three different scripts to eventually get from Typo to Simplelog, but I couldn’t figure out a simple way to do that without also installing a PHP app, and that just seemed way too complex, so I thought I’d roll my own.
The bad news I’m afraid, is that I haven’t bothered to sufficiently generalise it to make it work for everyone, but if you want to do this too, then you can use my code as a starting point and refine it for your needs.
When you want to script some little task in rails, the tool for the job is of course rake. I started working on a little rake task inside my typo installation, to export the data in a format I could use for simplelog. I started by comparing both database schemas, and seeing how I could generate SQL to use with simplelog.
It turns out that both these databases are non-trivial. They use single-table inheritance, and the also both have cache columns full of keywords and search text and so forth. The active record classes in both apps have the code to do this, so the best way to convert your data is to leverage that code. You could write a ruby app that loads both model layers and connects to both database, but that didn’t seem too trivial, so what I ended up doing was creating a typo rake export task that generated a simplelog rake import task.
The Code
Here’s the code, it only copies the data I was interested in (articles and comments), and it does so with a few specific values (such as a fixed author id). It also, on large databases, will hit some id conflicts as it does so. To fix this, run it multiple times until the auto-numbered ids become bigger than the ids in your old database (or solve that problem properly). Also, it’s butt ugly, remember, I just wanted to do this in an hour and never see it again.
Before running this script, make sure that you have setup your simplelog database (with the migrations), and created a user (my had id 2, which is hardcoded in the script). Also, set the default markup format in simplelog to be whatever you used in typo (I was using textile).
def q(value)
value = '' if value.nil?
if value.is_a? String
escaped = value.gsub(/\\/, '\\\\').gsub(/\r\n/, "\n").gsub(/'/, "\\\\'").gsub(/\n/, '\' + "\\\\n" + \'')
return "'#{escaped}'"
end
value
end
task :typo_export => [:environment] do
file = File.new("/path_to_craigs_simplelog/lib/tasks/import_from_typo.rake", 'w') file << "task :import_from_typo => [:environment] do \n"
file << " require 'find'\n"
file << " require 'post'\n"
file << " require 'preference'\n"
file << " require 'application'\n" file << " Post.delete_all\n"
file << " update_posts = \"\"\n"
for article in Article.find(:all)
file << " new_record = Post.create(:author_id => 2, :created_at => '#{article.created_at.to_s(:db)}'.to_time, :modified_at => '#{article.updated_at.to_s(:db)}'.to_time, :permalink => #{q article.permalink}, :title => #{q article.title}, :body_raw => #{q article.body}, :comment_status => 1, :text_filter => #{q 'textile'})\n"
file << " update_posts += \"UPDATE posts SET id = #{article.id} WHERE id = \#{new_record.id}; \"\n\n"
end
file << " Post.connection.execute update_posts\n\n" file "\n\n"
file << " Comment.delete_all\n"
file << " update_comments = \"\"\n" for comment in Comment.find(:all)
file << " rew_record = Comment.create(:post_id => #{comment.article.id}, :name => #{q comment.author}, :email => #{q (comment.email.blank? ? 'anon@noemail.com' : comment.email)}, :subject => '', :body_raw => #{q comment.body}, :ip => #{q comment.ip}, :is_approved => 1, :url => #{q comment.url})\n"
file << " update_comments += \"UPDATE comments SET id = #{comment.id}, is_approved = 1 WHERE id = \#{new_record.id}\"\n\n"
end
file << " Post.connection.execute update_comments\n\n" file << "end\n"
end
Once this is run, you can go to the simplelog installation and run rake import_from_typo. If it doesn’t work, figure out what went wrong, modify the script, and repeat. Please notice that this is a destructive import (note the calls to “delete_all”).
This is a long post already, so I’ll post comments on simplelog next time.

Comments
There are 16 comments on this post. Post yours →
Nice writeup Craig. I’m curious, why didn’t you go with something like Mephisto? Was it just a little too much for what you needed?
Simplelog does look pretty solid though.
Craig, thansk a lot for the code.
Very easy to understand.
Peter
I can do that, but whats wrong when we tall about music. Many mans in the world not eat every day.
hi
outdoor water fountain | swimming pool equipment | inground swimming pool | swimming pool cleaner | fiberglass pool | inground pool cover | swimming pool filter | swimming pool heater
Thanks for the info.
This is a great post … I am wondering, could you possibly help me with reversing the process?
I am trying to move my blog content OUT of Simplelog and into Wordpress and can’t find an easy way. I basically figure if I can get my content from SL to Typo, I can use these (http://blog.ifbydesign.com/2006/06/26/automatic-migration-from-typo-to-wordpress/) instructions to get the content into WP.
Any help would be greatly appreciated!
you really explain about those topic
Thanks for this and for including the screen shoots.
Cool post – I wondered if there was a simply way to do this. Thanks this will be really helpful
Thanks for this – I will try this out
I am glad you posted this thanks alot
What do you store in your ./public/system folder that you need to rsync back and forth for a DB backup??
thank you big …
I can do that, but whats wrong when we tall about music. Many mans in the world not eat every day.
I basically figure if I can get my content from SL to Typo, I can use these (http://blog.ifbydesign.com/2006/06/26/automatic-migration-from-typo-to-wordpress/) instructions to get the content into WP.
Post a comment
Required fields in bold.