Guest User

Untitled

a guest
Feb 21st, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.84 KB | None | 0 0
  1. # ISSUE: Clinician ID conflicts with KCH?
  2. # ISSUE: Right now we assume there is no patient data in OpenMRS. If there were, we would need to worry about merging...
  3. # ISSUE: None of the migrated patients have a new national id. The check digit algorithm thwarted us. Update this in Ruby.
  4. # ISSUE: Should we add a Clinician Role for the Clinician users? What about Provider?
  5. # see ISSUE below
  6. # see FIRSTRUN below
  7.  
  8. # Tables to be incorporated in the concept dictionary (no SQL migration required)
  9. # 1. arv_arvs | concept, drug
  10. # 2. arv_stages | concept
  11. # 3. arv_stage_defining_conditions | concept
  12. #
  13. # Other tables (SQL migration required):
  14. # 1. Clinician | users
  15. # 2. Site | location - use existing locations?
  16. # 3. MasterPatientRecord | patient, patient_name, patient_identifier
  17. # 4. arv_hiv_testing_centers | location - use existing locations?
  18. # 5. arv_patients | encounter, patient, patient_identifier
  19. # 6. arv_patient_arvs | obs
  20. # 7. arv_patient_stage_defining_conditions | obs
  21. # 8. arv_visits | encounter, obs
  22.  
  23. # FIRSTRUN: ALTER TABLE qech.Site ADD COLUMN OpenMRSSiteID INT(11);
  24. # FIRSTRUN: ALTER TABLE qech.arv_hiv_testing_centers ADD COLUMN OpenMRSSiteID INT(11);
  25. # FIRSTRUN: INSERT INTO qech.Clinician (Clinician_F_Name, Clinician_L_Name, Clinician_ID) VALUES ("", "", 222222);;
  26. # FIRSTRUN: UPDATE MasterPatientRecord SET Site_ID = 101 WHERE (Site_ID <> 101 AND Site_ID <> 102);;
  27. # DELETE FROM patient_identifier;
  28. # DELETE FROM patient_name;
  29. # DELETE FROM patient_address;
  30. # DELETE FROM relationship;
  31. # DELETE FROM person;
  32. # DELETE FROM obs;
  33. # DELETE FROM encounter;
  34. # DELETE FROM patient;
  35. class Migrate
  36.  
  37. def self.migrate()
  38.  
  39. ActiveRecord::Base.connection.execute("UNLOCK TABLES")
  40.  
  41. # Set up the defaults so that the foreign keys are okay...
  42. User.current_user = User.find_by_username "migration"
  43. Location.current_location = Location.find_by_name "Baobab Health Programming Room"
  44.  
  45. # Migrate the Sites
  46. # We assign the OpenMRSSiteID in qech to simplify the migration later
  47. kch_location = Location.find_by_name("Kamuzu Central Hospital")
  48. qech_location = Location.find_by_name("Queen Elizabeth Central Hospital")
  49. pih_location = Location.find_by_name("Moyo Clinic")
  50. ActiveRecord::Base.connection.update("UPDATE qech.Site SET OpenMRSSiteID = #{kch_location.id} WHERE Description = 'Kamuzu Central Hospital'")
  51. ActiveRecord::Base.connection.update("UPDATE qech.Site SET OpenMRSSiteID = #{qech_location.id} WHERE Description = 'Queen Elizabeth Central Hospital'")
  52. ActiveRecord::Base.connection.update("UPDATE qech.Site SET OpenMRSSiteID = #{pih_location.id} WHERE Description = 'Partners In Hope'")
  53.  
  54. # Migrate the arv_hiv_testing_centers | location
  55. # We assign the OpenMRSSiteID in qech to simplify the migration later
  56. # HIV testing centers map existing ids to new openmrs ids in migration of arv_patients
  57. # | id | name | openmrs location name (and details)
  58. # +____+____________________________________________________________________
  59. # | 1 | MACRO | "MACRO" (city:Blantyre)
  60. # | 2 | Lepra VCT | "Lepra" (desc:Voluntary Counseling and Testing, parent:QECH)
  61. # | 3 | BAH - Blantyre Adventist Hospital | "Adventist Hospital" (city:Blantyre)
  62. # | 4 | Tiyanjane | "Tiyanjane"
  63. # | 5 | Napham | "Napham"
  64. # | 6 | Unknown | "Unknown"
  65. # | 7 | College of Medicine | "College of Medicine" (parent:QECH)
  66. # | 8 | QECH | "Queen Elizabeth Central Hospital"
  67. # | 9 | Lighthouse | "Lighthouse"
  68. # | 10 | Mwaiwathu | "Mwaiwathu" (parent:QECH)
  69. # | 11 | John Hopkins | "John Hopkins"
  70. # | 12 | Other | "Other"
  71. # +____+____________________________________________________________________
  72. ActiveRecord::Base.connection.update("UPDATE qech.arv_hiv_testing_centers \
  73. SET OpenMRSSiteID = #{Location.find(:first, :conditions => ["name = ? AND city_village = ?", "MACRO", "Blantyre"]).id} \
  74. WHERE name = 'MACRO'")
  75. ActiveRecord::Base.connection.update("UPDATE qech.arv_hiv_testing_centers \
  76. SET OpenMRSSiteID = #{Location.find(:first, :conditions => ["name = ?", "Lepra"]).id} \
  77. WHERE name = 'Lepra VCT'")
  78. ActiveRecord::Base.connection.update("UPDATE qech.arv_hiv_testing_centers \
  79. SET OpenMRSSiteID = #{Location.find(:first, :conditions => ["name = ? AND city_village = ?", "Adventist Hospital", "Blantyre"]).id} \
  80. WHERE name = 'BAH'")
  81. ActiveRecord::Base.connection.update("UPDATE qech.arv_hiv_testing_centers \
  82. SET OpenMRSSiteID = #{Location.find(:first, :conditions => ["name = ?", "Tiyanjane"]).id} \
  83. WHERE name = 'Tiyanjane'")
  84. ActiveRecord::Base.connection.update("UPDATE qech.arv_hiv_testing_centers \
  85. SET OpenMRSSiteID = #{Location.find(:first, :conditions => ["name = ?", "Napham"]).id} \
  86. WHERE name = 'Napham'")
  87. ActiveRecord::Base.connection.update("UPDATE qech.arv_hiv_testing_centers \
  88. SET OpenMRSSiteID = #{Location.find(:first, :conditions => ["name = ?", "Unknown"]).id} \
  89. WHERE name = 'Unknown'")
  90. ActiveRecord::Base.connection.update("UPDATE qech.arv_hiv_testing_centers \
  91. SET OpenMRSSiteID = #{Location.find(:first, :conditions => ["name = ?", "College of Medicine"]).id} \
  92. WHERE name = 'College of Medicine'")
  93. ActiveRecord::Base.connection.update("UPDATE qech.arv_hiv_testing_centers \
  94. SET OpenMRSSiteID = #{Location.find(:first, :conditions => ["name = ?", "Queen Elizabeth Central Hospital"]).id} \
  95. WHERE name = 'QECH'")
  96. ActiveRecord::Base.connection.update("UPDATE qech.arv_hiv_testing_centers \
  97. SET OpenMRSSiteID = #{Location.find(:first, :conditions => ["name = ?", "Lighthouse"]).id} \
  98. WHERE name = 'Lighthouse'")
  99. ActiveRecord::Base.connection.update("UPDATE qech.arv_hiv_testing_centers \
  100. SET OpenMRSSiteID = #{Location.find(:first, :conditions => ["name = ?", "Mwaiwathu"]).id} \
  101. WHERE name = 'Mwaiwathu'")
  102. ActiveRecord::Base.connection.update("UPDATE qech.arv_hiv_testing_centers \
  103. SET OpenMRSSiteID = #{Location.find(:first, :conditions => ["name = ?", "John Hopkins"]).id} \
  104. WHERE name = 'John Hopkins'")
  105. ActiveRecord::Base.connection.update("UPDATE qech.arv_hiv_testing_centers \
  106. SET OpenMRSSiteID = #{Location.find(:first, :conditions => ["name = ?", "Other"]).id} \
  107. WHERE name = 'Other'")
  108.  
  109. # Migrate the Clinicians
  110. ActiveRecord::Base.connection.select_all("SELECT * FROM qech.Clinician").each { |clinician|
  111. migrate_clinician_to_user(clinician)
  112. }
  113.  
  114. legacy_nat = PatientIdentifierType.find(:first, :conditions => ["name = ?", "legacy national id"])
  115. legacy_ped = PatientIdentifierType.find(:first, :conditions => ["name = ?", "legacy pediatric id"])
  116. birth_ta = PatientIdentifierType.find(:first, :conditions => ["name = ?", "birth traditional authority"])
  117. mprs = ActiveRecord::Base.connection.select_all("SELECT * FROM qech.MasterPatientRecord")
  118.  
  119. @@start = Patient.count()
  120.  
  121. ActiveRecord::Base.connection.execute("ALTER TABLE `patient` DISABLE KEYS")
  122. ActiveRecord::Base.connection.execute("ALTER TABLE `patient_identifier` DISABLE KEYS")
  123. ActiveRecord::Base.connection.execute("ALTER TABLE `patient_name` DISABLE KEYS")
  124. ActiveRecord::Base.connection.execute("ALTER TABLE `patient_address` DISABLE KEYS")
  125.  
  126. # ActiveRecord::Base.connection.execute("LOCK TABLES `patient` WRITE")
  127. # ActiveRecord::Base.connection.execute("LOCK TABLES `patient_identifier` WRITE")
  128. # ActiveRecord::Base.connection.execute("LOCK TABLES `patient_name` WRITE")
  129. # ActiveRecord::Base.connection.execute("LOCK TABLES `patient_address` WRITE")
  130.  
  131. @@bi = ""
  132. @@count = 0
  133. mprs.each { |mpr|
  134. @@count = @@count + 1
  135. migrate_master_patient_record_to_patient(mpr, legacy_nat, legacy_ped, birth_ta)
  136. }
  137.  
  138. # ActiveRecord::Base.connection.execute("UNLOCK TABLES")
  139. ActiveRecord::Base.connection.execute("ALTER TABLE `patient` ENABLE KEYS")
  140. ActiveRecord::Base.connection.execute("ALTER TABLE `patient_identifier` ENABLE KEYS")
  141. ActiveRecord::Base.connection.execute("ALTER TABLE `patient_name` ENABLE KEYS")
  142. ActiveRecord::Base.connection.execute("ALTER TABLE `patient_address` ENABLE KEYS")
  143.  
  144. return "Migration complete"
  145. end
  146.  
  147.  
  148. def self.migrate_clinician_to_user(clinician)
  149.  
  150. # Feedback
  151. puts ":Migrating Clinician " + clinician["Clinician_F_Name"] + " " + clinician["Clinician_L_Name"] + " (" + clinician["Clinician_ID"] + ")\n"
  152.  
  153. # Try to see if this user already exists
  154. user = User.find_by_username(clinician["Clinician_ID"])
  155.  
  156. # If not add the clinician as a user
  157. if (user.nil?)
  158. user = User.new
  159. user.first_name = clinician["Clinician_F_Name"]
  160. user.last_name = clinician["Clinician_L_Name"]
  161. user.username = clinician["Clinician_ID"]
  162. user.password = clinician["Clinician_ID"]
  163. user.date_created = DateTime.now
  164. user.save
  165. end
  166.  
  167. # If the user is there, update the last used
  168. unless (user.nil? or clinician["Date_Last_Used"].nil?)
  169. user_prop = UserProperty.find(:first, :conditions => ["user_id = ? AND property = ?", user.id, "Date Last Used"])
  170. user_prop = UserProperty.new if user_prop.nil?
  171. user_prop.user_id = user.user_id
  172. user_prop.property = "Date last used"
  173. user_prop.property_value = clinician["Date_Last_Used"]
  174. user_prop.save
  175. end
  176. end
  177.  
  178. def self.migrate_master_patient_record_to_patient(mpr, legacy_nat, legacy_ped, birth_ta)
  179.  
  180. # Feedback
  181. # puts ":Migrating Master Patient Record " + mpr["First_Name"] + " " + mpr["Last_Name"] + " (" + @@count.to_s + ")\n"
  182.  
  183. # The Day_Of_Birth and Month_Of_Birth are used to contain the displayed
  184. # birthdate, actual or estimated. If the birth day is unknown, the
  185. # Day_Of_Birth field contains the string "??" instead of a number. This
  186. # is always displayed in the application as "??" but for date
  187. # calculations, the Birth_Date field is used (see below). The Birth_Date
  188. # field always contains the "15" for and unknown date and "Jun" for an
  189. # unknown month, or the actual birth date or month if available. So for
  190. # example, if a patient has no idea what time of year they were born and
  191. # an estimated year of 1981 is given, the following two fields contain
  192. # "??" and "???" and the Birth_Date field is assigned "15-Jun-1981".
  193.  
  194. # In openMRS this will need to be stored as patient.birthdate with
  195. # patient.birthdate_estimated = 1 (treated as boolean even though the
  196. # data type is SMALLINT(5) in the case of an estimated date). If only
  197. # day of month is unknown it gets set to 15. If month is also unknown
  198. # then month gets set to July and day of month gets set to 1. With this
  199. # in place we will be able to determine which component of the date was
  200. # estimated, which is important.
  201. birthdate_estimated = 0
  202.  
  203. # If both are unknown
  204. if (mpr["Day_Of_Birth"] = "??" and mpr["Month_Of_Birth"] = "??")
  205. mpr["Day_Of_Birth"] = "01"
  206. mpr["Month_Of_Birth"] = "06"
  207. birthdate_estimated = 1
  208. # If only day is unknown
  209. elsif (mpr["Day_Of_Birth"] = "??")
  210. mpr["Day_Of_Birth"] = "15"
  211. birthdate_estimated = 1
  212. # If only month is unknown (rare?)
  213. elsif (mpr["Month_Of_Birth"] = "??")
  214. mpr["Month_Of_Birth"] = "06"
  215. birthdate_estimated = 1
  216. end
  217.  
  218. # location_id = ActiveRecord::Base.connection.select_one("SELECT OpenMRSSiteID FROM qech.Site WHERE SiteID = #{mpr['Site_ID']}")["OpenMRSSiteID"]
  219. # user = User.find_by_username(mpr["Reg_By"])
  220. user = User.current_user
  221. location_id = 619
  222.  
  223. # Add the patient...
  224. # NOTE: the openmrs.patient table is assumed to not have conflicts!
  225. patient = Patient.new
  226. patient.birthdate = birthdate = DateTime::parse(mpr["Year_Of_Birth"] + "-" + mpr["Month_Of_Birth"] + "-" + mpr["Day_Of_Birth"])
  227. patient.birthdate_estimated = birthdate_estimated
  228. patient.creator = user.user_id
  229. patient.date_created = convert_date(mpr["Date_Reg"], "")
  230. patient.health_center = location_id
  231. if (mpr["Sex"] == "M")
  232. patient.gender = "Male"
  233. else
  234. patient.gender = "Female"
  235. end
  236. unless (@@bi == "")
  237. @@bi << ","
  238. end
  239. @@bi << "(" + (@@start+@@count).to_s + "," + \
  240. "'" + patient.gender + "'," + \
  241. "NULL," + \
  242. "'" + patient.birthdate.to_s + "'," + \
  243. birthdate_estimated.to_s + "," + \
  244. "NULL," + \
  245. "NULL," + \
  246. "NULL," + \
  247. "NULL," + \
  248. "NULL," + \
  249. "NULL," + \
  250. "NULL," + \
  251. "NULL," + \
  252. "NULL," + \
  253. patient.health_center.to_s + "," + \
  254. patient.creator.to_s + "," + \
  255. "'" + patient.date_created.to_s + "'," + \
  256. "NULL," + \
  257. "NULL," + \
  258. "NULL," + \
  259. "NULL," + \
  260. "NULL," + \
  261. "NULL)"
  262.  
  263. if (@@count % 100 == 0)
  264. puts "Buffering... " + @@count.to_s
  265. end
  266.  
  267. if (@@count % 10000 == 0)
  268. puts "Inserting block " + @@count.to_s
  269. ActiveRecord::Base.connection.execute("LOCK TABLES `patient` WRITE")
  270. ActiveRecord::Base.connection.execute("INSERT INTO patient VALUES " + @@bi)
  271. ActiveRecord::Base.connection.execute("UNLOCK TABLES")
  272. @@bi = ""
  273. end
  274.  
  275.  
  276. # patient.save
  277.  
  278.  
  279. return
  280. puts "Not here"
  281.  
  282. # Add in the identifiers and traditional authority
  283. unless (mpr["Pat_ID"].nil?)
  284. pi = PatientIdentifier.new
  285. pi.patient_id = patient.patient_id
  286. pi.identifier = mpr["Pat_ID"]
  287. pi.identifier_type = legacy_nat.patient_identifier_type_id
  288. pi.location_id = location_id
  289. pi.creator = user.user_id
  290. pi.date_created = patient.date_created
  291. pi.save
  292. else
  293. puts " Pat_ID is null (" + @@count.to_s + ")"
  294. end
  295.  
  296. unless (mpr["Legacy_Pat_Num"].nil?)
  297. pi = PatientIdentifier.new
  298. pi.patient_id = patient.patient_id
  299. pi.identifier = mpr["Legacy_Pat_Num"]
  300. pi.identifier_type = legacy_ped.patient_identifier_type_id
  301. pi.location_id = location_id
  302. pi.creator = user.user_id
  303. pi.date_created = patient.date_created
  304. pi.save
  305. else
  306. puts " Legacy_Pat_Num is null (" + @@count.to_s + ")"
  307. end
  308.  
  309. unless (mpr["Legacy_Pat_Num"].nil?)
  310. pi = PatientIdentifier.new
  311. pi.patient_id = patient.patient_id
  312. pi.identifier = mpr["Birth_TA"]
  313. pi.identifier_type = birth_ta.patient_identifier_type_id
  314. pi.location_id = location_id
  315. pi.creator = user.user_id
  316. pi.date_created = patient.date_created
  317. pi.save
  318. else
  319. puts " Birth_TA is null (" + @@count.to_s + ")"
  320. end
  321.  
  322.  
  323. # Add in first and last name
  324. pn = PatientName.new
  325. pn.patient_id = patient.patient_id
  326. pn.given_name = mpr["First_Name"]
  327. pn.family_name = mpr["Last_Name"]
  328. pn.creator = user.user_id
  329. pn.date_created = patient.date_created
  330. pn.save
  331.  
  332. # Add the address
  333. pa = PatientAddress.new
  334. pa.patient_id = patient.patient_id
  335. pa.address1 = mpr["Address"]
  336. pa.creator = user.user_id
  337. pa.date_created = patient.date_created
  338. pa.save
  339.  
  340. end
  341.  
  342.  
  343. # Convert a 12-JUL-2005 date to a Ruby DateTime
  344. # Yes, I know this function is longer than it could be, but it also faster
  345. # Than a Hash by 50% in some cases, so it is what it is
  346. def self.convert_date(old_date, old_time)
  347. old_day = old_date.slice(0, 2)
  348. old_month = old_date.slice(3, 3)
  349. old_year = old_date.slice(7, 4)
  350. case old_month
  351. when "JAN"
  352. DateTime::parse(old_year + "-01-" + old_day + "T" + old_time)
  353. when "FEB"
  354. DateTime::parse(old_year + "-02-" + old_day + "T" + old_time)
  355. when "MAR"
  356. DateTime::parse(old_year + "-03-" + old_day + "T" + old_time)
  357. when "APR"
  358. DateTime::parse(old_year + "-04-" + old_day + "T" + old_time)
  359. when "MAY"
  360. DateTime::parse(old_year + "-05-" + old_day + "T" + old_time)
  361. when "JUN"
  362. DateTime::parse(old_year + "-06-" + old_day + "T" + old_time)
  363. when "JUL"
  364. DateTime::parse(old_year + "-07-" + old_day + "T" + old_time)
  365. when "AUG"
  366. DateTime::parse(old_year + "-08-" + old_day + "T" + old_time)
  367. when "SEP"
  368. DateTime::parse(old_year + "-09-" + old_day + "T" + old_time)
  369. when "OCT"
  370. DateTime::parse(old_year + "-10-" + old_day + "T" + old_time)
  371. when "NOV"
  372. DateTime::parse(old_year + "-11-" + old_day + "T" + old_time)
  373. when "DEC"
  374. DateTime::parse(old_year + "-12-" + old_day + "T" + old_time)
  375. else
  376. raise "Invalid date for conversion: " + old_date + "T" + old_time
  377. end
  378. end
  379.  
  380.  
  381. end
Add Comment
Please, Sign In to add comment