require 'optparse' class TXPMigrate attr_accessor :options def initialize self.options = {} self.parse_options self.convert_entries end def convert_entries txp_entries = ActiveRecord::Base.connection.select_all(%{ SELECT ID, Title AS title, Body AS body, Posted AS created_at, (CASE Status WHEN '4' THEN "t" ELSE "f" END) AS active, FROM '#{self.options[:txp_db]}'.textpattern }) puts "Converting #{txp_entries.size} entries.." txp_entries.each do |entry| a = Article.new # a.attributes = entry.reject { |k,v| k =~ /^(Category|ID)/ } a.save end end def parse_options OptionParser.new do |opt| opt.banner = "Usage: textpattern.rb [options]" opt.on('--db DBNAME', String, 'Text Pattern database name.') { |d| self.options[:txp_db] = d } opt.on_tail('-h', '--help', 'Show this message.') do puts opt exit end opt.parse!(ARGV) end unless self.options.include?(:txp_db) puts "See textpattern.rb --help for help." exit end end end TXPMigrate.new
You need to create an account or log in to post comments to this site.