Skip to content

Commit 7457564

Browse files
snapshotId unique for each client change
1 parent 3773545 commit 7457564

File tree

14 files changed

+56
-12
lines changed

14 files changed

+56
-12
lines changed

apps/client/src/lib/schema.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ClientId, WorkspaceId } from "@local/sync";
1+
import { ClientId, SnapshotId, WorkspaceId } from "@local/sync";
22
import { Snapshot } from "@local/sync/loro";
33
import { Schema } from "effect";
44

@@ -21,4 +21,5 @@ export class TempWorkspaceTable extends Schema.Class<TempWorkspaceTable>(
2121
)({
2222
workspaceId: WorkspaceId,
2323
snapshot: Snapshot,
24+
snapshotId: SnapshotId,
2425
}) {}

apps/client/src/lib/services/loro-storage.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ export class LoroStorage extends Effect.Service<LoroStorage>()("LoroStorage", {
6464
from: new VersionVector(workspace.version),
6565
});
6666

67-
return yield* temp.put({ workspaceId, snapshot: snapshotExport });
67+
return yield* temp.put({
68+
workspaceId,
69+
snapshot: snapshotExport,
70+
snapshotId: crypto.randomUUID(),
71+
});
6872
});
6973

7074
return {

apps/client/src/lib/services/sync.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { Effect } from "effect";
2+
import { LoroDoc } from "loro-crdt";
23
import { ApiClient } from "../api-client";
34
import { Dexie } from "../dexie";
45
import type { WorkspaceTable } from "../schema";
56
import { TempWorkspace } from "./temp-workspace";
67
import { WorkspaceManager } from "./workspace-manager";
7-
import { LoroDoc } from "loro-crdt";
88

99
export class Sync extends Effect.Service<Sync>()("Sync", {
1010
dependencies: [
@@ -23,8 +23,10 @@ export class Sync extends Effect.Service<Sync>()("Sync", {
2323
push: ({
2424
snapshot,
2525
workspace,
26+
snapshotId,
2627
}: {
2728
workspace: WorkspaceTable;
29+
snapshotId: string;
2830
snapshot: globalThis.Uint8Array;
2931
}) =>
3032
Effect.gen(function* () {
@@ -38,7 +40,7 @@ export class Sync extends Effect.Service<Sync>()("Sync", {
3840
path: {
3941
workspaceId: workspace.workspaceId,
4042
},
41-
payload: { clientId, snapshot },
43+
payload: { clientId, snapshot, snapshotId },
4244
})
4345
.pipe(
4446
Effect.map((response) => ({
@@ -53,6 +55,7 @@ export class Sync extends Effect.Service<Sync>()("Sync", {
5355
payload: {
5456
clientId,
5557
snapshot,
58+
snapshotId,
5659
workspaceId: workspace.workspaceId,
5760
},
5861
})

apps/client/src/workers/live.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ const main = (params: { workspaceId: string }) =>
7474
yield* push({
7575
workspace,
7676
snapshot,
77+
snapshotId: message.snapshotId,
7778
});
7879
}
7980
})

apps/client/src/workers/sync.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ const WorkerLive = WorkerRunner.layerSerialized(WorkerMessage, {
2626
});
2727

2828
if (tempUpdates !== undefined) {
29-
yield* push({ snapshot: tempUpdates.snapshot, workspace });
29+
yield* push({
30+
workspace,
31+
snapshot: tempUpdates.snapshot,
32+
snapshotId: tempUpdates.snapshotId,
33+
});
3034
yield* Effect.log("Sync completed");
3135
} else {
3236
yield* Effect.log("No sync updates");

apps/server/drizzle/0000_jazzy_shiver_man.sql renamed to apps/server/drizzle/0000_parallel_jigsaw.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ CREATE TABLE "workspace" (
2020
"workspaceId" uuid NOT NULL,
2121
"ownerClientId" uuid NOT NULL,
2222
"clientId" uuid NOT NULL,
23+
"snapshotId" uuid NOT NULL,
2324
"createdAt" timestamp DEFAULT now() NOT NULL,
24-
"snapshot" "bytea" NOT NULL
25+
"snapshot" "bytea" NOT NULL,
26+
CONSTRAINT "workspace_snapshotId_unique" UNIQUE("snapshotId")
2527
);

apps/server/drizzle/meta/0000_snapshot.json

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"id": "b2e29f84-dda2-4aeb-bb8b-e99481c2f2c1",
2+
"id": "710fed2b-4b98-4e23-b4d0-d6dc775c98fe",
33
"prevId": "00000000-0000-0000-0000-000000000000",
44
"version": "7",
55
"dialect": "postgresql",
@@ -133,6 +133,12 @@
133133
"primaryKey": false,
134134
"notNull": true
135135
},
136+
"snapshotId": {
137+
"name": "snapshotId",
138+
"type": "uuid",
139+
"primaryKey": false,
140+
"notNull": true
141+
},
136142
"createdAt": {
137143
"name": "createdAt",
138144
"type": "timestamp",
@@ -150,7 +156,15 @@
150156
"indexes": {},
151157
"foreignKeys": {},
152158
"compositePrimaryKeys": {},
153-
"uniqueConstraints": {},
159+
"uniqueConstraints": {
160+
"workspace_snapshotId_unique": {
161+
"name": "workspace_snapshotId_unique",
162+
"nullsNotDistinct": false,
163+
"columns": [
164+
"snapshotId"
165+
]
166+
}
167+
},
154168
"policies": {},
155169
"checkConstraints": {},
156170
"isRLSEnabled": false

apps/server/drizzle/meta/_journal.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
{
66
"idx": 0,
77
"version": "7",
8-
"when": 1740634258753,
9-
"tag": "0000_jazzy_shiver_man",
8+
"when": 1740722484842,
9+
"tag": "0000_parallel_jigsaw",
1010
"breakpoints": true
1111
}
1212
]

apps/server/src/db/schema.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export const workspaceTable = pgTable("workspace", {
1919
workspaceId: uuid().notNull(),
2020
ownerClientId: uuid().notNull(),
2121
clientId: uuid().notNull(),
22+
snapshotId: uuid().notNull().unique(),
2223
createdAt: timestamp().notNull().defaultNow(),
2324
snapshot: bytea().notNull(),
2425
});

apps/server/src/group/sync-auth.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export const SyncAuthGroupLive = HttpApiBuilder.group(
5151
clientId,
5252
workspaceId,
5353
ownerClientId: clientId,
54+
snapshotId: payload.snapshotId,
5455
}),
5556
})({
5657
clientId: payload.clientId,

0 commit comments

Comments
 (0)