Skip to content

Commit f2898ff

Browse files
committed
fix(core): merge native history through the V1 importer
1 parent 5fa6324 commit f2898ff

3 files changed

Lines changed: 96 additions & 33 deletions

File tree

‎packages/core/src/database/migration/20260804233008_loose_psylocke.ts‎

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -136,26 +136,15 @@ const migration: DatabaseMigration.Migration = {
136136
CONSTRAINT \`fk_session_message_session_id_session_v2_id_fk\` FOREIGN KEY (\`session_id\`) REFERENCES \`session_v2\`(\`id\`) ON DELETE CASCADE
137137
);
138138
`)
139-
// 1.19's /api/session already writes V2 messages in this database.
140-
// Preserve their sessions before moving the projection's foreign key.
139+
// Preserve 1.19 API history until the data importer can resolve its owners.
140+
// This durable staging table survives interruption between schema and data migration.
141141
yield* tx.run(`
142-
INSERT OR IGNORE INTO \`session_v2\` (
143-
id, project_id, workspace_id, parent_id, slug, directory, path, title, version, share_url,
144-
summary_additions, summary_deletions, summary_files, summary_diffs, metadata, cost,
145-
tokens_input, tokens_output, tokens_reasoning, tokens_cache_read, tokens_cache_write,
146-
revert, permission, agent, model, time_created, time_updated, time_compacting, time_archived
147-
)
148-
SELECT
149-
id, project_id, workspace_id, parent_id, slug, directory, path, title, version, share_url,
150-
summary_additions, summary_deletions, summary_files, summary_diffs, metadata, cost,
151-
tokens_input, tokens_output, tokens_reasoning, tokens_cache_read, tokens_cache_write,
152-
revert, NULL, agent, model, time_created, time_updated, time_compacting, time_archived
153-
FROM \`session\`
154-
WHERE EXISTS (SELECT 1 FROM \`session_message\` WHERE session_id = session.id);
142+
CREATE TABLE \`migration_v1_session_message\` AS
143+
SELECT id, session_id, type, seq, time_created, time_updated, data FROM \`session_message\`;
155144
`)
156145
yield* tx.run(`
157-
INSERT INTO \`__new_session_message\` (id, session_id, type, seq, time_created, time_updated, data)
158-
SELECT id, session_id, type, seq, time_created, time_updated, data FROM \`session_message\`;
146+
CREATE INDEX \`migration_v1_session_message_session_idx\`
147+
ON \`migration_v1_session_message\` (session_id, seq);
159148
`)
160149
yield* tx.run(`DROP TABLE \`session_message\`;`)
161150
yield* tx.run(`ALTER TABLE \`__new_session_message\` RENAME TO \`session_message\`;`)

