From b8899e9a63a3daf8491b8b6f6cdb1ef5267599b6 Mon Sep 17 00:00:00 2001 From: Guillaume Chau Date: Wed, 18 Sep 2024 16:24:46 +0200 Subject: [PATCH] feat: throw if need to migrate snapshot but cant write --- packages/core/src/snapshot/addResourcesToSnapshot.ts | 2 +- packages/core/src/snapshot/migrate.ts | 7 ++++++- packages/core/src/snapshot/readResources.ts | 2 +- packages/core/src/snapshot/removeResourcesFromSnapshot.ts | 2 +- 4 files changed, 9 insertions(+), 4 deletions(-) diff --git a/packages/core/src/snapshot/addResourcesToSnapshot.ts b/packages/core/src/snapshot/addResourcesToSnapshot.ts index f74d24a..9ce14dc 100644 --- a/packages/core/src/snapshot/addResourcesToSnapshot.ts +++ b/packages/core/src/snapshot/addResourcesToSnapshot.ts @@ -18,7 +18,7 @@ export async function addResourcesToSnapshot(mq: MoquerieInstance, options: AddR const snapshotFolder = await getSnapshotFolder(mq, snapshot) - await migrateSnapshotFolder(snapshotFolder) + await migrateSnapshotFolder(mq, snapshotFolder) for (const resourceName in resourceIds) { const sourceFolder = path.join(getCurrentBranchFolder(mq), resourceName) diff --git a/packages/core/src/snapshot/migrate.ts b/packages/core/src/snapshot/migrate.ts index e71deb7..928f8cf 100644 --- a/packages/core/src/snapshot/migrate.ts +++ b/packages/core/src/snapshot/migrate.ts @@ -2,13 +2,14 @@ import fs from 'node:fs' import path from 'pathe' import glob from 'fast-glob' import SuperJSON from 'superjson' +import type { MoquerieInstance } from '../instance.js' /** * Migrate snapshot folder to new format with each resource type in a single file * @param snapshotFolder The snapshot folder to migrate * @returns Whether the folder was migrated */ -export async function migrateSnapshotFolder(snapshotFolder: string) { +export async function migrateSnapshotFolder(mq: MoquerieInstance, snapshotFolder: string) { const files = await glob('*/*.json', { cwd: snapshotFolder, onlyFiles: true, @@ -18,6 +19,10 @@ export async function migrateSnapshotFolder(snapshotFolder: string) { return false } + if (mq.data.skipWrites) { + throw new Error('Cannot migrate snapshot folder in read-only mode') + } + const dataPerResource = new Map>() for (const file of files) { diff --git a/packages/core/src/snapshot/readResources.ts b/packages/core/src/snapshot/readResources.ts index 59f13fd..3708408 100644 --- a/packages/core/src/snapshot/readResources.ts +++ b/packages/core/src/snapshot/readResources.ts @@ -70,7 +70,7 @@ export async function readSnapshotResources(mq: MoquerieInstance, snapshot: Data export async function readSnapshotAllResources(mq: MoquerieInstance, snapshot: DatabaseSnapshot) { const folder = await getSnapshotFolder(mq, snapshot) - await migrateSnapshotFolder(folder) + await migrateSnapshotFolder(mq, folder) const result: { [resourceName: string]: ResourceInstance[] } = {} diff --git a/packages/core/src/snapshot/removeResourcesFromSnapshot.ts b/packages/core/src/snapshot/removeResourcesFromSnapshot.ts index 62c2ac7..f649a70 100644 --- a/packages/core/src/snapshot/removeResourcesFromSnapshot.ts +++ b/packages/core/src/snapshot/removeResourcesFromSnapshot.ts @@ -16,7 +16,7 @@ export async function removeResourcesFromSnapshot(mq: MoquerieInstance, options: // Delete resources const snapshotFolder = await getSnapshotFolder(mq, snapshot) - await migrateSnapshotFolder(snapshotFolder) + await migrateSnapshotFolder(mq, snapshotFolder) for (const resourceName in resourceIds) { const ids = resourceIds[resourceName]