Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bjensen/159347 to your computer and use it in GitHub Desktop.
Save bjensen/159347 to your computer and use it in GitHub Desktop.
require "rubygems"
require "activerecord"
module New
class NewDatabase < ActiveRecord::Base
establish_connection(
:adapter => "mysql",
:encoding => "utf8")
end
class Profile < NewDatabase
belongs_to :student, :foreign_key => "user_id"
set_table_name "profiles"
validates_numericality_of :postal_code, :allow_blank => true
validates_numericality_of :phone, :allow_blank => true
validates_numericality_of :mobile, :allow_blank => true
validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :allow_blank => true
validates_presence_of :name
validates_length_of :postal_code, :is => 4, :allow_blank => true
validates_length_of :mobile, :is => 8, :allow_blank => true
validates_length_of :phone, :is => 8, :allow_blank => true
end
class Student < NewDatabase
has_one :profile, :foreign_key => "user_id"
has_many :stays
set_table_name "users"
validates_length_of :login, :is => 10
validates_numericality_of :login, :only_integer => true
end
class Workshop < NewDatabase
need to get the right attendances in dvs
tilpasese vaerksedsnavne saa vi kan bruge findbyname
end
class StatusMeeting < NewDatabase
belongs_to :stay
self.record_timestamps = false
set_table_name "status_meetings"
validates_presence_of :text
#validates_presence_of :creater_id
end
class WorkshopAttendance < NewDatabase
set_table_name "attendances"
belongs_to :stay
end
class Stay < NewDatabase
belongs_to :student
has_many :workshop_attendances
has_many :status_meetings
set_table_name "stays"
validates_presence_of :start_date
validates_presence_of :end_date
end
end
module Old
class OldDatabase < ActiveRecord::Base
establish_connection(
:adapter => "mysql",
:encoding => "utf8",
:database => "forlobsplaner2")
end
class Persopl < OldDatabase
set_table_name "persopl"
end
class Samtale < OldDatabase
set_table_name "samtaler"
end
class Andet < OldDatabase
set_table_name "andet"
end
class WorkshopSelection < OldDatabase
set_table_name "vaerkstedvalg"
end
class Userinfo < OldDatabase
set_table_name "phpSP_users"
end
end
debug = false
@skipped_students = 0
@my_login_array = Array.new
@trouble_students = Array.new
@doubles = Array.new
@convs_collected = 0;
@students = Old::Persopl.find :all
@cpr_numbers = Array.new
#if false
@students.each do |student|
begin
#verificer at det er et gyldigt cprnr
@cpr = student.cprnr[0..9]
#if cpr_valid?(@cpr) #kig paa dit tidl validerings script
if @my_login_array.include?(@cpr)
puts "#{student.personid} The student already in #{student.fornavn} #{student.efternavn} cpr #{@cpr}" if debug
@doubles << @cpr
puts "duplicate #{@cpr}"
@new_student = New::Student.find_by_login @cpr
else
puts "#{student.personid} Creating student #{student.fornavn} #{student.efternavn} cpr #{@cpr}" if debug
@my_login_array << @cpr
@userinfo = Old::Userinfo.find_by_primary_key(student.personid)
@new_student = New::Student.new
@new_student.login = @cpr
@new_student.crypted_password = "a673163edb52dbceb06714943ac816049adf2dece80502891c777543d4cd7e58c8ccf991ccad79f8ab4f3a25c59748e7ecb7d9a267b49cfb630773c409061d78"
@new_student.password_salt = "h9Qoq7szEeb6_zjUOrO9"
@new_student.persistence_token = ''
@new_student.single_access_token = ''
@new_student.state = "missing_background"
@new_student.perishable_token = ''
@new_student.login_count = 0
@new_student.failed_login_count = 0
puts "gyldig ny student" if @new_student.valid? and debug
end
#Create a new profile
@profile = @new_student.build_profile
@profile.name = "#{student.fornavn} #{student.efternavn}".gsub(/\w+/) {|w| w.capitalize}
@profile.address = student.adresse
@profile.phone = student.fastnettlf
@profile.mobile = student.mobiltlf
@profile.postal_code = student.postnr
@profile.city = student.by
@profile.email = student.email
puts "gyldig efter profile? #{@new_student.valid?}" if debug
@andet = Old::Andet.find_by_id! student.personid
@stay = @new_student.stays.build
@stay.start_date = student.startdato
if @andet.slutdato.present?
@stay.end_date = @andet.slutdato
else
puts "Vi mangler slutdato"
@stay.end_date = "2020-01-01"
end
puts "gyldig efter stay? #{@new_student.valid?} #{@stay.start_date} #{@stay.end_date}" if debug
@samtaler = Old::Samtale.find_all_by_id student.personid
@samtaler.each do |samtale|
@meeting = @stay.status_meetings.build
@meeting.text = samtale.tekst
@meeting.created_at = samtale.dato
@meeting.updated_at = samtale.dato
@meeting.creater_id = 1 #We will make the admin the user with id = 1
end
puts "gyldig efter statusmøder? #{@new_student.valid?} errors: #{@new_student.errors.full_messages}" if debug
@current_ws = Old::WorkshopSelection.find_by_id(student.personid) #Værkstedet er nok nedlagt
if @current_ws.present?
puts "Found a current workshop id: #{@current_ws.tildeltevaerksted}" if debug
@workshop_attendance = @stay.workshop_attendances.build
@workshop_attendance.start_date = student.startdato
@workshop_attendance.end_date = @andet.slutdato
@workshop_attendance.reason = "Ingen givet(gl. system)"
end
puts "gyldig efter ws attend? #{@new_student.valid?} errors: #{@new_student.errors.full_messages}" if debug
if(@new_student.valid?)
#@new_student.save
puts "saving"
@new_student.save
else
puts "Did not save the student #{student.personid}"
@trouble_students << student.personid
@my_login_array.delete(@cpr)
end
rescue Exception => e
puts "Skipping a student: #{e} #{student.personid}"
@skipped_students += 1
@my_login_array.delete(@cpr)
end
#Do the @new_student.save down here. We do a build the other places
end
puts "how many uniq users: #{@my_login_array.size} doubles: #{@doubles.size} skipped #{@skipped_students} samtaler: #{@convs_collected}"
puts "trouble studnets: #{@trouble_students.join(", ")}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment