From 2bdd60b5e8f87801d71e00692dacd5f6337f4ef0 Mon Sep 17 00:00:00 2001 From: Stefan Zermatten Date: Wed, 9 Feb 2022 12:37:16 +0200 Subject: [PATCH] Fixed issue where migrating attributes lost their base value calculation --- .../{2.0-beta.33-dbv1.js => dbv1/dbv1.js} | 8 ++- .../dbv1.test.js} | 58 ++++++++++++++++++- app/imports/migrations/server/index.js | 2 +- .../migrations/server/transformFields.test.js | 9 +++ 4 files changed, 72 insertions(+), 5 deletions(-) rename app/imports/migrations/server/{2.0-beta.33-dbv1.js => dbv1/dbv1.js} (95%) rename app/imports/migrations/server/{2.0-beta.33-dbv1.test.js => dbv1/dbv1.test.js} (72%) diff --git a/app/imports/migrations/server/2.0-beta.33-dbv1.js b/app/imports/migrations/server/dbv1/dbv1.js similarity index 95% rename from app/imports/migrations/server/2.0-beta.33-dbv1.js rename to app/imports/migrations/server/dbv1/dbv1.js index b16aa7e3..530f0774 100644 --- a/app/imports/migrations/server/2.0-beta.33-dbv1.js +++ b/app/imports/migrations/server/dbv1/dbv1.js @@ -109,12 +109,14 @@ const transformsByPropType = { {from: 'type', to: 'type', up: () => 'action'}, ], 'attribute': [ - {from: 'baseValueCalculation', to: 'baseValue.calculation'}, + // from: baseValue must be first or else it will delete the field we need {from: 'baseValue', to: 'baseValue.value', up: nanToNull}, + {from: 'baseValueCalculation', to: 'baseValue.calculation', up: calculationUp, down: calculationDown}, {from: 'baseValueErrors', to: 'baseValue.errors', up: trimErrors}, ...getComputedPropertyTransforms('spellSlotLevel'), ...getInlineComputationTransforms('description'), {from: 'value', to: 'total', up: nanToNull}, + {from: 'currentValue', to: 'value', up: nanToNull}, {from: 'proficiency', to: 'proficiency', up: stripZero}, ], 'buff': [ @@ -214,12 +216,12 @@ function getInlineComputationTransforms(key){ } function calculationUp(val){ - if (!val || !val.replace) return val; + if (typeof val !== 'string') return val; return val.replace('.value', '.total').replace('.currentValue', '.value'); } function calculationDown(val){ - if (!val || !val.replace) return val; + if (typeof val !== 'string') return val; return val.replace('.value', '.currentValue').replace('.total', '.value'); } diff --git a/app/imports/migrations/server/2.0-beta.33-dbv1.test.js b/app/imports/migrations/server/dbv1/dbv1.test.js similarity index 72% rename from app/imports/migrations/server/2.0-beta.33-dbv1.test.js rename to app/imports/migrations/server/dbv1/dbv1.test.js index fa82c911..13954186 100644 --- a/app/imports/migrations/server/2.0-beta.33-dbv1.test.js +++ b/app/imports/migrations/server/dbv1/dbv1.test.js @@ -1,5 +1,5 @@ import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js'; -import { migrateProperty } from './2.0-beta.33-dbv1.js'; +import { migrateProperty } from './dbv1.js'; import { assert } from 'chai'; const exampleAction = { @@ -63,6 +63,53 @@ const exampleAction = { ] }; +const exampleAttribute = { + _id:'idRWyoj5oxCv73feM', + name:'Hit Dice', + variableName:'clericHitDice', + attributeType:'hitDice', + type:'attribute', + hitDiceSize:'d8', + baseValueCalculation:'cleric.level', + parent:{'id':'8jSWKxvgQyKbunFtD','collection':'creatureProperties'}, + ancestors:[ + {'collection':'creatures','id':'m9sdCvs6iDf7qRaGv'}, + {'id':'8jSWKxvgQyKbunFtD','collection':'creatureProperties'} + ], + order: 84, + value: 20, + tags:[], + baseValue: 20, + damage: 3, + currentValue: 17, + constitutionMod: 2, + dependencies: ['8jSWKxvgQyKbunFtD','qPP5yQXPxS7uhuXo3'] +}; + +const expectedMigratedAttribute = { + _id:'idRWyoj5oxCv73feM', + name:'Hit Dice', + variableName:'clericHitDice', + attributeType:'hitDice', + type:'attribute', + hitDiceSize:'d8', + baseValue: { + calculation: 'cleric.level', + value: 20 + }, + parent:{'id':'8jSWKxvgQyKbunFtD','collection':'creatureProperties'}, + ancestors:[ + {'collection':'creatures','id':'m9sdCvs6iDf7qRaGv'}, + {'id':'8jSWKxvgQyKbunFtD','collection':'creatureProperties'} + ], + order: 84, + total: 20, + tags:[], + damage: 3, + value: 17, + constitutionMod: 2, +} + describe('migrateProperty', function () { it('Migrates actions reversibly', function () { const action = {...exampleAction}; @@ -78,4 +125,13 @@ describe('migrateProperty', function () { assert.deepEqual(action, exampleAction, 'action should not be bashed'); assert.deepEqual(exampleAction, reversedAction, 'operation should be reversible'); }); + it ('Migrates attributes as expected', function(){ + const attribute = {...exampleAttribute}; + const newAttribute = migrateProperty({ + collection: CreatureProperties, + prop: attribute + }); + assert.deepEqual(newAttribute, expectedMigratedAttribute, + 'Attribute should match the expected result'); + }); }); diff --git a/app/imports/migrations/server/index.js b/app/imports/migrations/server/index.js index 615035e1..74860df8 100644 --- a/app/imports/migrations/server/index.js +++ b/app/imports/migrations/server/index.js @@ -1 +1 @@ -import './2.0-beta.33-dbv1.js'; +import './v1/dbv1.js'; diff --git a/app/imports/migrations/server/transformFields.test.js b/app/imports/migrations/server/transformFields.test.js index b34d09e0..d3c1dd6f 100644 --- a/app/imports/migrations/server/transformFields.test.js +++ b/app/imports/migrations/server/transformFields.test.js @@ -44,6 +44,15 @@ describe('transformFields', function () { assert.equal(doc.name, 'DOC NAME', 'name in uppercase'); }); + it('Creates objects on the fly', function () { + let doc = {...originalDoc}; + const transformList = [ + {from: 'name', to:'newObj.name'}, + ]; + doc = transformFields(doc, transformList); + assert.deepEqual(doc.newObj, {name: 'doc name'}); + }); + it('Handles empty to and from fields', function () { let doc = {...originalDoc}; const transformList = [