Skip to content
Open
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
27 changes: 27 additions & 0 deletions packages/core/src/stores/sync-store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,33 @@ describe("useSyncStore", () => {
const result = await useSyncStore.getState().syncNow("upload");

expect(syncMocks.runSimpleSync).toHaveBeenCalledWith(mockBackend, expect.any(Function), {
forceUploadSnapshot: true,
fileSyncOptions: {
forceUploadAll: true,
},
});
expect(result).toMatchObject({
success: true,
direction: "upload",
});
});

it("forceFullSync upload forces both database snapshot and file upload", async () => {
useSyncStore.setState({
config: baseConfig,
isConfigured: true,
backendType: "webdav",
});
mockPlatformService.kvGetItem.mockImplementation(async (key: string) =>
key === "sync_webdav_password" ? "secret" : null,
);

const result = await useSyncStore.getState().forceFullSync("upload");

expect(syncMocks.runSimpleSync).toHaveBeenCalledWith(mockBackend, expect.any(Function), {
receiveOnly: false,
forceApply: false,
forceUploadSnapshot: true,
fileSyncOptions: {
forceUploadAll: true,
},
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/stores/sync-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,7 @@ export const useSyncStore = create<SyncState>((set, get) => ({
}
: uploadOnly
? {
forceUploadSnapshot: true,
fileSyncOptions: {
forceUploadAll: true,
},
Expand Down Expand Up @@ -825,6 +826,7 @@ export const useSyncStore = create<SyncState>((set, get) => ({
disableUploads: true,
disableRemoteDeletes: true,
},
...(direction === "upload" ? { forceUploadSnapshot: true } : {}),
},
);

Expand Down
32 changes: 32 additions & 0 deletions packages/core/src/sync/__tests__/simple-sync.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,38 @@ describe("simple sync convergence", () => {
).toHaveProperty("books");
});

it("force-uploads a fresh full snapshot even when incremental changes are empty", async () => {
const backend = new MemoryBackend();
const deviceA = new FakeSyncDb();
deviceA.syncMetadata.set("last_sync_at", "5000");
deviceA.insert("books", bookRow({ title: "Latest PC title", updated_at: 1000 }));

backend.jsonFiles.set("/readany/sync/device-device-a.json", {
deviceId: "device-a",
timestamp: 5900,
since: 0,
tables: {
books: {
records: [bookRow({ title: "Old remote snapshot", updated_at: 1000 })],
deletedIds: [],
},
},
});

now = 6000;
dbMocks.currentDeviceId = "device-a";
dbMocks.currentDb = deviceA;
const result = await runSimpleSync(backend, undefined, { forceUploadSnapshot: true });

expect(result.success).toBe(true);
const snapshot = backend.jsonFiles.get("/readany/sync/device-device-a.json") as {
timestamp: number;
tables: { books?: { records: Row[] } };
};
expect(snapshot.timestamp).toBe(6000);
expect(snapshot.tables.books?.records[0]?.title).toBe("Latest PC title");
});

it("downloads remote snapshots using the listed path", async () => {
class AliasPathBackend extends MemoryBackend {
async listDir(path: string): Promise<RemoteFile[]> {
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/sync/simple-sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export interface SimpleSyncOptions {
receiveOnly?: boolean;
/** When true, bypass timestamp comparisons and force-apply all remote records */
forceApply?: boolean;
/** When true, always upload a fresh full local device snapshot. */
forceUploadSnapshot?: boolean;
fileSyncOptions?: SyncFilesOptions;
}

Expand Down Expand Up @@ -828,7 +830,7 @@ export async function runSimpleSync(
);

try {
if (changeCount > 0 || totalApplied > 0) {
if (options.forceUploadSnapshot || changeCount > 0 || totalApplied > 0) {
onProgress?.({
phase: "database",
operation: "upload",
Expand Down