-
-
Save colin/62747 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# legacy User class to import users from a vbulletion database | |
class LegacyUser < LegacyModel | |
# important! please change: | |
self.model = User # which is the new Rails model | |
self.table_name = "vbulletin_user" # the vbulletin table name | |
# attribue mapping: {:rails => :vbulletin} | |
self.attribute_mapping = {:legacy_id => :userid, :name => :username, :email => :email, :password => :email, :password_confirmation => :email} # map old vbulletin attrbute names to new rails attribute names {:rails => :vbulletin} | |
attr_accessor *self.attribute_mapping.values | |
#attr :attribute # any other important attributes? | |
# change this only if needed | |
self.database = "vbulletin_" | |
self.username = "root" | |
self.password = "" | |
self.host = "localhost" | |
def after_save(imported_object) | |
imported_object.register! | |
imported_object.activate! | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Idea of an Legacy Database Importer | |
# I've used this to import data from a vbulletin board to a Rails app | |
# This is a LeagacyModel class which handles your DB connection and the importin | |
# have a look at the ImportUser class for an usage example and some "documentation" | |
# | |
require "active_support" | |
require "logger" | |
require "mysql" | |
require "active_record" | |
MysqlCompat.define_all_hashes_method! | |
class LegacyModel | |
cattr_accessor :model, :attribute_mapping, :errors, :table_name, :database, :host, :username,:password, :logger | |
def initialize(attributes={}) | |
attributes.each do |key,value| | |
self.send("#{key}=",value) if self.respond_to?("#{key}=") | |
end | |
end | |
def import | |
import_object = self.class.model.new | |
attribute_mapping.each do |key,value| | |
import_object.send("#{key}=",self.send(value)) | |
end | |
import_object.save | |
if !import_object.new_record? | |
after_save(import_object) | |
else | |
self.class.error(import_object) | |
end | |
end | |
def after_save(import_object) | |
end | |
def self.import_all | |
self.all.each do |o| | |
o.import | |
end | |
end | |
def self.all | |
returning [] do |collection| | |
connection.select_all("select * from #{self.table_name}").each do |entry| | |
collection << self.new(entry) | |
end | |
end | |
end | |
def self.error(object) | |
@@errors ||= [] | |
@@errors << object | |
end | |
def self.map_errors_to(attribute) | |
@@errors.collect {|e| { e.send(attribute) => e.errors } } | |
end | |
def self.map_formatted_errors_to(attribute) | |
@@errors.collect {|e| { e.send(attribute) => e.errors.full_messages } } | |
end | |
def self.connection | |
@@connection ||= ActiveRecord::ConnectionAdapters::MysqlAdapter.new(Mysql.init, self.logger, [self.host, self.username, self.password, self.database, nil, nil], {:host => self.host, :user => self.username, :password=>self.password, :encoding=>"utf8"}) | |
end | |
def self.logger | |
@@logger ||= (Rails.logger || Logger.new(STDOUT)) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment