From 87d455e98ba2afe40e6a668908be2f11fe123d00 Mon Sep 17 00:00:00 2001 From: Scott Erickson Date: Wed, 27 Aug 2014 18:16:19 -0700 Subject: [PATCH] Wrote a script to migrate component config defaults to the new system. --- .../2014-08-27-component-config-defaults.js | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 scripts/mongodb/migrations/2014-08-27-component-config-defaults.js diff --git a/scripts/mongodb/migrations/2014-08-27-component-config-defaults.js b/scripts/mongodb/migrations/2014-08-27-component-config-defaults.js new file mode 100644 index 000000000..6d70f2f5b --- /dev/null +++ b/scripts/mongodb/migrations/2014-08-27-component-config-defaults.js @@ -0,0 +1,33 @@ +load('bower_components/lodash/dist/lodash.js'); +load('bower_components/underscore.string/dist/underscore.string.min.js'); +load('bower_components/tv4/tv4.js'); +load('bower_components/treema/treema-utils.js'); +print(typeof TreemaUtils); + +print(db.level.components.count()); +db.level.components.find().limit(3000000).forEach(function (levelComponent) { + thisTv4 = tv4.freshApi(); + thisTv4.addSchema('#', levelComponent.configSchema || {}); + var data = levelComponent.configSchema.default || {}; + TreemaUtils.populateRequireds(data, levelComponent.configSchema); + var props = levelComponent.configSchema.properties; + if(props) { + _.keys(levelComponent.configSchema.properties).forEach(function (key) { + if(data[key]) return; + var childSchema = props[key]; + var workingSchema = TreemaUtils.buildWorkingSchemas(childSchema, thisTv4)[0]; + if(workingSchema.default) + return data[key] = TreemaUtils.cloneDeep(workingSchema.default); + var type = props[key].type; + if(!type) return; + if(Array.isArray(type)) type = type[0]; + data[key] = TreemaUtils.defaultForType(type); + }) + } + delete levelComponent.configSchema.required; + levelComponent.configSchema.default = data; + print('\n\n--------------------', levelComponent.name); + print("SCHEMA: ", JSON.stringify(levelComponent.configSchema, null, '\t')); + +}); +