Skip to content
This repository has been archived by the owner on Aug 6, 2020. It is now read-only.

Commit

Permalink
Adds a migration script to fix bad dates
Browse files Browse the repository at this point in the history
Finds all dates that have a space in them (implying they are the
problematic "17th Apr, 2010" style) and convert them to YYYY-MM-DD as
expected.

Issue: medic/cht-core#1672
  • Loading branch information
SCdF committed Apr 20, 2016
1 parent 12adfbc commit be50823
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions migrations/convert-bad-dob-format.js
@@ -0,0 +1,38 @@
var async = require('async'),
db = require('../db'),
moment = require('moment');

var temporaryView = {
"map": "function(doc) { if (doc.type === 'person' && doc.date_of_birth && doc.date_of_birth.indexOf(' ') >= 0) { emit(1); }}"
};

module.exports = {
name: 'convert-bad-dob-format',
created: new Date(2016, 4, 20),
run: function(callback) {
db.request({
db: 'medic',
method: 'POST',
path: '_temp_view',
body: temporaryView,
qs: {
include_docs: true,
}
}, function(err, result) {
var docs = result.rows.map(function(row) {

This comment has been minimized.

Copy link
@alxndrsn

alxndrsn Apr 21, 2016

Contributor

Seems weird to iterate twice.

This comment has been minimized.

Copy link
@SCdF

SCdF Apr 21, 2016

Author Contributor

True

return row.doc;
});

for(doc of docs) {

This comment has been minimized.

This comment has been minimized.

Copy link
@SCdF

SCdF Apr 21, 2016

Author Contributor

Yep, this is just run when medic-api boots up. Good point though, I should check what our minimal node / js version is.

This comment has been minimized.

Copy link
@garethbowen

garethbowen Apr 21, 2016

Member

It works, but Array.prototype.forEach() is preferred. IIRC it's more predictable.

This comment has been minimized.

Copy link
@SCdF

SCdF Apr 21, 2016

Author Contributor

FWIW it's changed to forEach in later commits, check the PR: #58

This comment has been minimized.

Copy link
@alxndrsn

alxndrsn Apr 21, 2016

Contributor

Worth a lot - that means this whole discussion is pointless :)

This comment has been minimized.

Copy link
@alxndrsn

alxndrsn Apr 21, 2016

Contributor

Also didn't realise this was a PR as well.

var currentDob = doc.date_of_birth;
var convertedDob = moment(doc.date_of_birth, 'MMM Do, YYYY').format('YYYY-MM-DD');

This comment has been minimized.

Copy link
@alxndrsn

alxndrsn Apr 21, 2016

Contributor

If we know the bad format is always 'MMM Do, YYYY', would it be safer to match this in the view as well, instead of just checking for ' '?

This comment has been minimized.

Copy link
@SCdF

SCdF Apr 21, 2016

Author Contributor

It would, but I'm not sure how I'd do it in the view? I could do a regex, something like /[a-zA-Z]{3} [0-9]{1-2}, [0-9]{4{/ or whatever. My thought process was that migrations only run once, and we know that all the bad dates right now are like this, so I shouldn't make the view any more complicated than it needs to be. Happy to add that in though.

This comment has been minimized.

Copy link
@alxndrsn

alxndrsn Apr 21, 2016

Contributor

Sounds fine as it is.


doc.date_of_birth = convertedDob;
}

db.medic.bulk({
docs: docs
}, callback);
});
}
};

0 comments on commit be50823

Please sign in to comment.