From 64b1ee0f3fd5865099f65fce9d9a2d2aa47b87ac Mon Sep 17 00:00:00 2001 From: tangollama Date: Thu, 24 Mar 2016 22:49:22 -0400 Subject: [PATCH 1/4] patient notes migration Tied to #32 in hospitalrun-frontend --- .gitignore | 4 +++ utils/paitnet-notes-mig.js | 53 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 utils/paitnet-notes-mig.js diff --git a/.gitignore b/.gitignore index bd442479..634cd70a 100644 --- a/.gitignore +++ b/.gitignore @@ -24,3 +24,7 @@ restart.sh start.sh stop.sh + +newrelic_agent.log + +newrelic.js diff --git a/utils/paitnet-notes-mig.js b/utils/paitnet-notes-mig.js new file mode 100644 index 00000000..8d87db1f --- /dev/null +++ b/utils/paitnet-notes-mig.js @@ -0,0 +1,53 @@ +var config = require('../config.js'), + nano = require('nano')(config.couchAuthDbURL), + maindb = nano.use('main'), + userdb = nano.use('user'), + uuid = require('node-uuid'); + +function updateRecord(record, callback) { + var updateParams = record._id; + maindb.insert(record, updateParams, function(err, body) { + if (err) { + callback(err); + } else { + record._id = body.id; + record._rev = body.rev; + callback(null, record); + } + }); +} + +maindb.list({startkey:'visit_', endkey:'visit_\uffff', include_docs: true}, function(err, results) { + console.log("start"); + if (!err) { + results.rows.forEach(function(result) { + var visitDoc = result.doc.data; + if (visitDoc !== null && visitDoc.notes !== null && visitDoc.patientNotes === null) { + var patientNote = { + _id: 'patientNote_2_'+ uuid.v4(), + content: visitDoc.notes, + createdBy: 'system', + date: new Date(), + noteType: 'General', + patient: visitDoc.patient, + visit: visitDoc._id + }; + updateRecord(patientNote, function(error, result) { + if (error !== null) { + console.log("error"); + console.dir(error); + } else { + visitDoc.patientNotes = [ patientNote._id ]; + updateRecord(visitDoc, function() { + console.log("sucess"); + console.log("added: " + result._id + " to " + visitDoc._id); + }); + } + }); + } + }); + console.log("end loop"); + } else { + console.log("ERROR fetching visit records", err); + } +}); \ No newline at end of file From 30874e6df45cc83c2c2c8930eef107779be75edc Mon Sep 17 00:00:00 2001 From: tangollama Date: Thu, 31 Mar 2016 10:40:47 -0700 Subject: [PATCH 2/4] mislabeled name of the file --- utils/{paitnet-notes-mig.js => patient-notes-mig.js} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename utils/{paitnet-notes-mig.js => patient-notes-mig.js} (100%) diff --git a/utils/paitnet-notes-mig.js b/utils/patient-notes-mig.js similarity index 100% rename from utils/paitnet-notes-mig.js rename to utils/patient-notes-mig.js From a786b4f515df7ab019608f724681514953802fc4 Mon Sep 17 00:00:00 2001 From: John Kleinschmidt Date: Fri, 8 Apr 2016 15:40:57 -0400 Subject: [PATCH 3/4] Removed old _id from migrated data because it is not needed Also, this field was causing confusion. --- utils/pouch-mig.js | 1 + 1 file changed, 1 insertion(+) diff --git a/utils/pouch-mig.js b/utils/pouch-mig.js index 12067c6b..aded4099 100644 --- a/utils/pouch-mig.js +++ b/utils/pouch-mig.js @@ -31,6 +31,7 @@ function migrateRecords() { data: result.doc }; existingKeys[newDocId] = true; + delete newDoc.data._id; delete newDoc.data._rev; switch (parsedId.doctype) { From 7b7dfafd86141a464ea9e5eeb5a8ac31c31259bc Mon Sep 17 00:00:00 2001 From: John Kleinschmidt Date: Fri, 8 Apr 2016 15:41:20 -0400 Subject: [PATCH 4/4] Changed to use bulk update and fixed various issues. --- utils/patient-notes-mig.js | 75 ++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 43 deletions(-) diff --git a/utils/patient-notes-mig.js b/utils/patient-notes-mig.js index 8d87db1f..1cdf57ef 100644 --- a/utils/patient-notes-mig.js +++ b/utils/patient-notes-mig.js @@ -1,53 +1,42 @@ -var config = require('../config.js'), - nano = require('nano')(config.couchAuthDbURL), - maindb = nano.use('main'), - userdb = nano.use('user'), - uuid = require('node-uuid'); +var config = require('../config.js'); +var nano = require('nano')(config.couchAuthDbURL); +var maindb = nano.use('main'); +var uuid = require('node-uuid'); +var recordsToUpdate = []; -function updateRecord(record, callback) { - var updateParams = record._id; - maindb.insert(record, updateParams, function(err, body) { - if (err) { - callback(err); - } else { - record._id = body.id; - record._rev = body.rev; - callback(null, record); - } - }); -} - -maindb.list({startkey:'visit_', endkey:'visit_\uffff', include_docs: true}, function(err, results) { - console.log("start"); +maindb.list({startkey: 'visit_', endkey: 'visit_\uffff', include_docs: true}, function(err, results) { if (!err) { results.rows.forEach(function(result) { var visitDoc = result.doc.data; - if (visitDoc !== null && visitDoc.notes !== null && visitDoc.patientNotes === null) { + if (visitDoc !== null && visitDoc.notes && !visitDoc.patientNotes) { + var noteUuid = uuid.v4(); + var visitId = result.doc._id.substr(8); var patientNote = { - _id: 'patientNote_2_'+ uuid.v4(), - content: visitDoc.notes, - createdBy: 'system', - date: new Date(), - noteType: 'General', - patient: visitDoc.patient, - visit: visitDoc._id - }; - updateRecord(patientNote, function(error, result) { - if (error !== null) { - console.log("error"); - console.dir(error); - } else { - visitDoc.patientNotes = [ patientNote._id ]; - updateRecord(visitDoc, function() { - console.log("sucess"); - console.log("added: " + result._id + " to " + visitDoc._id); - }); + _id: 'patientNote_2_' + noteUuid, + data: { + content: visitDoc.notes, + createdBy: 'system', + date: new Date(), + noteType: 'General', + patient: visitDoc.patient, + visit: visitId } - }); + }; + recordsToUpdate.push(patientNote); + result.doc.data.patientNotes = [noteUuid]; + recordsToUpdate.push(result.doc); } }); - console.log("end loop"); + if (recordsToUpdate.length > 0) { + maindb.bulk({ docs: recordsToUpdate}, function(err, results) { + if (err) { + console.log('Error updating patient notes records.', err); + } else { + console.log('Success updating patient notes records.', JSON.stringify(results, null, 2)); + } + }); + } } else { - console.log("ERROR fetching visit records", err); + console.log('ERROR fetching visit records', err); } -}); \ No newline at end of file +});