Repository navigation
Add pull-only CLI command for backups #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+240
−2
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import type { CliAppContext } from "../app/context"; | ||
|
|
||
| export async function runPull(ctx: CliAppContext): Promise<number> { | ||
| await ctx.initializeAuth(); | ||
| ctx.requireVerifiedAuth(); | ||
| await ctx.openVaultSession(); | ||
|
|
||
| ctx.logger.log(`Pulling remote changes into ${ctx.vaultPath} ...`); | ||
| await ctx.engine.pullOnlyOnce(); | ||
| ctx.logger.log( | ||
| `Pull complete (${ctx.syncProgress.completedEntries}/${ctx.syncProgress.totalEntries} entries).`, | ||
| ); | ||
| return 0; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
packages/sync-client/src/sync/engine/__tests__/auto-sync/pull-only.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| import { describe, expect, it, vi } from "vitest"; | ||
|
|
||
| import { createTestSyncStore } from "../../../../test-support/in-memory-sync-store"; | ||
| import { SyncAutoLoop } from "../../auto-sync"; | ||
| import { | ||
| createPushResult, | ||
| createRealtimeClient, | ||
| createToken, | ||
| } from "./helpers"; | ||
|
|
||
| describe("SyncAutoLoop pull-only", () => { | ||
| it("pulls once without scheduling pending local mutations for push", async () => { | ||
| const store = createTestSyncStore(); | ||
| await store.setCursor(4); | ||
| const pullOnce = vi.fn(async () => {}); | ||
| const pushPendingMutations = vi.fn(async () => createPushResult()); | ||
| let sessionClosed = false; | ||
| const realtimeClient = createRealtimeClient( | ||
| undefined, | ||
| (session) => { | ||
| session.close = () => { | ||
| sessionClosed = true; | ||
| }; | ||
| }, | ||
| 7, | ||
| ); | ||
| const openSession = vi.fn(realtimeClient.openSession); | ||
| const autoLoop = new SyncAutoLoop({ | ||
| getApiBaseUrl: () => "http://127.0.0.1:8787", | ||
| getSyncToken: async () => createToken(), | ||
| getSyncStore: () => store, | ||
| pushPendingMutations, | ||
| pullOnce, | ||
| realtimeClient: { openSession }, | ||
| }); | ||
|
|
||
| await autoLoop.pullOnlyOnce(); | ||
|
|
||
| expect(openSession).toHaveBeenCalledWith( | ||
| "http://127.0.0.1:8787", | ||
| expect.objectContaining({ vaultId: "vault-1" }), | ||
| 4, | ||
| expect.any(Object), | ||
| ); | ||
| expect(pullOnce).toHaveBeenCalledTimes(1); | ||
| expect(pushPendingMutations).not.toHaveBeenCalled(); | ||
| expect(sessionClosed).toBe(true); | ||
| await store.close(); | ||
| }); | ||
|
|
||
| it("refuses to run alongside the auto-sync loop", async () => { | ||
| const store = createTestSyncStore(); | ||
| const autoLoop = new SyncAutoLoop({ | ||
| getApiBaseUrl: () => "http://127.0.0.1:8787", | ||
| getSyncToken: async () => createToken(), | ||
| getSyncStore: () => store, | ||
| pushPendingMutations: vi.fn(async () => createPushResult()), | ||
| pullOnce: vi.fn(async () => {}), | ||
| realtimeClient: createRealtimeClient(), | ||
| }); | ||
|
|
||
| await autoLoop.start(); | ||
| await expect(autoLoop.pullOnlyOnce()).rejects.toThrow( | ||
| "requires the auto-sync loop and all in-flight sync work to be stopped", | ||
| ); | ||
| autoLoop.stop(); | ||
| await store.close(); | ||
| }); | ||
|
|
||
| it("refuses to pull when the store contains pending local changes", async () => { | ||
| const store = createTestSyncStore(); | ||
| await store.markEntryDirty({ | ||
| mutationId: "mutation-1", | ||
| entryId: "entry-1", | ||
| op: "delete", | ||
| baseRevision: 0, | ||
| blobId: null, | ||
| hash: null, | ||
| encryptedMetadata: "encrypted-metadata", | ||
| createdAt: 1, | ||
| }); | ||
| const pullOnce = vi.fn(async () => {}); | ||
| const openSession = vi.fn(createRealtimeClient().openSession); | ||
| const autoLoop = new SyncAutoLoop({ | ||
| getApiBaseUrl: () => "http://127.0.0.1:8787", | ||
| getSyncToken: async () => createToken(), | ||
| getSyncStore: () => store, | ||
| pushPendingMutations: vi.fn(async () => createPushResult()), | ||
| pullOnce, | ||
| realtimeClient: { openSession }, | ||
| }); | ||
|
|
||
| await expect(autoLoop.pullOnlyOnce()).rejects.toThrow( | ||
| "no pending local changes", | ||
| ); | ||
| expect(openSession).not.toHaveBeenCalled(); | ||
| expect(pullOnce).not.toHaveBeenCalled(); | ||
| await store.close(); | ||
| }); | ||
|
|
||
| it("explains how to rebuild state when the local cursor is ahead", async () => { | ||
| const store = createTestSyncStore(); | ||
| await store.setCursor(8); | ||
| const autoLoop = new SyncAutoLoop({ | ||
| getApiBaseUrl: () => "http://127.0.0.1:8787", | ||
| getSyncToken: async () => createToken(), | ||
| getSyncStore: () => store, | ||
| pushPendingMutations: vi.fn(async () => createPushResult()), | ||
| pullOnce: vi.fn(async () => {}), | ||
| realtimeClient: createRealtimeClient(undefined, undefined, 7), | ||
| }); | ||
|
|
||
| await expect(autoLoop.pullOnlyOnce()).rejects.toThrow( | ||
| "Move .synch/sync.sqlite aside", | ||
| ); | ||
| await store.close(); | ||
| }); | ||
|
|
||
| it("propagates asynchronous session errors and still closes the session", async () => { | ||
| const store = createTestSyncStore(); | ||
| let sessionClosed = false; | ||
| const sessionError = new Error("session failed"); | ||
| const autoLoop = new SyncAutoLoop({ | ||
| getApiBaseUrl: () => "http://127.0.0.1:8787", | ||
| getSyncToken: async () => createToken(), | ||
| getSyncStore: () => store, | ||
| pushPendingMutations: vi.fn(async () => createPushResult()), | ||
| pullOnce: vi.fn(async () => {}), | ||
| realtimeClient: createRealtimeClient( | ||
| (callbacks) => callbacks.onError(sessionError), | ||
| (session) => { | ||
| session.close = () => { | ||
| sessionClosed = true; | ||
| }; | ||
| }, | ||
| ), | ||
| }); | ||
|
|
||
| await expect(autoLoop.pullOnlyOnce()).rejects.toThrow("session failed"); | ||
| expect(sessionClosed).toBe(true); | ||
| await store.close(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.