Skip to content

Instantly share code, notes, and snippets.

@TGOlson
Last active August 29, 2015 14:21
Show Gist options
  • Select an option

  • Save TGOlson/29651eb759557b81e8bb to your computer and use it in GitHub Desktop.

Select an option

Save TGOlson/29651eb759557b81e8bb to your computer and use it in GitHub Desktop.
Update tenant auth connectors migration
/*
* Generate migration file:
* $ gulp create-migration --name update-tenant-auth-connectors
*/
// Define migration
var systemService = require('<systemService>'),
systemTenant = systemService.systemTenant;
exports.up = Scaffold.defineMigration(function() {
// performOperationOnAllCollections is internal to the dbutils - should expose
return DbUtils.performOperationOnAllCollections(updateTenantAuthConnectorsInDataSpace);
});
function updateTenantAuthConnectorsInDataSpace(dsKey) {
return findTenantsInDataSpace(dsKey)
.then(R.map(EntityUtils.loadEntity({$all: true}))) // whatever, makes the rest easier
.then(R.map(updateTenantAuthConnectors))
}
function findTenantsInDataSpace(dsKey) {
return EntityDao.select(dsKey, {type: 'Tenant'})
.then(R.map(getEntityFromDoc(dsKey)));
}
var getEntityFromDoc = R.curry(function(dsKey, doc) {
return systemService.get(systemService.makeEid(dsKey, doc.entityKey));
});
function updateTenantAuthConnectors(tenantEntity) {
var oldAuthConnectors = tenantEntity.authConnectors,
updatedAuthConnectors = R.map(R.createMapEntry('connectionType'), oldAuthConnectors);
return tenantEntity.set({authConnectors: updatedAuthConnectors});
}
// A lot of this work is done just to find all the collections,
// make a query against all the collections, and then map those docs to entities
// maybe that could be a db-utils helper function?
// DbUtils.findAllEntities(<query>) ...
// Btw, This is how that would work if we could use real queries in entity.select
// not just fake sparql strings...
function findAllEntities(query) {
return findAllDataSpaces().then(R.map(select(query)))
}
function findAllDataSpaces() {
return EntityDao.getDataspaceKeys()
.then(R.map(getDataSpaceByDsKey))
}
function getDataSpaceByDsKey(dsKey) {
return EntityUtils.getDataSpace(dsKey, systemService);
}
var select = R.curry(function(query, entity) {
return entity.select(query);
});
var MigrationTest = require('./migration-test-assertions');
var UpdateTenantAuthConnectors = require('../update-tenant-auth-connectors');
var initialData = {
'example.com': [{
type: 'Tenant',
authConnectors: ['one', 'two']
}],
'mindjet.com': [{
type: 'Tenant',
authConnectors: ['three', 'four']
}]
};
var postData = {
'example.com': [{
type: 'Tenant',
authConnectors: [{connectionType: 'one'}, {connectionType: 'two'}]
}],
'mindjet.com': [{
type: 'Tenant',
authConnectors: [{connectionType: 'three'}, {connectionType: 'four'}]
}]
};
MigrationTest.assertMigrationEq(UpdateTenantAuthConnectors, initialData, postData);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment