diff --git a/lib/util/cache/repository/index.spec.ts b/lib/util/cache/repository/index.spec.ts index 50afc037117ac6da3e01c700f2e6cbb6a019a96b..911987beb59bd6a33c2950a943b7482f141400f2 100644 --- a/lib/util/cache/repository/index.spec.ts +++ b/lib/util/cache/repository/index.spec.ts @@ -38,28 +38,16 @@ describe('lib/util/cache/repository', () => { }); }); it('reads from cache and finalizes', async () => { - fs.readFile.mockResolvedValueOnce('{"repository":"abc/def"}' as any); - await repositoryCache.initialize({ - ...config, - repositoryCache: 'enabled', - }); - await repositoryCache.finalize(); - expect(fs.readFile.mock.calls).toHaveLength(1); - expect(fs.outputFile.mock.calls).toHaveLength(1); - }); - it('migrates', async () => { fs.readFile.mockResolvedValueOnce( - '{"repository":"abc/def","branches":[{"upgrades":[{"fromVersion":"1.0.0","toVersion":"1.0.1"}]}]}' as any + `{"repository":"abc/def","revision":${repositoryCache.CACHE_REVISION}}` as any ); await repositoryCache.initialize({ ...config, repositoryCache: 'enabled', }); - expect(repositoryCache.getCache().branches[0].upgrades[0]).toEqual({ - currentVersion: '1.0.0', - newVersion: '1.0.1', - }); await repositoryCache.finalize(); + expect(fs.readFile.mock.calls).toHaveLength(1); + expect(fs.outputFile.mock.calls).toHaveLength(1); }); it('gets', () => { expect(repositoryCache.getCache()).toEqual({ scan: {} }); diff --git a/lib/util/cache/repository/index.ts b/lib/util/cache/repository/index.ts index 4c98c4f9093a3f3dbe45f354327fd508efaa88f4..c84643901f1a7f0f27ae6ac7fee7164f14ebef70 100644 --- a/lib/util/cache/repository/index.ts +++ b/lib/util/cache/repository/index.ts @@ -5,6 +5,9 @@ import { logger } from '../../../logger'; import { PackageFile } from '../../../manager/common'; import { RepoInitConfig } from '../../../workers/repository/init/common'; +// Increment this whenever there could be incompatibilities between old and new cache structure +export const CACHE_REVISION = 1; + export interface BaseBranchCache { sha: string; // branch commit sha configHash: string; // object hash of config @@ -38,6 +41,7 @@ export interface BranchCache { export interface Cache { branches?: BranchCache[]; repository?: string; + revision?: number; init?: RepoInitConfig; scan?: Record<string, BaseBranchCache>; } @@ -56,7 +60,11 @@ export function getCacheFileName(config: RenovateConfig): string { } function validate(config: RenovateConfig, input: any): Cache | null { - if (input?.repository === config.repository) { + if ( + input && + input.repository === config.repository && + input.revision === CACHE_REVISION + ) { logger.debug('Repository cache is valid'); return input as Cache; } @@ -79,27 +87,14 @@ export async function initialize(config: RenovateConfig): Promise<void> { } catch (err) { logger.debug({ cacheFileName }, 'Repository cache not found'); } - cache = cache || Object.create({}); + cache = cache || Object.create({ revision: CACHE_REVISION }); cache.repository = config.repository; } export function getCache(): Cache { - cache = cache || Object.create({}); + cache = cache || Object.create({ revision: CACHE_REVISION }); delete cache.init; cache.scan = cache.scan || Object.create({}); - for (const branch of cache.branches || []) { - for (const upgrade of (branch.upgrades || []) as any) { - // migrate fromVersion to currentVersion - if (upgrade.fromVersion) { - upgrade.currentVersion = upgrade.fromVersion; - delete upgrade.fromVersion; - } - if (upgrade.toVersion) { - upgrade.newVersion = upgrade.toVersion; - delete upgrade.toVersion; - } - } - } return cache; }