Skip to content

Commit

Permalink
feat: throw if need to migrate snapshot but cant write
Browse files Browse the repository at this point in the history
  • Loading branch information
Akryum committed Sep 18, 2024
1 parent 55c205b commit b8899e9
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/snapshot/addResourcesToSnapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion packages/core/src/snapshot/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<string, Record<string, any>>()

for (const file of files) {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/snapshot/readResources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] } = {}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/snapshot/removeResourcesFromSnapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit b8899e9

Please sign in to comment.