Skip to content

Instantly share code, notes, and snippets.

@tonkla
Created March 15, 2012 15:01
Show Gist options
  • Save tonkla/2044668 to your computer and use it in GitHub Desktop.
Save tonkla/2044668 to your computer and use it in GitHub Desktop.
Blog migration script. Migrate from MySQL to Markdown text file (Jekyll).
require 'rubygems'
require 'erb'
require 'mysql2'
require 'thor'
class Blogelf < Thor
desc 'migrate', 'Migrate from MySQL to Markdown text file (Jekyll)'
def migrate
Dir.mkdir('_posts') unless Dir.exist?('_posts')
db = Mysql2::Client.new(host: 'localhost', username: 'root', database: 'blogelf')
result = db.query("SELECT id, title, body, description, created_at FROM posts")
result.each do |r|
@title = r['title']
@body = r['body']
@created_at = r['created_at']
@description = r['description']
year = @created_at.strftime('%Y')
Dir.mkdir("_posts/#{year}") unless Dir.exist?("_posts/#{year}")
File.open("_posts/#{year}/#{@created_at.strftime('%Y-%m-%d')}-#{r['id']}.md", 'w') do |f|
f.write(ERB.new(File.read('post_template.erb')).result(binding))
end
end
db.close
end
end
---
layout: post
title: "<%= @title.gsub(/"/, '\"') %>"
description: "<%= @description.gsub(/"/, '\"') if @description %>"
date: <%= @created_at.strftime('%Y-%m-%d %H:%M:%S') %>
categories:
---
<%= @body %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment