Skip to content

Instantly share code, notes, and snippets.

@NickClark
Created April 11, 2011 15:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NickClark/913689 to your computer and use it in GitHub Desktop.
Save NickClark/913689 to your computer and use it in GitHub Desktop.
this would be best optimized as a MYSQL script
#client is a MYSQL::Client object
client.query("SELECT * FROM local_event_attendance").each do |ea|
event = Event.find_by_orig_id(table_prefix + ea["eventID"].to_s + current_import_year.year.to_s).try(:id)
person = Person.find_by_orig_id(table_prefix + ea["memberID"].to_s).try(:id)
Registration.create(
:event_id => event,
:person_id => person,
:created_at => ea["date"],
:uniform => ea["uniform"].to_s.downcase == "yes"
)
end
use old_database;
INSERT INTO new_database.registrations (created_at, uniform, event_id, person_id)
SELECT date AS created_at, attendance.uniform = 'yes' AS uniform,
new_database.events.id AS event_id,
new_database.people.id AS person_id
FROM attendance
JOIN new_database.events on new_database.events.orig_id = CONCAT('something', eventID, '2010')
JOIN new_database.people on new_database.people.orig_id = CONCAT(memberID, 'something');
//Turns out, I need to normalize the column first. Also, not shown here, orig_id on both events and people need to be added as an index, other wise performance is not really improved.
use pathfinders;
DROP TEMPORARY TABLE IF EXISTS attendance_tmp;
CREATE TEMPORARY TABLE attendance_tmp
SELECT date AS created_at, uniform = 'yes' as uniform, CONCAT('', eventID, '2010') as eventID, CONCAT('', memberID) as memberID
FROM local_event_attendance;
INSERT INTO clubs_development.registrations (created_at, uniform, event_id, person_id)
SELECT attendance_tmp.created_at, attendance_tmp.uniform,
clubs_development.events.id AS event_id,
clubs_development.people.id AS person_id
from attendance_tmp
join clubs_development.events on clubs_development.events.orig_id = eventID
join clubs_development.people on clubs_development.people.orig_id = memberID;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment