Skip to content

Commit

Permalink
fix: migration to breast
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Barkmin committed Jun 7, 2020
1 parent f534a1b commit 2a20f1a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
1 change: 1 addition & 0 deletions server/src/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function connectDB() {
);
setTimeout(connectDB, 5000);
} else {
console.info('==== MIGRATIONS ====');
migrations.up();
}
}
Expand Down
12 changes: 10 additions & 2 deletions server/src/migrations/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import m202006607 from './m20200607';

const migrations = [m202006607];

const up = () => {
m202006607.up();
migrations.forEach((m) => {
console.info(`UP: ${m.title}`);
m.up();
});
};

const down = () => {
m202006607.down();
migrations.forEach((m) => {
console.info(`DOWN: ${m.title}`);
m.down();
});
};

export default {
Expand Down
15 changes: 10 additions & 5 deletions server/src/migrations/m20200607.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
import modules from '../modules';

const title = 'convert breastLeft to breast';

function up() {
modules.nursing.Model.find({ breastLeft: { $exists: true } }).then((docs) => {
console.info(`> ${docs.length} affected`);
docs.forEach(function (doc) {
if (doc.breastLeft === true) {
const breastLeft = doc.get('breastLeft', { strict: false });
if (breastLeft === true) {
doc.breast = 'left';
} else if (doc.breastLeft === false) {
} else if (breastLeft === false) {
doc.breast = 'right';
}
delete doc.breastLeft;
doc.save();
});
});
}

function down() {
modules.nursing.Model.find({ breast: { $exists: false } }).then((docs) => {
console.info(`> ${docs.length} affected`);
docs.forEach(function (doc) {
if (doc.breast === 'left') {
const breast = doc.get('breast', { strict: false });
if (breast === 'left') {
doc.breastLeft = true;
} else {
doc.breastLeft = false;
}
delete doc.breast;
doc.save();
});
});
Expand All @@ -31,4 +35,5 @@ function down() {
export default {
up,
down,
title,
};

0 comments on commit 2a20f1a

Please sign in to comment.