‎packages/core/src/database/v1-migration.bun.ts‎

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,9 @@ export function run(
573573
}),
574574
)
575575
.pipe(Effect.orDie)
576+
const staged = yield* db.get(sql`
577+
SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'migration_v1_session_message'
578+
`)
576579
const sourceTotal = yield* countNextSessions(nextPath(options, global.data))
577580
const legacyTotal = (yield* db.get<{ value: number }>(sql`SELECT COUNT(*) AS value FROM session`))?.value ?? 0
578581
const cursor = state?.phase === "sessions" ? state.cursor : undefined
@@ -647,7 +650,7 @@ export function run(
647650
set: { value: { phase: "sessions", cursor: nextID.id }, time_updated: Date.now() },
648651
})
649652
.run()
650-
// Native V2 history (including 1.19 API sessions) is already authoritative.
653+
// Sessions imported from opencode-next.db remain authoritative.
651654
if (nextID.imported) return
652655
if (projectID !== nextID.project_id)
653656
yield* Effect.logWarning("Reassigned V1 session with missing project", {
@@ -700,35 +703,51 @@ export function run(
700703
sql`SELECT id, message_id, session_id, time_created, time_updated, data FROM part WHERE session_id = ${next.id}`,
701704
)
702705
const transformed = transformSession({ session: next, messages: sourceMessages, parts: sourceParts })
706+
const native = staged
707+
? yield* tx.all<NextMessage>(sql`
708+
SELECT id, session_id, type, seq, time_created, time_updated, data
709+
FROM migration_v1_session_message WHERE session_id = ${next.id} ORDER BY seq
710+
`)
711+
: []
712+
const nativeIDs = new Set(native.map((message) => message.id))
713+
const legacy = transformed.messages.filter((message) => !nativeIDs.has(message.id))
714+
const messages = [
715+
...legacy.map((message, seq) => ({ ...message, seq, data: JSON.stringify(message.data) })),
716+
...native.map((message) => ({ ...message, seq: message.seq + legacy.length })),
717+
]
718+
const watermark = messages.at(-1)?.seq ?? -1
703719
yield* Effect.forEach(transformed.warnings, (warning) =>
704720
Effect.logWarning("Skipped V1 migration row", warning),
705721
)
706722
yield* tx.delete(SessionMessageTable).where(eq(SessionMessageTable.session_id, next.id)).run()
707-
yield* Effect.forEach(transformed.messages, (message) =>
723+
yield* Effect.forEach(messages, (message) =>
708724
tx
709725
.insert(SessionMessageTable)
710726
.values({
711727
id: SessionMessage.ID.make(message.id),
712728
session_id: SessionSchema.ID.make(message.session_id),
713-
type: message.type,
729+
type: message.type as SessionMessage.Type,
714730
seq: message.seq,
715731
time_created: message.time_created,
716732
time_updated: message.time_updated,
717-
data: sql`${JSON.stringify(message.data)}`,
733+
data: sql`${message.data}`,
718734
})
719735
.run(),
720736
)
721737
yield* tx
722738
.update(SessionTable)
723-
.set({ ...transformed.session, time_updated: next.time_updated })
739+
.set({ ...(native.length ? {} : transformed.session), time_updated: next.time_updated })
724740
.where(eq(SessionTable.id, next.id))
725741
.run()
726742
yield* tx
727743
.insert(EventSequenceTable)
728-
.values({ aggregate_id: next.id, seq: transformed.watermark })
744+
.values({ aggregate_id: next.id, seq: watermark })
729745
.onConflictDoUpdate({
730746
target: EventSequenceTable.aggregate_id,
731-
set: { seq: transformed.watermark, owner_id: null },
747+
set: {
748+
seq: native.length ? sql`max(${EventSequenceTable.seq}, ${watermark})` : watermark,
749+
owner_id: null,
750+
},
732751
})
733752
.run()
734753
}),
@@ -749,6 +768,7 @@ export function run(
749768
yield* db
750769
.transaction((tx) =>
751770
Effect.gen(function* () {
771+
if (staged) yield* tx.run(sql`DROP TABLE migration_v1_session_message`)
752772
yield* tx
753773
.insert(KVTable)
754774
.values({ key: MIGRATION_STATE_KEY, value: { phase: "completed" } })

‎packages/core/test/v1-migration.test.ts‎

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,8 @@ describe("V1Migration database workflow", () => {
903903
)
904904

905905
test("preserves 1.19 API messages while migrating legacy sessions in the same database", async () => {
906+
await using tmp = await tmpdir()
907+
await initRepo(tmp.path)
906908
await run(
907909
Effect.gen(function* () {
908910
const db = yield* makeDb
@@ -914,12 +916,12 @@ describe("V1Migration database workflow", () => {
914916
)
915917
yield* db.run(sql`
916918
INSERT INTO project (id, worktree, time_created, time_updated, sandboxes)
917-
VALUES ('project', '/tmp/test', 1, 2, '[]')
919+
VALUES ('project', ${tmp.path}, 1, 2, '[]'), ('global', '/', 1, 2, '[]')
918920
`)
919921
yield* db.run(sql`
920922
INSERT INTO session (id, project_id, slug, directory, title, version, cost, tokens_input,
921923
agent, model, metadata, time_created, time_updated)
922-
VALUES ('ses_native', 'project', 'native', '/tmp/test', 'API history', '1.19.3', 2.25, 42,
924+
VALUES ('ses_native', 'global', 'native', ${tmp.path}, 'API history', '1.19.3', 2.25, 42,
923925
'build', '{"id":"model","providerID":"provider"}', '{"keep":true}', 1, 2),
924926
('ses_test', 'project', 'legacy', '/tmp/test', 'Legacy history', '1.19.3', 0, 0,
925927
NULL, NULL, NULL, 1, 2)
@@ -941,19 +943,50 @@ describe("V1Migration database workflow", () => {
941943
VALUES ('prt_1', ${source.id}, 'ses_test', 1, 2, ${sourcePart.data})
942944
`)
943945

946+
// A legacy session can later be continued through the 1.19 API.
947+
yield* db.run(sql`
948+
INSERT INTO session_message (id, session_id, type, seq, time_created, time_updated, data)
949+
VALUES ('msg_mixed', 'ses_test', 'user', 0, 20, 21, ${data})
950+
`)
951+
yield* db.run(sql`
952+
INSERT INTO session (id, project_id, slug, directory, title, version, time_created, time_updated)
953+
VALUES ('ses_relative', 'project', 'relative', '.', 'Relative', '1.19.3', 1, 2)
954+
`)
955+
yield* db.run(sql`
956+
INSERT INTO session_message (id, session_id, type, seq, time_created, time_updated, data)
957+
VALUES ('msg_relative', 'ses_relative', 'user', 0, 3, 4, ${data})
958+
`)
959+
yield* db.run(sql`
960+
INSERT INTO message (id, session_id, time_created, time_updated, data)
961+
VALUES ('msg_relative', 'ses_relative', 10, 11, ${source.data})
962+
`)
944963
yield* DatabaseMigration.apply(db)
945-
expect(yield* db.get(sql`SELECT id, seq, data FROM session_message WHERE session_id = 'ses_native'`)).toEqual({
946-
id: "msg_native",
947-
seq: 7,
948-
data,
949-
})
950-
yield* V1Migration.run().pipe(
964+
yield* db.run(sql`
965+
CREATE TRIGGER fail_native BEFORE INSERT ON session_message
966+
WHEN NEW.id = 'msg_native' BEGIN SELECT RAISE(ABORT, 'stop'); END
967+
`)
968+
const migrate = V1Migration.run().pipe(
951969
Effect.provide(
952970
AppNodeBuilder.build(LayerNode.group([Project.node, Database.node, FSUtil.node]), [
953971
Database.node.replace(Layer.succeed(Database.Service, { db })),
954972
]),
955973
),
956974
)
975+
expect((yield* Effect.exit(migrate))._tag).toBe("Failure")
976+
expect(yield* db.get(sql`SELECT id FROM session_v2 WHERE id = 'ses_native'`)).toBeUndefined()
977+
expect(yield* db.get(sql`SELECT value FROM kv WHERE key = 'migration.v1-v2'`)).toEqual({
978+
value: '{"phase":"sessions","cursor":"ses_relative"}',
979+
})
980+
expect(
981+
yield* db.get(sql`SELECT id, seq, data FROM migration_v1_session_message WHERE session_id = 'ses_native'`),
982+
).toEqual({
983+
id: "msg_native",
984+
seq: 7,
985+
data,
986+
})
987+
yield* db.run(sql`DROP TRIGGER fail_native`)
988+
expect(yield* migrate).toEqual({ status: "completed" })
989+
expect(yield* migrate).toEqual({ status: "completed" })
957990

958991
expect(yield* db.all(sql`SELECT id, seq, data FROM session_message WHERE session_id = 'ses_native'`)).toEqual([
959992
{ id: "msg_native", seq: 7, data },
@@ -970,10 +1003,31 @@ describe("V1Migration database workflow", () => {
9701003
model: '{"id":"model","providerID":"provider"}',
9711004
metadata: '{"keep":true}',
9721005
})
1006+
const owner = yield* db.get<{ id: string }>(
1007+
sql`SELECT id FROM project WHERE worktree = ${tmp.path} AND id <> 'project'`,
1008+
)
1009+
expect(owner).toBeDefined()
1010+
expect(yield* db.get(sql`SELECT project_id, path FROM session_v2 WHERE id = 'ses_native'`)).toEqual({
1011+
project_id: owner?.id,
1012+
path: "",
1013+
})
1014+
expect(yield* db.get(sql`SELECT directory FROM session_v2 WHERE id = 'ses_relative'`)).toEqual({
1015+
directory: tmp.path,
1016+
})
1017+
expect(yield* db.get(sql`SELECT id, data FROM session_message WHERE session_id = 'ses_relative'`)).toEqual({
1018+
id: "msg_relative",
1019+
data,
1020+
})
9731021
expect(yield* db.get(sql`SELECT seq FROM event_sequence WHERE aggregate_id = 'ses_native'`)).toEqual({ seq: 9 })
974-
expect(yield* db.all(sql`SELECT id, data FROM session_message WHERE session_id = 'ses_test'`)).toEqual([
1022+
expect(
1023+
yield* db.all(sql`SELECT id, data FROM session_message WHERE session_id = 'ses_test' ORDER BY seq`),
1024+
).toEqual([
9751025
{ id: source.id, data: '{"text":"legacy text","time":{"created":10}}' },
1026+
{ id: "msg_mixed", data },
9761027
])
1028+
expect(
1029+
yield* db.get(sql`SELECT name FROM sqlite_master WHERE name = 'migration_v1_session_message'`),
1030+
).toBeUndefined()
9771031
expect(yield* db.get(sql`SELECT value FROM kv WHERE key = 'migration.v1-v2'`)).toEqual({
9781032
value: '{"phase":"completed"}',
9791033
})

0 commit comments

Comments
 (0)