Move more stuff in to DataConversion::Base, refactor method names. Add spec for importer.

This commit is contained in:
Sarah Mei 2011-01-04 20:28:10 -08:00
parent 7453130c63
commit 55191c6838
4 changed files with 39 additions and 18 deletions

View file

@ -16,5 +16,13 @@ module DataConversion
end
Rails.logger.debug(message) if Rails.logger
end
def export_directory
"tmp/export-for-mysql"
end
def export_path
"#{Rails.root}/#{export_directory}"
end
end
end

View file

@ -21,18 +21,10 @@ module DataConversion
:force_quotes => false}
end
def dirname
"export-for-mysql"
end
def dirpath
"#{Rails.root}/#{dirname}"
end
def clear_dir
`rm -rf #{dirpath}`
`mkdir -p #{dirpath}/json`
`mkdir -p #{dirpath}/csv`
`rm -rf #{export_path}`
`mkdir -p #{export_path}/json`
`mkdir -p #{export_path}/csv`
end
def db_name
@ -72,10 +64,10 @@ module DataConversion
log "Starting JSON export..."
models.each do |model|
log "Starting #{model[:name]} JSON export..."
filename ="#{dirpath}/json/#{model[:name]}.json"
filename ="#{export_path}/json/#{model[:name]}.json"
model[:json_file] = filename
`#{json_for_model(model[:name])} > #{filename}`
log "Completed #{model[:name]} JSON export to #{dirname}/json/#{model[:name]}.json."
log "Completed #{model[:name]} JSON export to #{export_directory}/json/#{model[:name]}.json."
end
log "JSON export complete."
end
@ -90,7 +82,7 @@ module DataConversion
log "Converting #{model_hash[:name]} json to csv"
json_file = File.open(model_hash[:json_file])
csv = CSV.open("#{dirpath}/csv/#{model_hash[:name]}.csv", 'w')
csv = CSV.open("#{export_path}/csv/#{model_hash[:name]}.csv", 'w')
csv << model_hash[:attrs]
json_file.each do |aspect_json|
@ -148,10 +140,10 @@ module DataConversion
log "Converting #{model_hash[:name]} json to csv"
json_file = File.open(model_hash[:json_file])
people_csv = CSV.open("#{dirpath}/csv/#{model_hash[:name]}.csv", 'w')
people_csv = CSV.open("#{export_path}/csv/#{model_hash[:name]}.csv", 'w')
people_csv << model_hash[:attrs]
profiles_csv = CSV.open("#{dirpath}/csv/profiles.csv", 'w')
profiles_csv = CSV.open("#{export_path}/csv/profiles.csv", 'w')
profiles_csv << model_hash[:profile_attrs]
json_file.each do |aspect_json|
@ -217,10 +209,10 @@ module DataConversion
log "Converting #{model_hash[:name]} json to two csvs"
json_file = File.open(model_hash[:json_file])
main_csv = CSV.open("#{dirpath}/csv/#{model_hash[:name]}.csv", 'w')
main_csv = CSV.open("#{export_path}/csv/#{model_hash[:name]}.csv", 'w')
main_csv << model_hash[:main_attrs]
join_csv = CSV.open("#{dirpath}/csv/#{model_hash[:join_table_name]}.csv", 'w')
join_csv = CSV.open("#{export_path}/csv/#{model_hash[:join_table_name]}.csv", 'w')
join_csv << model_hash[:join_table_attrs]
json_file.each do |aspect_json|

View file

@ -0,0 +1,8 @@
# Copyright (c) 2010, Diaspora Inc. This file is
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
module DataConversion
class ImportToMysql < DataConversion::Base
end
end

View file

@ -0,0 +1,13 @@
# Copyright (c) 2010, Diaspora Inc. This file is
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
require 'spec_helper'
Dir.glob(File.join(Rails.root, 'lib', 'data_conversion', '*.rb')).each { |f| require f }
describe DataConversion::ImportToMysql do
before do
@migrator = DataConversion::ImportToMysql.new
end
end