From f249be392f12c74d67e9be618e9785e0462723bc Mon Sep 17 00:00:00 2001 From: "Fred K. Schott" Date: Wed, 24 Jan 2024 16:41:19 -0800 Subject: [PATCH] improve snapshot parsing support --- packages/db/src/migrations.ts | 15 ++++++++++----- packages/db/src/types.ts | 7 +++++-- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/packages/db/src/migrations.ts b/packages/db/src/migrations.ts index 26aa4bb3ae..cb51d61774 100644 --- a/packages/db/src/migrations.ts +++ b/packages/db/src/migrations.ts @@ -20,10 +20,15 @@ export async function loadMigration(migration: string): Promise<{ diff: any[]; d export async function loadInitialSnapshot(): Promise { const snapshot = JSON.parse(await readFile('./migrations/0000_snapshot.json', 'utf-8')); - if (!snapshot.version) { - return { version: 2, meta: {}, schema: snapshot }; + // `experimentalVersion: 1` -- added the version field + if (snapshot.experimentalVersion === 1) { + return snapshot; } - return snapshot; + // `experimentalVersion: 0` -- initial format + if (!snapshot.schema) { + return { experimentalVersion: 1, schema: snapshot }; + } + throw new Error('Invalid snapshot format'); } export async function initializeMigrationsDirectory(currentSnapshot: DBSnapshot) { @@ -45,8 +50,8 @@ export async function initializeFromMigrations(allMigrationFiles: string[]): Pro export function createCurrentSnapshot(config: AstroConfig): DBSnapshot { const schema = JSON.parse(JSON.stringify(config.db?.collections ?? {})); - return { version: 2, meta: {}, schema }; + return { experimentalVersion: 1, schema }; } export function createEmptySnapshot(): DBSnapshot { - return { version: 2, meta: {}, schema: {} }; + return { experimentalVersion: 1, schema: {} }; } diff --git a/packages/db/src/types.ts b/packages/db/src/types.ts index 08ff59aac7..5f408b1d45 100644 --- a/packages/db/src/types.ts +++ b/packages/db/src/types.ts @@ -98,9 +98,12 @@ export type DBCollection = z.infer< >; export type DBCollections = Record; export type DBSnapshot = { - version: number; - meta: Record; schema: Record; + /** + * Snapshot version. Breaking changes to the snapshot format increment this number. + * @todo Rename to "version" once closer to release. + */ + experimentalVersion: number; }; export type ReadableDBCollection = z.infer; export type WritableDBCollection = z.infer;