Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions projects/kit/offline/src/lib/sqlite-offline-repository.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,49 @@ describe('SqliteOfflineRepository community sqlite driver', () => {
).toBe(false);
});

it('initial revision read失敗後もreader leaseとwrite gateを解放する', async () => {
const repository = createRepository();
await repository.initialize();
const operation = vi.fn(async () => undefined);
plugin.query.mockImplementation(async ({ statement }: { statement: string }) => {
if (statement === 'PRAGMA data_version') throw new Error('temporary SQLite read failure');
if (statement.includes('offline_replica_schema_metadata')) {
return {
columns: ['version', 'schema_hash'],
rows: [[storedReplicaMetadata!.version, storedReplicaMetadata!.schemaHash]],
};
}
if (statement.startsWith('PRAGMA table_info')) return { rows: [{ name: 'next_local_id' }] };
return { rows: [] };
});

await expect(repository[OFFLINE_REPOSITORY_ATOMIC_MUTATION]!(operation)).rejects.toThrow('temporary SQLite read failure');
expect(operation).not.toHaveBeenCalled();

plugin.query.mockImplementation(async ({ statement }: { statement: string }) => {
if (statement === 'PRAGMA data_version') return { columns: ['data_version'], rows: [[1]] };
return { rows: [] };
});
await expect(
repository.putCommand({
userId: 1,
scopeId: '10',
commandId: 'after-startup-failure',
aggregateType: 'test_items',
sourceKey: 'test_items',
identity: { kind: 'generated', localId: '019d-after-failure' },
operation: 'test_items.update',
payload: {},
baseRevision: null,
state: 'pending',
attempts: 0,
retryAt: null,
createdAt: 1,
lastErrorCode: null,
}),
).resolves.toBeUndefined();
});

it('guarded commitはwrite lockをrevision確認より先に取得し、確認後にreplica mutationを適用する', async () => {
plugin.query.mockImplementation(async ({ statement }: { statement: string }) => {
if (statement === 'PRAGMA data_version') return { columns: ['data_version'], rows: [[1]] };
Expand Down
14 changes: 7 additions & 7 deletions projects/kit/offline/src/lib/sqlite-offline-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,14 +328,14 @@ export class SqliteOfflineRepository implements OfflineRepository {
throw new Error('Nested offline replica atomic mutations are not supported.');
}
this.#beginReaders();
const databaseId = await this.#databaseConnection();
this.#atomicOperations = Promise.resolve();
this.#atomicMutationCommitted = false;
this.#atomicIdle = new Promise<void>((resolve) => {
this.#resolveAtomicIdle = resolve;
});
this.#atomicMutationRevision = await this.#nativeTransaction(databaseId, () => this.#dataVersion(databaseId));
try {
const databaseId = await this.#databaseConnection();
this.#atomicOperations = Promise.resolve();
this.#atomicMutationCommitted = false;
this.#atomicIdle = new Promise<void>((resolve) => {
this.#resolveAtomicIdle = resolve;
});
this.#atomicMutationRevision = await this.#nativeTransaction(databaseId, () => this.#dataVersion(databaseId));
const result = await operation(this.#atomicRepository());
await this.#atomicOperations;
if (!this.#atomicMutationCommitted) {
Expand Down