Files
DiceCloud/app/imports/migrations/server/2.0-beta.33-dbv1.js

89 lines
2.8 KiB
JavaScript

import { Migrations } from 'meteor/percolate:migrations';
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
import LibraryNodes from '/imports/api/library/LibraryNodes.js';
import { get } from 'lodash';
import embedInlineCalculations from '/imports/api/creature/computation/afterComputation/embedInlineCalculations.js';
import transformFields from '/imports/migrations/server/transformFields.js';
import SCHEMA_VERSION from '/imports/constants/SCHEMA_VERSION.js';
// Git version 2.0-beta.33
// Database version 1
Migrations.add({
version: 1,
name: 'Unifies calculated field schema',
up(){
migrate();
},
down(){
migrate({reversed: true});
},
});
function migrate({reversed} = {}){
migrateCollection({collection: CreatureProperties, reversed});
migrateCollection({collection: LibraryNodes, reversed});
}
function migrateCollection({collection, reversed}){
const bulk = collection.rawCollection().initializeUnorderedBulkOp();
collection.find({}).forEach(prop => {
const newProp = migrateProperty({collection, reversed, prop});
bulk.find({ _id: prop._id }).replaceOne(newProp);
});
bulk.execute();
}
export default function migrateProperty({collection, reversed, prop}){
const transforms = transformsByPropType[prop.type];
let migratedProp = transformFields(prop, transforms, reversed);
const schema = collection.simpleSchema({type: prop.type});
// Only clean if the schema version matches our destination version
if(!reversed && SCHEMA_VERSION === 1){
try {
migratedProp = schema.clean(migratedProp);
schema.validate(migratedProp);
} catch(e){
console.warn(e);
}
}
return migratedProp;
}
const actionTransforms = [
...getComputedPropertyTransforms('uses'),
...getComputedPropertyTransforms('resources.attributesConsumed.$.quantity'),
...getComputedPropertyTransforms('resources.itemsConsumed.$.quantity'),
...getInlineComputationTransforms('summary'),
...getInlineComputationTransforms('description'),
];
const transformsByPropType = {
'action': actionTransforms,
'adjustment': [
...getComputedPropertyTransforms('amount'),
],
'attack': [
...actionTransforms,
...getComputedPropertyTransforms('rollBonus'),
],
};
function getComputedPropertyTransforms(key){
return [
{from: key, to: `${key}.calculation`},
{from: `${key}Result`, to: `${key}.value`},
{from: `${key}Errors`, to: `${key}.errors`},
];
}
function getInlineComputationTransforms(key){
return [
{from: key, to: `${key}.text`},
{from: `${key}Calculations`, to: `${key}.inlineCalculations`},
{to: `${key}.value`, up: (val, doc) =>
embedInlineCalculations(get(doc, key), get(doc, `${key}Calculations`))
},
{from: `${key}Calculations.$.result`, to: `${key}.inlineCalculations.$.value`},
];
}