From 1b101a15aa22348983daa0d2e7f2c4564cc4e096 Mon Sep 17 00:00:00 2001 From: Grant Forrest Date: Thu, 17 Oct 2024 22:16:23 -0400 Subject: [PATCH] some integration tests pass --- packages/store/src/entities/Entity.ts | 1 + packages/store/src/entities/EntityCache.ts | 9 -- .../persistence/idb/metadata/IdbMetadataDb.ts | 12 ++- .../idb/queries/migration/openWIPDatabase.ts | 97 ------------------- packages/store/src/sync/Sync.ts | 4 +- test/client/schemaVersions/index.d.ts | 8 +- test/tests/fuzz.test.ts | 3 - 7 files changed, 16 insertions(+), 118 deletions(-) delete mode 100644 packages/store/src/persistence/idb/queries/migration/openWIPDatabase.ts diff --git a/packages/store/src/entities/Entity.ts b/packages/store/src/entities/Entity.ts index ec298b3d..701683f2 100644 --- a/packages/store/src/entities/Entity.ts +++ b/packages/store/src/entities/Entity.ts @@ -628,6 +628,7 @@ export class Entity< } const file = this.files.get(child.id, { downloadRemote: !!fieldSchema.downloadRemote, + ctx: this.ctx, }); // FIXME: this seems bad and inconsistent diff --git a/packages/store/src/entities/EntityCache.ts b/packages/store/src/entities/EntityCache.ts index 048062e2..d5fc0cfb 100644 --- a/packages/store/src/entities/EntityCache.ts +++ b/packages/store/src/entities/EntityCache.ts @@ -26,15 +26,6 @@ export class EntityCache { return this.cache.has(oid); }; - getFile = (id: string, options: { downloadRemote: boolean }): EntityFile => { - if (this.cache.has(id)) { - return this.cache.get(id)! as EntityFile; - } - const file = new EntityFile(id, options); - this.cache.set(id, file); - return file; - }; - getCached = (oid: string) => { return this.cache.get(oid); }; diff --git a/packages/store/src/persistence/idb/metadata/IdbMetadataDb.ts b/packages/store/src/persistence/idb/metadata/IdbMetadataDb.ts index bfb50377..226633de 100644 --- a/packages/store/src/persistence/idb/metadata/IdbMetadataDb.ts +++ b/packages/store/src/persistence/idb/metadata/IdbMetadataDb.ts @@ -122,7 +122,7 @@ export class IdbMetadataDb extends IdbService implements PersistenceMetadataDb { updateLocalReplica = async ( data: Partial, - opts?: CommonQueryOptions, + opts: CommonQueryOptions = writeOpts, ): Promise => { const localReplicaInfo = await this.getLocalReplica(opts); Object.assign(localReplicaInfo, data); @@ -199,7 +199,7 @@ export class IdbMetadataDb extends IdbService implements PersistenceMetadataDb { setBaselines = async ( baselines: DocumentBaseline[], - opts?: CommonQueryOptions, + opts: CommonQueryOptions = writeOpts, ): Promise => { await this.runAll( 'baselines', @@ -210,7 +210,7 @@ export class IdbMetadataDb extends IdbService implements PersistenceMetadataDb { deleteBaseline = async ( oid: string, - opts?: CommonQueryOptions, + opts: CommonQueryOptions = writeOpts, ): Promise => { await this.run( 'baselines', @@ -269,7 +269,7 @@ export class IdbMetadataDb extends IdbService implements PersistenceMetadataDb { consumeEntityOperations = ( oid: string, iterator: Iterator, - opts?: CommonQueryOptions & { to?: string | null }, + opts: CommonQueryOptions & { to?: string | null } = writeOpts, ): Promise => { return this.iterate( 'operations', @@ -363,7 +363,7 @@ export class IdbMetadataDb extends IdbService implements PersistenceMetadataDb { addOperations = async ( ops: StoredClientOperation[], - opts?: CommonQueryOptions, + opts: CommonQueryOptions = writeOpts, ): Promise => { let affected = new Set(); await this.runAll( @@ -454,3 +454,5 @@ export class IdbMetadataDb extends IdbService implements PersistenceMetadataDb { }; }; } + +const writeOpts = { mode: 'readwrite' } as const; diff --git a/packages/store/src/persistence/idb/queries/migration/openWIPDatabase.ts b/packages/store/src/persistence/idb/queries/migration/openWIPDatabase.ts deleted file mode 100644 index 07b1c6d9..00000000 --- a/packages/store/src/persistence/idb/queries/migration/openWIPDatabase.ts +++ /dev/null @@ -1,97 +0,0 @@ -import { Migration } from '@verdant-web/common'; -import { Metadata } from '../metadata/Metadata.js'; -import { copyAll, getDatabaseVersion, openDatabase } from './db.js'; -import { runMigrations } from './migrations.js'; -import { getMigrationPath } from './paths.js'; -import { OpenDocumentDbContext } from './types.js'; - -const globalIDB = - typeof window !== 'undefined' ? window.indexedDB : (undefined as any); - -export async function openWIPDatabase({ - version, - indexedDB = globalIDB, - migrations, - meta, - context, - wipNamespace, -}: { - version: number; - migrations: Migration[]; - indexedDB?: IDBFactory; - meta: Metadata; - context: OpenDocumentDbContext; - wipNamespace: string; -}) { - context.log('debug', 'Opening WIP database', wipNamespace); - const currentWIPVersion = await getDatabaseVersion( - indexedDB, - wipNamespace, - version, - context.log, - ); - - if (currentWIPVersion === version) { - context.log('info', `WIP schema is up-to-date; not refreshing database`); - } else { - context.log('info', `WIP schema is out-of-date; refreshing database`); - - // first we need to copy the data from the production database to the WIP database - // at the current (non-wip) version. - - const initialToRun = getMigrationPath({ - currentVersion: currentWIPVersion, - targetVersion: version - 1, - migrations, - }); - - if (initialToRun.length > 0) { - await runMigrations({ - context, - toRun: initialToRun, - meta, - indexedDB, - namespace: wipNamespace, - }); - - // now, we copy the data from the main database. - const mainDatabase = await openDatabase({ - indexedDB, - namespace: context.namespace, - version: version - 1, - context, - }); - - const wipDatabase = await openDatabase({ - indexedDB, - namespace: wipNamespace, - version: version - 1, - context, - }); - await copyAll(mainDatabase, wipDatabase); - } - - const toRun = getMigrationPath({ - currentVersion: version - 1, - targetVersion: version, - migrations, - }); - - if (toRun.length > 0) { - await runMigrations({ - context, - toRun, - meta, - indexedDB, - namespace: wipNamespace, - }); - } - } - - return openDatabase({ - indexedDB, - namespace: wipNamespace, - version, - context, - }); -} diff --git a/packages/store/src/sync/Sync.ts b/packages/store/src/sync/Sync.ts index ce9ee89d..1916faad 100644 --- a/packages/store/src/sync/Sync.ts +++ b/packages/store/src/sync/Sync.ts @@ -438,7 +438,9 @@ export class ServerSync this.ctx.log( 'error', 'Failed to upload unsynced files', - results.filter((r) => r.status === 'rejected').map((r) => r.reason), + results + .filter((r): r is PromiseRejectedResult => r.status === 'rejected') + .map((r) => r.reason), ); } } diff --git a/test/client/schemaVersions/index.d.ts b/test/client/schemaVersions/index.d.ts index f539a117..3251fd36 100644 --- a/test/client/schemaVersions/index.d.ts +++ b/test/client/schemaVersions/index.d.ts @@ -1,3 +1,5 @@ -import { StorageSchema } from '@verdant-web/common'; -declare const versions: StorageSchema[]; -export default versions; + + import { StorageSchema } from '@verdant-web/common'; + declare const versions: StorageSchema[]; + export default versions; + \ No newline at end of file diff --git a/test/tests/fuzz.test.ts b/test/tests/fuzz.test.ts index d43ade07..f8f474a5 100644 --- a/test/tests/fuzz.test.ts +++ b/test/tests/fuzz.test.ts @@ -1,10 +1,8 @@ import { ReplicaType, Server } from '@verdant-web/server'; import { ClientWithCollections, - collection, createMigration, Entity, - migrate, schema, StorageDescriptor, } from '@verdant-web/store'; @@ -222,7 +220,6 @@ async function waitForConsistency( const fuzz2 = await getFuzz(client2); const fuzz1Pending = fuzz1.metadata.pendingOperations; const fuzz2Pending = fuzz2.metadata.pendingOperations; - debugger; return `[${debugTag}] consistency (${attempts} attempts): ${snap1}