Skip to content

Commit 957edf0

Browse files
committed
Add backward compatibility helpers for context versioning
Introduce functions to handle legacy context versioning and previous versions. Update mutation and query methods to utilize these helpers, ensuring smooth version tracking and compatibility with existing data structures.
1 parent a3f3d76 commit 957edf0

File tree

2 files changed

+54
-17
lines changed

2 files changed

+54
-17
lines changed

convex-dev/contexts.ts

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,24 @@
88
import { v } from "convex/values";
99
import { mutation, query } from "./_generated/server";
1010

11+
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
12+
// Backward Compatibility Helpers
13+
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
14+
15+
/**
16+
* Get version number with backward compatibility for legacy contexts
17+
*/
18+
function getContextVersion(context: any): number {
19+
return context.version ?? 1;
20+
}
21+
22+
/**
23+
* Get previous versions array with backward compatibility for legacy contexts
24+
*/
25+
function getContextPreviousVersions(context: any): any[] {
26+
return context.previousVersions ?? [];
27+
}
28+
1129
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
1230
// Mutations (Write Operations)
1331
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
@@ -135,9 +153,12 @@ export const update = mutation({
135153
...args.data,
136154
};
137155

138-
// Create version snapshot
156+
// Create version snapshot (with backward compatibility)
157+
const currentVersion = getContextVersion(context);
158+
const previousVersions = getContextPreviousVersions(context);
159+
139160
const newVersion = {
140-
version: context.version,
161+
version: currentVersion,
141162
status: context.status,
142163
data: context.data,
143164
timestamp: context.updatedAt,
@@ -149,8 +170,8 @@ export const update = mutation({
149170
await ctx.db.patch(context._id, {
150171
status: newStatus,
151172
data: newData,
152-
version: context.version + 1,
153-
previousVersions: [...context.previousVersions, newVersion],
173+
version: currentVersion + 1,
174+
previousVersions: [...previousVersions, newVersion],
154175
updatedAt: now,
155176
completedAt:
156177
args.completedAt !== undefined
@@ -766,8 +787,12 @@ export const updateMany = mutation({
766787

767788
// Update each context
768789
for (const context of contexts) {
790+
// Backward compatibility for version tracking
791+
const currentVersion = getContextVersion(context);
792+
const previousVersions = getContextPreviousVersions(context);
793+
769794
const newVersion = {
770-
version: context.version,
795+
version: currentVersion,
771796
status: context.status,
772797
data: context.data,
773798
timestamp: context.updatedAt,
@@ -784,8 +809,8 @@ export const updateMany = mutation({
784809
? args.updates.status
785810
: context.status,
786811
data: newData,
787-
version: context.version + 1,
788-
previousVersions: [...context.previousVersions, newVersion],
812+
version: currentVersion + 1,
813+
previousVersions: [...previousVersions, newVersion],
789814
updatedAt: now,
790815
});
791816

@@ -959,10 +984,14 @@ export const getVersion = query({
959984
return null;
960985
}
961986

987+
// Backward compatibility for version tracking
988+
const currentVersion = getContextVersion(context);
989+
const previousVersions = getContextPreviousVersions(context);
990+
962991
// Check if it's the current version
963-
if (context.version === args.version) {
992+
if (currentVersion === args.version) {
964993
return {
965-
version: context.version,
994+
version: currentVersion,
966995
status: context.status,
967996
data: context.data,
968997
timestamp: context.updatedAt,
@@ -971,7 +1000,7 @@ export const getVersion = query({
9711000
}
9721001

9731002
// Check previous versions
974-
const versionRecord = context.previousVersions.find(
1003+
const versionRecord = previousVersions.find(
9751004
(v: any) => v.version === args.version,
9761005
);
9771006

@@ -996,11 +1025,15 @@ export const getHistory = query({
9961025
return [];
9971026
}
9981027

1028+
// Backward compatibility for version tracking
1029+
const currentVersion = getContextVersion(context);
1030+
const previousVersions = getContextPreviousVersions(context);
1031+
9991032
// Return all previous versions + current version
10001033
const versions = [
1001-
...context.previousVersions,
1034+
...previousVersions,
10021035
{
1003-
version: context.version,
1036+
version: currentVersion,
10041037
status: context.status,
10051038
data: context.data,
10061039
timestamp: context.updatedAt,
@@ -1030,10 +1063,14 @@ export const getAtTimestamp = query({
10301063
return null;
10311064
}
10321065

1066+
// Backward compatibility for version tracking
1067+
const currentVersion = getContextVersion(context);
1068+
const previousVersions = getContextPreviousVersions(context);
1069+
10331070
// If timestamp is after current version, return current
10341071
if (args.timestamp >= context.updatedAt) {
10351072
return {
1036-
version: context.version,
1073+
version: currentVersion,
10371074
status: context.status,
10381075
data: context.data,
10391076
timestamp: context.updatedAt,
@@ -1044,9 +1081,9 @@ export const getAtTimestamp = query({
10441081
// Find the version that was current at the timestamp
10451082
// Walk backwards through versions
10461083
const allVersions = [
1047-
...context.previousVersions,
1084+
...previousVersions,
10481085
{
1049-
version: context.version,
1086+
version: currentVersion,
10501087
status: context.status,
10511088
data: context.data,
10521089
timestamp: context.updatedAt,

tests/streaming/rememberStream.integration.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ describe("rememberStream Integration Tests", () => {
3939
afterAll(async () => {
4040
// Cleanup
4141
if (client) {
42-
client.close();
42+
await client.close();
4343
}
4444
});
4545

@@ -123,7 +123,7 @@ describe("rememberStream Integration Tests", () => {
123123
onChunk: (event) => {
124124
chunks.push(event.chunk);
125125
},
126-
onProgress: (event) => {
126+
onProgress: (_event) => {
127127
progressCallbacks++;
128128
},
129129
onComplete: (_event) => {

0 commit comments

Comments
 (0)