mirror of
https://github.com/withastro/astro.git
synced 2025-02-10 22:38:53 -05:00
improve snapshot parsing support
This commit is contained in:
parent
7c786276f5
commit
f249be392f
2 changed files with 15 additions and 7 deletions
|
@ -20,10 +20,15 @@ export async function loadMigration(migration: string): Promise<{ diff: any[]; d
|
||||||
|
|
||||||
export async function loadInitialSnapshot(): Promise<DBSnapshot> {
|
export async function loadInitialSnapshot(): Promise<DBSnapshot> {
|
||||||
const snapshot = JSON.parse(await readFile('./migrations/0000_snapshot.json', 'utf-8'));
|
const snapshot = JSON.parse(await readFile('./migrations/0000_snapshot.json', 'utf-8'));
|
||||||
if (!snapshot.version) {
|
// `experimentalVersion: 1` -- added the version field
|
||||||
return { version: 2, meta: {}, schema: snapshot };
|
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) {
|
export async function initializeMigrationsDirectory(currentSnapshot: DBSnapshot) {
|
||||||
|
@ -45,8 +50,8 @@ export async function initializeFromMigrations(allMigrationFiles: string[]): Pro
|
||||||
|
|
||||||
export function createCurrentSnapshot(config: AstroConfig): DBSnapshot {
|
export function createCurrentSnapshot(config: AstroConfig): DBSnapshot {
|
||||||
const schema = JSON.parse(JSON.stringify(config.db?.collections ?? {}));
|
const schema = JSON.parse(JSON.stringify(config.db?.collections ?? {}));
|
||||||
return { version: 2, meta: {}, schema };
|
return { experimentalVersion: 1, schema };
|
||||||
}
|
}
|
||||||
export function createEmptySnapshot(): DBSnapshot {
|
export function createEmptySnapshot(): DBSnapshot {
|
||||||
return { version: 2, meta: {}, schema: {} };
|
return { experimentalVersion: 1, schema: {} };
|
||||||
}
|
}
|
||||||
|
|
|
@ -98,9 +98,12 @@ export type DBCollection = z.infer<
|
||||||
>;
|
>;
|
||||||
export type DBCollections = Record<string, DBCollection>;
|
export type DBCollections = Record<string, DBCollection>;
|
||||||
export type DBSnapshot = {
|
export type DBSnapshot = {
|
||||||
version: number;
|
|
||||||
meta: Record<string, never>;
|
|
||||||
schema: Record<string, DBCollection>;
|
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 ReadableDBCollection = z.infer<typeof readableCollectionSchema>;
|
||||||
export type WritableDBCollection = z.infer<typeof writableCollectionSchema>;
|
export type WritableDBCollection = z.infer<typeof writableCollectionSchema>;
|
||||||
|
|
Loading…
Add table
Reference in a new issue