Skip to content

Commit

Permalink
Tweak migration loop
Browse files Browse the repository at this point in the history
1. The migration loop no longer resets if the object does not have
a [type] field. Objects never have this field after being
deserialized from their raw document format, this functionality
appears to be unnecessary and can cause unexpected behavior
(though migrations still worked because the loop would get
restarted).
2. Changed one unit test case that depended upon the aforementioned
condition. This appeared to be an invalid test case which would not
actually be encountered in a real world scenario, since it relied
on an object containing a [type] field.
3. The migration loop now resets if an object's migrationVersion
has explicitly been increased by a transform function. This is
expected behavior and a unit test exercises it, but it was
a side effect of the aforementioned condition.
  • Loading branch information
jportner committed Dec 2, 2020
1 parent 1b0165b commit e27e6be
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -382,36 +382,26 @@ describe('DocumentMigrator', () => {
it('allows props to be renamed', () => {
const migrator = new DocumentMigrator({
...testOpts(),
typeRegistry: createRegistry(
{
name: 'animal',
migrations: {
'1.0.0': setAttr('animal', (name: string) => `Animal: ${name}`),
'3.2.1': renameAttr('animal', 'dawg'),
},
typeRegistry: createRegistry({
name: 'dog',
migrations: {
'1.0.0': setAttr('attributes.name', (name: string) => `Name: ${name}`),
'1.0.1': renameAttr('attributes.name', 'attributes.title'),
'1.0.2': setAttr('attributes.title', (name: string) => `Title: ${name}`),
},
{
name: 'dawg',
migrations: {
'2.2.4': renameAttr('dawg', 'animal'),
'3.2.0': setAttr('dawg', (name: string) => `Dawg3.x: ${name}`),
},
}
),
}),
});
const actual = migrator.migrate({
id: 'smelly',
type: 'foo',
type: 'dog',
attributes: { name: 'Callie' },
dawg: 'Yo',
migrationVersion: {},
} as SavedObjectUnsanitizedDoc);
});
expect(actual).toEqual({
id: 'smelly',
type: 'foo',
attributes: { name: 'Callie' },
dawg: 'Dawg3.x: Animal: Yo',
migrationVersion: { animal: '3.2.1', dawg: '3.2.0' },
type: 'dog',
attributes: { title: 'Title: Name: Callie' },
migrationVersion: { dog: '1.0.2' },
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,14 +361,20 @@ function migrateProp(
): SavedObjectUnsanitizedDoc {
const originalType = doc.type;
let migrationVersion = _.clone(doc.migrationVersion) || {};
const typeChanged = () => !doc.hasOwnProperty(prop) || doc.type !== originalType;

for (const { version, transform } of applicableTransforms(migrations, doc, prop)) {
const currentVersion = propVersion(doc, prop);
if (currentVersion && Semver.gt(currentVersion, version)) {
// the previous transform function increased the object's migrationVersion; break out of the loop
break;
}

doc = transform(doc);
migrationVersion = updateMigrationVersion(doc, migrationVersion, prop, version);
doc.migrationVersion = _.clone(migrationVersion);

if (typeChanged()) {
if (doc.type !== originalType) {
// the transform function changed the object's type; break out of the loop
break;
}
}
Expand Down

0 comments on commit e27e6be

Please sign in to comment.