Throw error when migration not defined in same plugin as mappings (#31739) (#32411)

* Throw error when migration not defined in same plugin as mappings

* Apply PR feedback
This commit is contained in:
Mike Côté 2019-03-04 15:22:41 -05:00 committed by GitHub
parent 794a97cb9d
commit 53ab2c7beb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 64 additions and 2 deletions

View file

@ -90,5 +90,48 @@ describe('plugin discovery', () => {
},
});
});
it(`throws an error when migrations and mappings aren't defined in the same plugin`, () => {
const invalidSpecs = new PluginPack({
path: '/dev/null',
pkg: {
name: 'test',
version: 'kibana',
},
provider({ Plugin }) {
return [
new Plugin({
id: 'test',
uiExports: {
mappings: {
'test-type': {
properties: {},
},
},
},
}),
new Plugin({
id: 'test2',
uiExports: {
migrations: {
'test-type': {
'1.2.3': (doc) => {
return doc;
},
},
},
},
}),
];
},
}).getPluginSpecs();
expect(
() => collectUiExports(invalidSpecs),
).to.throwError((err) => {
expect(err).to.be.a(Error);
expect(err).to.have.property('message', 'Migrations and mappings must be defined together in the uiExports of a single plugin. ' +
'test2 defines migrations for types test-type but does not define their mappings.');
});
});
});
});

View file

@ -30,10 +30,29 @@ export const mappings = wrap(
flatConcatAtType
);
const pluginId = (pluginSpec) => pluginSpec.id ? pluginSpec.id() : pluginSpec.getId();
// Combines the `migrations` property of each plugin,
// ensuring that properties are unique across plugins.
// ensuring that properties are unique across plugins
// and has migrations defined where the mappings are defined.
// See saved_objects/migrations for more details.
export const migrations = wrap(alias('savedObjectMigrations'), uniqueKeys(), mergeAtType);
export const migrations = wrap(
alias('savedObjectMigrations'),
(next) => (acc, spec, type, pluginSpec) => {
const mappings = pluginSpec.getExportSpecs().mappings || {};
const invalidMigrationTypes = Object.keys(spec)
.filter(type => !mappings[type]);
if (invalidMigrationTypes.length) {
throw new Error(
'Migrations and mappings must be defined together in the uiExports of a single plugin. ' +
`${pluginId(pluginSpec)} defines migrations for types ${invalidMigrationTypes.join(', ')} but does not define their mappings.`
);
}
return next(acc, spec, type, pluginSpec);
},
uniqueKeys(),
mergeAtType,
);
export const savedObjectSchemas = wrap(uniqueKeys(), mergeAtType);