Created
March 3, 2014 14:15
-
-
Save iest/9325760 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Changes effective as of v1.2-2014-g59824b3 | |
* | |
* Update Holistic's CMS MongoDB schema to reflect API changes. | |
* | |
* 1. Remove leading slashes from the `relativePath`s of all pages | |
* 2. Replace template ObjectIDs stored on the page with the template file name. | |
* 3. Drops the `simpleCmsTemplates` collection. | |
* 4. Recursively add `absolutePath`s to all pages | |
* | |
* It can be run multiple times without messing stuff up, so don't worry if you've run it multiple times. | |
*/ | |
var pagesDb = db.TEST_mmt, | |
templatesDb = db.TEST_mmtTemplates; | |
/** | |
* @method findPage | |
* @param {ObjectID} id | |
* @return {Document} | |
*/ | |
function findPage(id) { | |
return pagesDb.findOne({ | |
_id: id | |
}); | |
} | |
/** | |
* @method getParent | |
* @param {Document} page | |
* @param {String} url | |
* @return {String} | |
*/ | |
function getParent(page, url) { | |
// Find the parent of page | |
var parent = findPage(page.parent); | |
// If the parent doesn't exist, it means this page is an orphan, so delete it. | |
if (!parent) { | |
pagesDb.remove(page._id); | |
} | |
// Put the parent's relative path before the page's relative path, with a slash to separate | |
if (parent) { | |
url = parent.relativePath + "/" + url; | |
} | |
// If the parent is a child itself, recurse | |
if (parent && parent.parent) { | |
url = getParent(parent, url); | |
} | |
return url; | |
} | |
/** | |
* 1. Find all pages that have a `/` as their first character of the `relativePath` | |
* 2. Remove the `/` | |
* 3. Save the page | |
*/ | |
pagesDb.find({ | |
"relativePath": /^\// | |
}) | |
.snapshot() | |
.forEach( | |
function(e) { | |
e.relativePath = e.relativePath.substring(1, e.relativePath.length); | |
pagesDb.save(e); | |
}); | |
/** | |
* 1. Find all pages where template is an Object ID (type 7) | |
* 2. Resolve the template for that page by hitting the template collection | |
* 3. Set the template value to be the value of the resolved template | |
* 4. Save the page | |
*/ | |
pagesDb.find({ | |
template: { | |
$type: 7 | |
} | |
}) | |
.snapshot() | |
.forEach( | |
function(e) { | |
var template = templatesDb.findOne({ | |
_id: e.template | |
}); | |
e.template = template.templateFile; | |
pagesDb.save(e); | |
}); | |
/** | |
* Drop the templates collection. | |
*/ | |
templatesDb.drop(); | |
/** | |
* 1. Find all pages | |
* 2. If the page has a parent, resolve them recursively | |
* 3. If the page has a parent set, but it doens't exist, deletes that page (orphan) | |
* 4. Set the `absolutePath` on the page | |
*/ | |
pagesDb.find({ | |
absolutePath: { | |
$exists: false | |
} | |
}) | |
.snapshot() | |
.forEach( | |
function(e) { | |
if (e.parent) { | |
e.absolutePath = getParent(e, e.relativePath); | |
} else { | |
e.absolutePath = "/" + e.relativePath; | |
} | |
pagesDb.save(e); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment