From 6b6c8332c7a5740110fef58909b3a0891be3e409 Mon Sep 17 00:00:00 2001 From: Joe Portner Date: Wed, 1 Jun 2022 14:12:21 -0400 Subject: [PATCH] POC --- .../create_or_upgrade_saved_config.ts | 40 ++++++++++++++++++- .../get_upgradeable_config.ts | 12 +++++- 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/src/core/server/ui_settings/create_or_upgrade_saved_config/create_or_upgrade_saved_config.ts b/src/core/server/ui_settings/create_or_upgrade_saved_config/create_or_upgrade_saved_config.ts index d55a6537957cbd..3853a49d89dd8a 100644 --- a/src/core/server/ui_settings/create_or_upgrade_saved_config/create_or_upgrade_saved_config.ts +++ b/src/core/server/ui_settings/create_or_upgrade_saved_config/create_or_upgrade_saved_config.ts @@ -39,10 +39,46 @@ export async function createOrUpgradeSavedConfig( version, }); + let defaultIndex: string | undefined; + let isDefaultIndexMigrated = false; + if ( + upgradeableConfig?.attributes.defaultIndex && + !upgradeableConfig.attributes.isDefaultIndexMigrated + ) { + defaultIndex = upgradeableConfig.attributes.defaultIndex; // Keep the existing defaultIndex attribute if the data view is not found + try { + // The defaultIndex for this config object was created prior to 8.3, and it might refer to a data view ID that is no longer valid. + // We should try to resolve the data view and change the defaultIndex to the new ID, if necessary. + const resolvedIndex = await savedObjectsClient.resolve('index-pattern', defaultIndex); + if (resolvedIndex.outcome === 'aliasMatch' || resolvedIndex.outcome === 'conflict') { + // This resolved to an aliasMatch or conflict outcome; that means we should change the defaultIndex to the data view's new ID. + // Note, the alias_target_id field is guaranteed to exist iff the resolve outcome is aliasMatch or conflict. + defaultIndex = resolvedIndex.alias_target_id!; + isDefaultIndexMigrated = true; + } + } catch (err) { + // If the defaultIndex is not found at all, it will throw a Not Found error and we should mark the defaultIndex attribute as upgraded. + if (SavedObjectsErrorHelpers.isNotFoundError(err)) { + isDefaultIndexMigrated = true; + } + // For any other error, leave it unchanged so we can try to upgrade this attribute again in the future. + } + } + // default to the attributes of the upgradeableConfig if available const attributes = defaults( - { buildNum }, - upgradeableConfig ? (upgradeableConfig.attributes as any) : {} + { + buildNum, + ...(isDefaultIndexMigrated && { + // We migrated the defaultIndex attribute for this config, make sure these two fields take precedence over any in the old config + defaultIndex, + isDefaultIndexMigrated, + }), + }, + upgradeableConfig ?? { + // We didn't find an upgradeable config, so this is a fresh one; mark the defaultIndex as migrated so we don't change it later! + isDefaultIndexMigrated: true, + } ); try { diff --git a/src/core/server/ui_settings/create_or_upgrade_saved_config/get_upgradeable_config.ts b/src/core/server/ui_settings/create_or_upgrade_saved_config/get_upgradeable_config.ts index 493efa518f22ba..09d7a17db7616c 100644 --- a/src/core/server/ui_settings/create_or_upgrade_saved_config/get_upgradeable_config.ts +++ b/src/core/server/ui_settings/create_or_upgrade_saved_config/get_upgradeable_config.ts @@ -9,6 +9,15 @@ import { SavedObjectsClientContract } from '../../saved_objects/types'; import { isConfigVersionUpgradeable } from './is_config_version_upgradeable'; +/** + * This contains a subset of `config` object attributes that are relevant for upgrading it. + */ +export interface UpgradeableConfigType { + buildNum: string; + defaultIndex: string | undefined; + isDefaultIndexMigrated: boolean | undefined; +} + /** * Find the most recent SavedConfig that is upgradeable to the specified version * @param {Object} options @@ -24,10 +33,11 @@ export async function getUpgradeableConfig({ version: string; }) { // attempt to find a config we can upgrade - const { saved_objects: savedConfigs } = await savedObjectsClient.find({ + const { saved_objects: savedConfigs } = await savedObjectsClient.find({ type: 'config', page: 1, perPage: 1000, + fields: ['buildNum', 'defaultIndex', 'isDefaultIndexMigrated'], // Optimization: we only need these type-level fields, don't return anything else sortField: 'buildNum', sortOrder: 'desc', });