Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/legacy/ui/ui_exports/__tests__/collect_ui_exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
});
});
});
});
23 changes: 21 additions & 2 deletions src/legacy/ui/ui_exports/ui_export_types/saved_object.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down