Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions packages/opencode/src/project/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,6 @@ export namespace Project {
updated: Date.now(),
},
}
if (data.id !== ProjectID.global) {
await migrateFromGlobal(data.id, data.worktree)
}
return fresh
})

Expand Down Expand Up @@ -277,6 +274,11 @@ export namespace Project {
Database.use((db) =>
db.insert(ProjectTable).values(insert).onConflictDoUpdate({ target: ProjectTable.id, set: updateSet }).run(),
)

if (data.id !== ProjectID.global) {
await migrateFromGlobal(data.id, data.worktree)
}

GlobalBus.emit("event", {
payload: {
type: Event.Updated.type,
Expand Down
57 changes: 57 additions & 0 deletions packages/opencode/test/project/project.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import { tmpdir } from "../fixture/fixture"
import { Filesystem } from "../../src/util/filesystem"
import { GlobalBus } from "../../src/bus/global"
import { ProjectID } from "../../src/project/schema"
import { Session } from "../../src/session"
import { Instance } from "../../src/project/instance"
import { Database } from "../../src/storage/db"
import { SessionTable } from "../../src/session/session.sql"
import { eq } from "../../src/storage/db"

Log.init({ print: false })

Expand Down Expand Up @@ -347,3 +352,55 @@ describe("Project.update", () => {
expect(updated.commands?.start).toBe("make start")
})
})

describe("Project session migration", () => {
test("migrates sessions from global to git project after git init", async () => {
await using tmp = await tmpdir()

const globalProject = await Project.fromDirectory(tmp.path)
expect(globalProject.project.id).toBe(ProjectID.global)

const sessions = await Instance.provide({
directory: tmp.path,
fn: async () => {
const session1 = await Session.create({ title: "Session before git init" })
const session2 = await Session.create({ title: "Another pre-git session" })

const globalSessions = [...Session.list({ roots: true })]
expect(globalSessions.length).toBe(2)
expect(globalSessions.map((s) => s.id)).toContain(session1.id)
expect(globalSessions.map((s) => s.id)).toContain(session2.id)

return { session1, session2 }
},
})

await $`git init`.cwd(tmp.path).quiet()
await $`git config user.email "test@test.com"`.cwd(tmp.path).quiet()
await $`git config user.name "Test User"`.cwd(tmp.path).quiet()
await $`git commit --allow-empty -m "initial commit"`.cwd(tmp.path).quiet()

const gitProject = await Project.fromDirectory(tmp.path)
expect(gitProject.project.id).not.toBe(ProjectID.global)
expect(gitProject.project.vcs).toBe("git")

const migratedRows = Database.use((db) =>
db.select().from(SessionTable).where(eq(SessionTable.project_id, gitProject.project.id)).all(),
)
expect(migratedRows.length).toBe(2)

await Instance.reload({
directory: tmp.path,
project: gitProject.project,
worktree: gitProject.project.worktree,
})

await Instance.provide({
directory: tmp.path,
fn: async () => {
const gitSessions = [...Session.list({ roots: true })]
expect(gitSessions.length).toBe(2)
},
})
})
})
Loading