Skip to content

Commit

Permalink
POC
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe Portner committed Jun 1, 2022
1 parent 7958fa0 commit 6b6c833
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<UpgradeableConfigType>({
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',
});
Expand Down

0 comments on commit 6b6c833

Please sign in to comment.