Skip to content

Commit 2bab5e8

Browse files
committed
fix: derive all IDs from file paths during json migration
Earlier migrations moved data to new directories without updating JSON fields. Now consistently derives all IDs from file paths: - Projects: id from filename - Sessions: id from filename, projectID from parent directory - Messages: id from filename, sessionID from parent directory - Parts: id from filename, messageID from parent directory This ensures migrated data matches the actual file layout regardless of stale values in JSON content.
1 parent 85b5f5b commit 2bab5e8

File tree

1 file changed

+16
-25
lines changed

1 file changed

+16
-25
lines changed

packages/opencode/src/storage/json-migration.ts

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ export namespace JsonMigration {
152152
sqlite.exec("BEGIN TRANSACTION")
153153

154154
// Migrate projects first (no FK deps)
155+
// Derive all IDs from file paths, not JSON content
155156
const projectIds = new Set<string>()
156157
const projectValues = [] as any[]
157158
for (let i = 0; i < projectFiles.length; i += batchSize) {
@@ -161,13 +162,10 @@ export namespace JsonMigration {
161162
for (let j = 0; j < batch.length; j++) {
162163
const data = batch[j]
163164
if (!data) continue
164-
if (!data?.id) {
165-
errs.push(`project missing id: ${projectFiles[i + j]}`)
166-
continue
167-
}
168-
projectIds.add(data.id)
165+
const id = path.basename(projectFiles[i + j], ".json")
166+
projectIds.add(id)
169167
projectValues.push({
170-
id: data.id,
168+
id,
171169
worktree: data.worktree ?? "/",
172170
vcs: data.vcs,
173171
name: data.name ?? undefined,
@@ -186,6 +184,9 @@ export namespace JsonMigration {
186184
log.info("migrated projects", { count: stats.projects, duration: Math.round(performance.now() - start) })
187185

188186
// Migrate sessions (depends on projects)
187+
// Derive all IDs from directory/file paths, not JSON content, since earlier
188+
// migrations may have moved sessions to new directories without updating the JSON
189+
const sessionProjects = sessionFiles.map((file) => path.basename(path.dirname(file)))
189190
const sessionIds = new Set<string>()
190191
const sessionValues = [] as any[]
191192
for (let i = 0; i < sessionFiles.length; i += batchSize) {
@@ -195,18 +196,16 @@ export namespace JsonMigration {
195196
for (let j = 0; j < batch.length; j++) {
196197
const data = batch[j]
197198
if (!data) continue
198-
if (!data?.id || !data?.projectID) {
199-
errs.push(`session missing id or projectID: ${sessionFiles[i + j]}`)
200-
continue
201-
}
202-
if (!projectIds.has(data.projectID)) {
199+
const id = path.basename(sessionFiles[i + j], ".json")
200+
const projectID = sessionProjects[i + j]
201+
if (!projectIds.has(projectID)) {
203202
orphans.sessions++
204203
continue
205204
}
206-
sessionIds.add(data.id)
205+
sessionIds.add(id)
207206
sessionValues.push({
208-
id: data.id,
209-
project_id: data.projectID,
207+
id,
208+
project_id: projectID,
210209
parent_id: data.parentID ?? null,
211210
slug: data.slug ?? "",
212211
directory: data.directory ?? "",
@@ -253,11 +252,7 @@ export namespace JsonMigration {
253252
const data = batch[j]
254253
if (!data) continue
255254
const file = allMessageFiles[i + j]
256-
const id = data.id ?? path.basename(file, ".json")
257-
if (!id) {
258-
errs.push(`message missing id: ${file}`)
259-
continue
260-
}
255+
const id = path.basename(file, ".json")
261256
const sessionID = allMessageSessions[i + j]
262257
messageSessions.set(id, sessionID)
263258
const rest = data
@@ -287,12 +282,8 @@ export namespace JsonMigration {
287282
const data = batch[j]
288283
if (!data) continue
289284
const file = partFiles[i + j]
290-
const id = data.id ?? path.basename(file, ".json")
291-
const messageID = data.messageID ?? path.basename(path.dirname(file))
292-
if (!id || !messageID) {
293-
errs.push(`part missing id/messageID/sessionID: ${file}`)
294-
continue
295-
}
285+
const id = path.basename(file, ".json")
286+
const messageID = path.basename(path.dirname(file))
296287
const sessionID = messageSessions.get(messageID)
297288
if (!sessionID) {
298289
errs.push(`part missing message session: ${file}`)

0 commit comments

Comments
 (0)