0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-02-03 22:29:08 -05:00

improve snapshot parsing support

This commit is contained in:
Fred K. Schott 2024-01-24 16:41:19 -08:00
parent 7c786276f5
commit f249be392f
2 changed files with 15 additions and 7 deletions

View file

@ -20,10 +20,15 @@ export async function loadMigration(migration: string): Promise<{ diff: any[]; d
export async function loadInitialSnapshot(): Promise<DBSnapshot> {
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: {} };
}

View file

@ -98,9 +98,12 @@ export type DBCollection = z.infer<
>;
export type DBCollections = Record<string, DBCollection>;
export type DBSnapshot = {
version: number;
meta: Record<string, never>;
schema: Record<string, DBCollection>;
/**
* 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<typeof readableCollectionSchema>;
export type WritableDBCollection = z.infer<typeof writableCollectionSchema>;