Skip to content

Commit

Permalink
feat: add db migration scripts (#1072)
Browse files Browse the repository at this point in the history
  • Loading branch information
mantariksh authored Feb 4, 2021
1 parent 5d6ae59 commit 43e0915
Show file tree
Hide file tree
Showing 6 changed files with 764 additions and 0 deletions.
60 changes: 60 additions & 0 deletions scripts/20210202_deprecate-myinfo-v2/billadd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* eslint-disable */

// BEFORE
// Count total number of these fields
{
const ATTR_NAME = 'billadd'
const beforePipeline = [
{ $match: { form_fields: { $exists: true, $not: { $size: 0 } }, 'form_fields.myInfo.attr': ATTR_NAME } },
{ $project: { form_fields: 1 } },
{ $unwind: '$form_fields' },
{ $match: { 'form_fields.myInfo.attr': ATTR_NAME } },
{ $count: 'numFields' }
]
db.forms.aggregate(beforePipeline)
}

// Count number of forms with this field
{
const ATTR_NAME = 'billadd'
const pipeline = [
{ $match: { form_fields: { $exists: true, $not: { $size: 0 } }, 'form_fields.myInfo.attr': ATTR_NAME } },
{ $count: 'numForms' }
]
db.forms.aggregate(pipeline)
}

// UPDATE
// Should be same as number of forms
{
const ATTR_NAME = 'billadd'
db.forms.updateMany(
{ form_fields: { $exists: true, $not: { $size: 0 } }, 'form_fields.myInfo.attr': ATTR_NAME },
{ $unset: { 'form_fields.$[field].myInfo': 1 } },
{ arrayFilters: [{ 'field.myInfo.attr': ATTR_NAME }] }
)
}

// AFTER
// Ideally 0, unless someone has inserted a new field in between
{
const ATTR_NAME = 'billadd'
const afterPipeline = [
{ $match: { form_fields: { $exists: true, $not: { $size: 0 } }, 'form_fields.myInfo.attr': ATTR_NAME } },
{ $project: { form_fields: 1 } },
{ $unwind: '$form_fields' },
{ $match: { 'form_fields.myInfo.attr': ATTR_NAME } },
{ $count: 'numFields' }
]
db.forms.aggregate(afterPipeline)
}

// Count number of forms with this field
{
const ATTR_NAME = 'billadd'
const pipeline = [
{ $match: { form_fields: { $exists: true, $not: { $size: 0 } }, 'form_fields.myInfo.attr': ATTR_NAME } },
{ $count: 'numForms' }
]
db.forms.aggregate(pipeline)
}
73 changes: 73 additions & 0 deletions scripts/20210202_deprecate-myinfo-v2/edulevel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/* eslint-disable */

// BEFORE
// Count total number of these fields
{
const ATTR_NAME = 'edulevel'
const pipeline = [
{ $match: { form_fields: { $exists: true, $not: { $size: 0 } }, 'form_fields.myInfo.attr': ATTR_NAME } },
{ $project: { form_fields: 1 } },
{ $unwind: '$form_fields' },
{ $match: { 'form_fields.myInfo.attr': ATTR_NAME } },
{ $count: 'numFields' }
]
db.forms.aggregate(pipeline)
}

// Count number of forms with this field
{
const ATTR_NAME = 'edulevel'
const pipeline = [
{ $match: { form_fields: { $exists: true, $not: { $size: 0 } }, 'form_fields.myInfo.attr': ATTR_NAME } },
{ $count: 'numForms' }
]
db.forms.aggregate(pipeline)
}

// UPDATE
// Should be same as number of forms
{
const ATTR_NAME = 'edulevel'
const FIELD_OPTIONS = [
'NO FORMAL QUALIFICATION / PRE-PRIMARY / LOWER PRIMARY',
'PRIMARY',
'LOWER SECONDARY',
'SECONDARY',
'POST-SECONDARY (NON-TERTIARY): GENERAL & VOCATION',
'POLYTECHNIC DIPLOMA',
'PROFESSIONAL QUALIFICATION AND OTHER DIPLOMA',
"BACHELOR'S OR EQUIVALENT",
"POSTGRADUATE DIPLOMA / CERTIFICATE (EXCLUDING MASTER'S AND DOCTORATE)",
"MASTER'S AND DOCTORATE OR EQUIVALENT",
'MODULAR CERTIFICATION (NON-AWARD COURSES / NON-FULL QUALIFICATIONS)'
]
db.forms.updateMany(
{ form_fields: { $exists: true, $not: { $size: 0 } }, 'form_fields.myInfo.attr': ATTR_NAME },
{ $unset: { 'form_fields.$[field].myInfo': 1 }, $set: { 'form_fields.$[field].fieldOptions': FIELD_OPTIONS } },
{ arrayFilters: [{ 'field.myInfo.attr': ATTR_NAME }] }
)
}

// AFTER
// Ideally 0, unless someone has inserted a new field in between
{
const ATTR_NAME = 'edulevel'
const pipeline = [
{ $match: { form_fields: { $exists: true, $not: { $size: 0 } }, 'form_fields.myInfo.attr': ATTR_NAME } },
{ $project: { form_fields: 1 } },
{ $unwind: '$form_fields' },
{ $match: { 'form_fields.myInfo.attr': ATTR_NAME } },
{ $count: 'numFields' }
]
db.forms.aggregate(pipeline)
}

// Count number of forms with this field
{
const ATTR_NAME = 'edulevel'
const pipeline = [
{ $match: { form_fields: { $exists: true, $not: { $size: 0 } }, 'form_fields.myInfo.attr': ATTR_NAME } },
{ $count: 'numForms' }
]
db.forms.aggregate(pipeline)
}
66 changes: 66 additions & 0 deletions scripts/20210202_deprecate-myinfo-v2/gradyear.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/* eslint-disable */

// BEFORE
// Count total number of these fields
{
const ATTR_NAME = 'gradyear'
const beforePipeline = [
{ $match: { form_fields: { $exists: true, $not: { $size: 0 } }, 'form_fields.myInfo.attr': ATTR_NAME } },
{ $project: { form_fields: 1 } },
{ $unwind: '$form_fields' },
{ $match: { 'form_fields.myInfo.attr': ATTR_NAME } },
{ $count: 'numFields' }
]
db.forms.aggregate(beforePipeline)
}

// Count number of forms with this field
{
const ATTR_NAME = 'gradyear'
const pipeline = [
{ $match: { form_fields: { $exists: true, $not: { $size: 0 } }, 'form_fields.myInfo.attr': ATTR_NAME } },
{ $count: 'numForms' }
]
db.forms.aggregate(pipeline)
}

// UPDATE
// Should be same as number of forms
{
const ATTR_NAME = 'gradyear'
const VALIDATION_OPTIONS = {
customMax: 4,
customMin: 4,
customVal: 4,
selectedValidation: 'Exact'
}
db.forms.updateMany(
{ form_fields: { $exists: true, $not: { $size: 0 } }, 'form_fields.myInfo.attr': ATTR_NAME },
{ $unset: { 'form_fields.$[field].myInfo': 1 }, $set: { 'form_fields.$[field].ValidationOptions': VALIDATION_OPTIONS } },
{ arrayFilters: [{ 'field.myInfo.attr': ATTR_NAME }] }
)
}

// AFTER
// Ideally 0, unless someone has inserted a new field in between
{
const ATTR_NAME = 'gradyear'
const afterPipeline = [
{ $match: { form_fields: { $exists: true, $not: { $size: 0 } }, 'form_fields.myInfo.attr': ATTR_NAME } },
{ $project: { form_fields: 1 } },
{ $unwind: '$form_fields' },
{ $match: { 'form_fields.myInfo.attr': ATTR_NAME } },
{ $count: 'numFields' }
]
db.forms.aggregate(afterPipeline)
}

// Count number of forms with this field
{
const ATTR_NAME = 'gradyear'
const pipeline = [
{ $match: { form_fields: { $exists: true, $not: { $size: 0 } }, 'form_fields.myInfo.attr': ATTR_NAME } },
{ $count: 'numForms' }
]
db.forms.aggregate(pipeline)
}
60 changes: 60 additions & 0 deletions scripts/20210202_deprecate-myinfo-v2/homeno.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* eslint-disable */

// BEFORE
// Count total number of these fields
{
const ATTR_NAME = 'homeno'
const beforePipeline = [
{ $match: { form_fields: { $exists: true, $not: { $size: 0 } }, 'form_fields.myInfo.attr': ATTR_NAME } },
{ $project: { form_fields: 1 } },
{ $unwind: '$form_fields' },
{ $match: { 'form_fields.myInfo.attr': ATTR_NAME } },
{ $count: 'numFields' }
]
db.forms.aggregate(beforePipeline)
}

// Count number of forms with this field
{
const ATTR_NAME = 'homeno'
const pipeline = [
{ $match: { form_fields: { $exists: true, $not: { $size: 0 } }, 'form_fields.myInfo.attr': ATTR_NAME } },
{ $count: 'numForms' }
]
db.forms.aggregate(pipeline)
}

// UPDATE
// Should be same as number of forms
{
const ATTR_NAME = 'homeno'
db.forms.updateMany(
{ form_fields: { $exists: true, $not: { $size: 0 } }, 'form_fields.myInfo.attr': ATTR_NAME },
{ $unset: { 'form_fields.$[field].myInfo': 1 } },
{ arrayFilters: [{ 'field.myInfo.attr': ATTR_NAME }] }
)
}

// AFTER
// Ideally 0, unless someone has inserted a new field in between
{
const ATTR_NAME = 'homeno'
const afterPipeline = [
{ $match: { form_fields: { $exists: true, $not: { $size: 0 } }, 'form_fields.myInfo.attr': ATTR_NAME } },
{ $project: { form_fields: 1 } },
{ $unwind: '$form_fields' },
{ $match: { 'form_fields.myInfo.attr': ATTR_NAME } },
{ $count: 'numFields' }
]
db.forms.aggregate(afterPipeline)
}

// Count number of forms with this field
{
const ATTR_NAME = 'homeno'
const pipeline = [
{ $match: { form_fields: { $exists: true, $not: { $size: 0 } }, 'form_fields.myInfo.attr': ATTR_NAME } },
{ $count: 'numForms' }
]
db.forms.aggregate(pipeline)
}
60 changes: 60 additions & 0 deletions scripts/20210202_deprecate-myinfo-v2/mailadd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* eslint-disable */

// BEFORE
// Count total number of these fields
{
const ATTR_NAME = 'mailadd'
const beforePipeline = [
{ $match: { form_fields: { $exists: true, $not: { $size: 0 } }, 'form_fields.myInfo.attr': ATTR_NAME } },
{ $project: { form_fields: 1 } },
{ $unwind: '$form_fields' },
{ $match: { 'form_fields.myInfo.attr': ATTR_NAME } },
{ $count: 'numFields' }
]
db.forms.aggregate(beforePipeline)
}

// Count number of forms with this field
{
const ATTR_NAME = 'mailadd'
const pipeline = [
{ $match: { form_fields: { $exists: true, $not: { $size: 0 } }, 'form_fields.myInfo.attr': ATTR_NAME } },
{ $count: 'numForms' }
]
db.forms.aggregate(pipeline)
}

// UPDATE
// Should be same as number of forms
{
const ATTR_NAME = 'mailadd'
db.forms.updateMany(
{ form_fields: { $exists: true, $not: { $size: 0 } }, 'form_fields.myInfo.attr': ATTR_NAME },
{ $unset: { 'form_fields.$[field].myInfo': 1 } },
{ arrayFilters: [{ 'field.myInfo.attr': ATTR_NAME }] }
)
}

// AFTER
// Ideally 0, unless someone has inserted a new field in between
{
const ATTR_NAME = 'mailadd'
const afterPipeline = [
{ $match: { form_fields: { $exists: true, $not: { $size: 0 } }, 'form_fields.myInfo.attr': ATTR_NAME } },
{ $project: { form_fields: 1 } },
{ $unwind: '$form_fields' },
{ $match: { 'form_fields.myInfo.attr': ATTR_NAME } },
{ $count: 'numFields' }
]
db.forms.aggregate(afterPipeline)
}

// Count number of forms with this field
{
const ATTR_NAME = 'mailadd'
const pipeline = [
{ $match: { form_fields: { $exists: true, $not: { $size: 0 } }, 'form_fields.myInfo.attr': ATTR_NAME } },
{ $count: 'numForms' }
]
db.forms.aggregate(pipeline)
}
Loading

0 comments on commit 43e0915

Please sign in to comment.