database-migration.test.ts 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. import { describe, expect, test } from "bun:test"
  2. import { $ } from "bun"
  3. import { fileURLToPath } from "url"
  4. import path from "path"
  5. import { SqliteClient } from "@effect/sql-sqlite-bun"
  6. import { EffectDrizzleSqlite } from "@opencode-ai/effect-drizzle-sqlite"
  7. import { Effect, Layer } from "effect"
  8. import { eq, inArray, sql } from "drizzle-orm"
  9. import { DatabaseMigration } from "@opencode-ai/core/database/migration"
  10. import { migrations } from "@opencode-ai/core/database/migration.gen"
  11. import sessionUsageMigration from "@opencode-ai/core/database/migration/20260510033149_session_usage"
  12. import normalizeStoragePathsMigration from "@opencode-ai/core/database/migration/20260601010001_normalize_storage_paths"
  13. import sessionMessageProjectionOrderMigration from "@opencode-ai/core/database/migration/20260603040000_session_message_projection_order"
  14. import eventSourcedSessionInputMigration from "@opencode-ai/core/database/migration/20260604172448_event_sourced_session_input"
  15. import contextEpochAgentMigration from "@opencode-ai/core/database/migration/20260605042240_add_context_epoch_agent"
  16. import simplifyIntegrationCredentialsMigration from "@opencode-ai/core/database/migration/20260611192811_lush_chimera"
  17. import simplifySessionInputMigration from "@opencode-ai/core/database/migration/20260622202450_simplify_session_input"
  18. import { EventV2 } from "@opencode-ai/core/event"
  19. import { ProjectV2 } from "@opencode-ai/core/project"
  20. import { ProjectTable } from "@opencode-ai/core/project/sql"
  21. import { AbsolutePath } from "@opencode-ai/core/schema"
  22. import { SessionSchema } from "@opencode-ai/core/session/schema"
  23. import { SessionTable } from "@opencode-ai/core/session/sql"
  24. import sessionMetadataMigration from "@opencode-ai/core/database/migration/20260511173437_session-metadata"
  25. import type { SqlClient as SqlClientService } from "effect/unstable/sql/SqlClient"
  26. import { Database } from "@opencode-ai/core/database/database"
  27. import { SessionProjector } from "@opencode-ai/core/session/projector"
  28. import { SessionV1 } from "@opencode-ai/core/v1/session"
  29. import { tmpdir } from "./fixture/tmpdir"
  30. const run = <A, E>(effect: Effect.Effect<A, E, SqlClientService>) =>
  31. Effect.runPromise(
  32. effect.pipe(Effect.provide(SqliteClient.layer({ filename: ":memory:", disableWAL: true })), Effect.scoped),
  33. )
  34. const makeDb = EffectDrizzleSqlite.makeWithDefaults()
  35. describe("DatabaseMigration", () => {
  36. test("serializes concurrent embedded initialization for one database path", async () => {
  37. await using tmp = await tmpdir()
  38. const filename = path.join(tmp.path, "embedded.sqlite")
  39. const layers = [Database.layerFromPath(filename), Database.layerFromPath(filename)]
  40. await Effect.runPromise(
  41. Effect.all(
  42. layers.map((layer) => Effect.scoped(Layer.build(layer))),
  43. { concurrency: "unbounded" },
  44. ),
  45. )
  46. })
  47. if (process.platform === "linux") {
  48. test("declared schema has no ungenerated migrations", async () => {
  49. const result = await $`bun ${fileURLToPath(new URL("../script/migration.ts", import.meta.url))} --check`
  50. .quiet()
  51. .nothrow()
  52. expect(result.exitCode, result.stderr.toString()).toBe(0)
  53. expect(result.stdout.toString()).toContain("No schema changes, nothing to migrate")
  54. }, 30_000)
  55. }
  56. test("applies tracked migrations to an empty database", async () => {
  57. await run(
  58. Effect.gen(function* () {
  59. const db = yield* makeDb
  60. yield* DatabaseMigration.apply(db)
  61. expect(yield* db.get(sql`SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'session'`)).toEqual({
  62. name: "session",
  63. })
  64. expect(
  65. yield* db.get(sql`SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'session_input'`),
  66. ).toEqual({ name: "session_input" })
  67. expect(
  68. yield* db.get(sql`SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'session_context_epoch'`),
  69. ).toEqual({ name: "session_context_epoch" })
  70. expect(
  71. yield* db.get(
  72. sql`SELECT name FROM pragma_table_info('session_context_epoch') WHERE name IN ('agent', 'replacement_seq', 'revision')`,
  73. ),
  74. ).toBeUndefined()
  75. expect(yield* db.get(sql`SELECT count(*) as count FROM migration`)).toEqual({ count: migrations.length })
  76. expect(
  77. yield* db.all(
  78. sql`SELECT name FROM sqlite_master WHERE type = 'index' AND name IN ('event_aggregate_seq_idx', 'event_aggregate_type_seq_idx', 'session_input_session_pending_seq_idx', 'session_input_session_pending_delivery_seq_idx', 'session_input_session_admitted_seq_idx', 'session_input_session_promoted_seq_idx', 'session_message_session_idx', 'session_message_session_type_idx', 'session_message_session_seq_idx', 'session_message_session_type_seq_idx', 'session_message_session_time_created_id_idx') ORDER BY name`,
  79. ),
  80. ).toEqual([
  81. { name: "event_aggregate_seq_idx" },
  82. { name: "event_aggregate_type_seq_idx" },
  83. { name: "session_input_session_admitted_seq_idx" },
  84. { name: "session_input_session_pending_delivery_seq_idx" },
  85. { name: "session_input_session_promoted_seq_idx" },
  86. { name: "session_message_session_seq_idx" },
  87. { name: "session_message_session_time_created_id_idx" },
  88. { name: "session_message_session_type_seq_idx" },
  89. ])
  90. }),
  91. )
  92. })
  93. test("rejects a non-empty database without a session table", async () => {
  94. await expect(
  95. run(
  96. Effect.gen(function* () {
  97. const db = yield* makeDb
  98. yield* db.run(sql`CREATE TABLE unrelated (id text PRIMARY KEY)`)
  99. yield* DatabaseMigration.apply(db)
  100. }),
  101. ),
  102. ).rejects.toThrow("Database is not empty and has no session table")
  103. })
  104. test("backfills existing Context Epoch rows to the build agent", async () => {
  105. await run(
  106. Effect.gen(function* () {
  107. const db = yield* makeDb
  108. yield* db.run(
  109. sql`CREATE TABLE session_context_epoch (session_id text PRIMARY KEY, baseline text NOT NULL, snapshot text NOT NULL, baseline_seq integer NOT NULL, replacement_seq integer, revision integer DEFAULT 0 NOT NULL)`,
  110. )
  111. yield* db.run(
  112. sql`INSERT INTO session_context_epoch (session_id, baseline, snapshot, baseline_seq) VALUES ('ses_existing', 'baseline', '{}', 0)`,
  113. )
  114. yield* DatabaseMigration.applyOnly(db, [contextEpochAgentMigration])
  115. expect(yield* db.get(sql`SELECT agent FROM session_context_epoch WHERE session_id = 'ses_existing'`)).toEqual({
  116. agent: "build",
  117. })
  118. }),
  119. )
  120. })
  121. test("keeps legacy credential fields nullable", async () => {
  122. await run(
  123. Effect.gen(function* () {
  124. const db = yield* makeDb
  125. yield* db.run(
  126. sql`CREATE TABLE credential (id text PRIMARY KEY, connector_id text NOT NULL, method_id text NOT NULL, label text NOT NULL, value text NOT NULL, active integer DEFAULT false NOT NULL, time_created integer NOT NULL, time_updated integer NOT NULL)`,
  127. )
  128. yield* db.run(
  129. sql`CREATE UNIQUE INDEX credential_connector_active_idx ON credential (connector_id) WHERE active = 1`,
  130. )
  131. yield* DatabaseMigration.applyOnly(db, [simplifyIntegrationCredentialsMigration])
  132. yield* db.run(
  133. sql`INSERT INTO credential (id, connector_id, method_id, label, value, active, time_created, time_updated) VALUES ('legacy', 'openai', 'oauth', 'Legacy', '{}', 1, 1, 1)`,
  134. )
  135. yield* db.run(
  136. sql`INSERT INTO credential (id, integration_id, label, value, time_created, time_updated) VALUES ('current', 'anthropic', 'Current', '{}', 2, 2)`,
  137. )
  138. expect(yield* db.get(sql`SELECT connector_id, method_id, active FROM credential WHERE id = 'current'`)).toEqual(
  139. { connector_id: null, method_id: null, active: null },
  140. )
  141. }),
  142. )
  143. })
  144. test("resets beta history and rebuilds event-sourced Session input storage", async () => {
  145. await run(
  146. Effect.gen(function* () {
  147. const db = yield* makeDb
  148. yield* db.run(sql`CREATE TABLE session (id text PRIMARY KEY, workspace_id text)`)
  149. yield* db.run(sql`CREATE TABLE workspace (id text PRIMARY KEY)`)
  150. yield* db.run(sql`CREATE TABLE message (id text PRIMARY KEY)`)
  151. yield* db.run(sql`CREATE TABLE part (id text PRIMARY KEY)`)
  152. yield* db.run(sql`CREATE TABLE event_sequence (aggregate_id text PRIMARY KEY, seq integer NOT NULL)`)
  153. yield* db.run(
  154. sql`CREATE TABLE event (id text PRIMARY KEY, aggregate_id text NOT NULL, seq integer NOT NULL, type text NOT NULL, data text NOT NULL)`,
  155. )
  156. yield* db.run(sql`CREATE INDEX event_aggregate_seq_idx ON event (aggregate_id, seq)`)
  157. yield* db.run(sql`CREATE INDEX event_aggregate_type_seq_idx ON event (aggregate_id, type, seq)`)
  158. yield* db.run(
  159. sql`CREATE TABLE session_message (id text PRIMARY KEY, session_id text NOT NULL, type text NOT NULL, seq integer NOT NULL, time_created integer NOT NULL, time_updated integer NOT NULL, data text NOT NULL)`,
  160. )
  161. yield* db.run(sql`CREATE INDEX session_message_session_seq_idx ON session_message (session_id, seq)`)
  162. yield* db.run(
  163. sql`CREATE TABLE session_input (seq integer PRIMARY KEY AUTOINCREMENT, id text NOT NULL UNIQUE, session_id text NOT NULL, prompt text NOT NULL, delivery text NOT NULL, promoted_seq integer, time_created integer NOT NULL)`,
  164. )
  165. yield* db.run(
  166. sql`CREATE INDEX session_input_session_pending_delivery_seq_idx ON session_input (session_id, promoted_seq, delivery, seq)`,
  167. )
  168. yield* db.run(sql`INSERT INTO session (id, workspace_id) VALUES ('session', 'wrk_old')`)
  169. yield* db.run(sql`INSERT INTO workspace (id) VALUES ('wrk_old')`)
  170. yield* db.run(sql`INSERT INTO message (id) VALUES ('message')`)
  171. yield* db.run(sql`INSERT INTO part (id) VALUES ('part')`)
  172. yield* db.run(sql`INSERT INTO event_sequence (aggregate_id, seq) VALUES ('session', 0)`)
  173. yield* db.run(
  174. sql`INSERT INTO event (id, aggregate_id, seq, type, data) VALUES ('evt_old', 'session', 0, 'old.1', '{}')`,
  175. )
  176. yield* db.run(
  177. sql`INSERT INTO session_message (id, session_id, type, seq, time_created, time_updated, data) VALUES ('msg_old', 'session', 'user', 0, 1, 1, '{}')`,
  178. )
  179. yield* db.run(
  180. sql`INSERT INTO session_input (id, session_id, prompt, delivery, time_created) VALUES ('msg_pending', 'session', '{}', 'steer', 1)`,
  181. )
  182. yield* DatabaseMigration.applyOnly(db, [eventSourcedSessionInputMigration])
  183. expect(yield* db.all(sql`SELECT id, workspace_id FROM session`)).toEqual([
  184. { id: "session", workspace_id: null },
  185. ])
  186. expect(yield* db.all(sql`SELECT id FROM workspace`)).toEqual([])
  187. expect(yield* db.all(sql`SELECT id FROM message`)).toEqual([{ id: "message" }])
  188. expect(yield* db.all(sql`SELECT id FROM part`)).toEqual([{ id: "part" }])
  189. expect(yield* db.all(sql`SELECT id FROM event`)).toEqual([])
  190. expect(yield* db.all(sql`SELECT aggregate_id FROM event_sequence`)).toEqual([])
  191. expect(yield* db.all(sql`SELECT id FROM session_message`)).toEqual([])
  192. expect(yield* db.all(sql`SELECT id FROM session_input`)).toEqual([])
  193. expect(
  194. (yield* db.all<{ name: string }>(sql`PRAGMA table_info(session_input)`)).map((column) => column.name),
  195. ).toEqual(["id", "session_id", "prompt", "delivery", "admitted_seq", "promoted_seq", "time_created"])
  196. expect(
  197. (yield* db.all<{ name: string; unique: number }>(sql`PRAGMA index_list(session_message)`)).find(
  198. (index) => index.name === "session_message_session_seq_idx",
  199. ),
  200. ).toMatchObject({ unique: 1 })
  201. expect(
  202. (yield* db.all<{ name: string; unique: number }>(sql`PRAGMA index_list(event)`)).find(
  203. (index) => index.name === "event_aggregate_seq_idx",
  204. ),
  205. ).toMatchObject({ unique: 1 })
  206. expect(
  207. (yield* db.all<{ name: string; unique: number }>(sql`PRAGMA index_list(session_input)`)).filter((index) =>
  208. ["session_input_session_admitted_seq_idx", "session_input_session_promoted_seq_idx"].includes(index.name),
  209. ),
  210. ).toEqual([
  211. expect.objectContaining({ name: "session_input_session_promoted_seq_idx", unique: 1 }),
  212. expect.objectContaining({ name: "session_input_session_admitted_seq_idx", unique: 1 }),
  213. ])
  214. }),
  215. )
  216. })
  217. test("preserves canonical V1 state and restarts its event stream", async () => {
  218. await run(
  219. Effect.gen(function* () {
  220. const db = yield* makeDb
  221. yield* db.run(sql`PRAGMA foreign_keys = ON`)
  222. yield* DatabaseMigration.apply(db)
  223. yield* db.run(
  224. sql`INSERT INTO project (id, worktree, time_created, time_updated, sandboxes) VALUES ('global', '/project', 1, 1, '[]')`,
  225. )
  226. yield* db.run(
  227. sql`INSERT INTO workspace (id, type, project_id, time_used) VALUES ('workspace', 'local', 'global', 1)`,
  228. )
  229. yield* db.run(
  230. sql`INSERT INTO session (id, project_id, workspace_id, slug, directory, title, version, time_created, time_updated) VALUES ('session', 'global', 'workspace', 'session', '/project', 'Before', 'test', 1, 1)`,
  231. )
  232. yield* db.run(
  233. sql`INSERT INTO message (id, session_id, time_created, time_updated, data) VALUES ('message', 'session', 1, 1, '{}')`,
  234. )
  235. yield* db.run(
  236. sql`INSERT INTO part (id, message_id, session_id, time_created, time_updated, data) VALUES ('part', 'message', 'session', 1, 1, '{}')`,
  237. )
  238. yield* db.run(sql`INSERT INTO event_sequence (aggregate_id, seq) VALUES ('session', 9)`)
  239. yield* db.run(
  240. sql`INSERT INTO event (id, aggregate_id, seq, type, data) VALUES ('event', 'session', 9, 'session.updated.1', '{}')`,
  241. )
  242. yield* db.run(
  243. sql`INSERT INTO session_input (id, session_id, prompt, delivery, admitted_seq, time_created) VALUES ('input', 'session', '{}', 'steer', 9, 1)`,
  244. )
  245. yield* db.run(
  246. sql`INSERT INTO session_message (id, session_id, type, seq, time_created, time_updated, data) VALUES ('projected', 'session', 'user', 9, 1, 1, '{}')`,
  247. )
  248. yield* db.run(
  249. sql`INSERT INTO session_context_epoch (session_id, baseline, snapshot, baseline_seq) VALUES ('session', 'baseline', '{}', 9)`,
  250. )
  251. yield* db.run(sql`DELETE FROM migration WHERE id = ${simplifySessionInputMigration.id}`)
  252. yield* DatabaseMigration.applyOnly(db, [simplifySessionInputMigration])
  253. const database = Layer.succeed(Database.Service, { db })
  254. const events = EventV2.layer.pipe(Layer.provide(database))
  255. yield* EventV2.Service.use((service) =>
  256. service.publish(SessionV1.Event.Updated, {
  257. sessionID: SessionSchema.ID.make("session"),
  258. info: {
  259. id: SessionSchema.ID.make("session"),
  260. slug: "session",
  261. projectID: ProjectV2.ID.global,
  262. directory: "/project",
  263. title: "After",
  264. version: "test",
  265. time: { created: 1, updated: 2 },
  266. },
  267. }),
  268. ).pipe(
  269. Effect.provide(
  270. Layer.merge(events, SessionProjector.layer.pipe(Layer.provide(events), Layer.provide(database))),
  271. ),
  272. )
  273. expect(
  274. yield* db.get(sql`
  275. SELECT
  276. (SELECT title FROM session WHERE id = 'session') AS title,
  277. (SELECT workspace_id FROM session WHERE id = 'session') AS workspaceID,
  278. (SELECT COUNT(*) FROM message WHERE id = 'message') AS messages,
  279. (SELECT COUNT(*) FROM part WHERE id = 'part') AS parts,
  280. (SELECT COUNT(*) FROM workspace) AS workspaces,
  281. (SELECT COUNT(*) FROM session_input) AS sessionInputs,
  282. (SELECT COUNT(*) FROM session_message) AS sessionMessages,
  283. (SELECT COUNT(*) FROM session_context_epoch) AS contextEpochs,
  284. (SELECT seq FROM event_sequence WHERE aggregate_id = 'session') AS seq,
  285. (SELECT type FROM event WHERE aggregate_id = 'session') AS eventType
  286. `),
  287. ).toEqual({
  288. title: "After",
  289. workspaceID: null,
  290. messages: 1,
  291. parts: 1,
  292. workspaces: 0,
  293. sessionInputs: 0,
  294. sessionMessages: 0,
  295. contextEpochs: 0,
  296. seq: 0,
  297. eventType: "session.updated.1",
  298. })
  299. }),
  300. )
  301. })
  302. test("resets incompatible projected Session messages before adding sequence order", async () => {
  303. await run(
  304. Effect.gen(function* () {
  305. const db = yield* makeDb
  306. yield* db.run(sql`CREATE TABLE session (id text PRIMARY KEY)`)
  307. yield* db.run(
  308. sql`CREATE TABLE message (id text PRIMARY KEY, session_id text NOT NULL, time_created integer NOT NULL, time_updated integer NOT NULL, data text NOT NULL)`,
  309. )
  310. yield* db.run(
  311. sql`CREATE TABLE part (id text PRIMARY KEY, message_id text NOT NULL, session_id text NOT NULL, time_created integer NOT NULL, time_updated integer NOT NULL, data text NOT NULL)`,
  312. )
  313. yield* db.run(sql`CREATE TABLE event (id text PRIMARY KEY, seq integer NOT NULL)`)
  314. yield* db.run(
  315. sql`CREATE TABLE session_message (id text PRIMARY KEY, session_id text NOT NULL, type text NOT NULL, time_created integer NOT NULL, time_updated integer NOT NULL, data text NOT NULL)`,
  316. )
  317. yield* db.run(
  318. sql`CREATE INDEX session_message_session_time_created_id_idx ON session_message (session_id, time_created, id)`,
  319. )
  320. yield* db.run(
  321. sql`CREATE INDEX session_message_session_type_time_created_id_idx ON session_message (session_id, type, time_created, id)`,
  322. )
  323. yield* db.run(sql`INSERT INTO session (id) VALUES ('session')`)
  324. yield* db.run(
  325. sql`INSERT INTO message (id, session_id, time_created, time_updated, data) VALUES ('legacy_message', 'session', 1, 1, '{"role":"user"}')`,
  326. )
  327. yield* db.run(
  328. sql`INSERT INTO part (id, message_id, session_id, time_created, time_updated, data) VALUES ('legacy_part', 'legacy_message', 'session', 1, 1, '{"type":"text","text":"hello"}')`,
  329. )
  330. yield* db.run(
  331. sql`INSERT INTO session_message (id, session_id, type, time_created, time_updated, data) VALUES ('stale_projection', 'session', 'user', 1, 1, '{}')`,
  332. )
  333. yield* DatabaseMigration.applyOnly(db, [sessionMessageProjectionOrderMigration])
  334. expect(yield* db.all(sql`SELECT id, session_id, data FROM message`)).toEqual([
  335. { id: "legacy_message", session_id: "session", data: '{"role":"user"}' },
  336. ])
  337. expect(yield* db.all(sql`SELECT id, message_id, session_id, data FROM part`)).toEqual([
  338. {
  339. id: "legacy_part",
  340. message_id: "legacy_message",
  341. session_id: "session",
  342. data: '{"type":"text","text":"hello"}',
  343. },
  344. ])
  345. expect(yield* db.all(sql`SELECT id FROM session_message`)).toEqual([])
  346. yield* db.run(
  347. sql`INSERT INTO session_message (id, session_id, type, seq, time_created, time_updated, data) VALUES ('fresh_projection', 'session', 'user', 7, 2, 2, '{}')`,
  348. )
  349. expect(yield* db.get(sql`SELECT id, seq FROM session_message`)).toEqual({ id: "fresh_projection", seq: 7 })
  350. }),
  351. )
  352. })
  353. test("runs session usage backfill in order with schema changes", async () => {
  354. await run(
  355. Effect.gen(function* () {
  356. const db = yield* makeDb
  357. yield* db.run(sql`CREATE TABLE session (id text PRIMARY KEY, time_updated integer NOT NULL)`)
  358. yield* db.run(sql`CREATE TABLE message (id text PRIMARY KEY, session_id text NOT NULL, data text NOT NULL)`)
  359. yield* db.run(sql`INSERT INTO session (id, time_updated) VALUES ('session_1', 1)`)
  360. yield* db.run(
  361. sql`INSERT INTO message (id, session_id, data) VALUES ('message_1', 'session_1', '{"role":"assistant","cost":1.25,"tokens":{"input":2,"output":3,"reasoning":4,"cache":{"read":5,"write":6}}}')`,
  362. )
  363. yield* DatabaseMigration.applyOnly(db, [sessionUsageMigration])
  364. expect(
  365. yield* db.get(
  366. sql`SELECT cost, tokens_input, tokens_output, tokens_reasoning, tokens_cache_read, tokens_cache_write FROM session WHERE id = 'session_1'`,
  367. ),
  368. ).toEqual({
  369. cost: 1.25,
  370. tokens_input: 2,
  371. tokens_output: 3,
  372. tokens_reasoning: 4,
  373. tokens_cache_read: 5,
  374. tokens_cache_write: 6,
  375. })
  376. }),
  377. )
  378. })
  379. test("normalizes Windows storage paths and leaves POSIX paths untouched", async () => {
  380. await run(
  381. Effect.gen(function* () {
  382. const db = yield* makeDb
  383. yield* db.run(sql`CREATE TABLE project (id text PRIMARY KEY, worktree text NOT NULL, sandboxes text NOT NULL)`)
  384. yield* db.run(sql`CREATE TABLE session (id text PRIMARY KEY, directory text NOT NULL, path text)`)
  385. // Windows-shaped rows (drive + backslash) must be normalized.
  386. yield* db.run(
  387. sql`INSERT INTO project (id, worktree, sandboxes) VALUES (${"win"}, ${"C:\\Repo\\Thing"}, ${JSON.stringify([
  388. "C:\\Repo\\Thing\\sandbox",
  389. ])})`,
  390. )
  391. yield* db.run(
  392. sql`INSERT INTO session (id, directory, path) VALUES (${"win"}, ${"C:\\Repo\\Thing\\packages\\api"}, ${"packages\\api"})`,
  393. )
  394. // UNC worktrees and their sandboxes must normalize too (not just drive paths).
  395. yield* db.run(
  396. sql`INSERT INTO project (id, worktree, sandboxes) VALUES (${"unc"}, ${"\\\\server\\share"}, ${JSON.stringify([
  397. "\\\\server\\share\\sandbox",
  398. ])})`,
  399. )
  400. // The "/" worktree sentinel and POSIX paths (including a pathological
  401. // backslash in a POSIX filename) must survive byte-for-byte.
  402. yield* db.run(sql`INSERT INTO project (id, worktree, sandboxes) VALUES (${"global"}, ${"/"}, ${"[]"})`)
  403. yield* db.run(
  404. sql`INSERT INTO session (id, directory, path) VALUES (${"posix"}, ${"/home/me/we\\ird"}, ${"src\\weird"})`,
  405. )
  406. yield* DatabaseMigration.applyOnly(db, [normalizeStoragePathsMigration])
  407. expect(yield* db.get(sql`SELECT worktree, sandboxes FROM project WHERE id = 'win'`)).toEqual({
  408. worktree: "C:/Repo/Thing",
  409. sandboxes: JSON.stringify(["C:/Repo/Thing/sandbox"]),
  410. })
  411. expect(yield* db.get(sql`SELECT directory, path FROM session WHERE id = 'win'`)).toEqual({
  412. directory: "C:/Repo/Thing/packages/api",
  413. path: "packages/api",
  414. })
  415. expect(yield* db.get(sql`SELECT worktree, sandboxes FROM project WHERE id = 'unc'`)).toEqual({
  416. worktree: "//server/share",
  417. sandboxes: JSON.stringify(["//server/share/sandbox"]),
  418. })
  419. expect(yield* db.get(sql`SELECT worktree FROM project WHERE id = 'global'`)).toEqual({ worktree: "/" })
  420. expect(yield* db.get(sql`SELECT directory, path FROM session WHERE id = 'posix'`)).toEqual({
  421. directory: "/home/me/we\\ird",
  422. path: "src\\weird",
  423. })
  424. }),
  425. )
  426. })
  427. test("maps native Windows paths through database columns", async () => {
  428. if (process.platform !== "win32") return
  429. await run(
  430. Effect.gen(function* () {
  431. const db = yield* makeDb
  432. yield* DatabaseMigration.apply(db)
  433. const projectID = ProjectV2.ID.make("codec_project")
  434. const worktree = AbsolutePath.make("C:\\Repo\\Thing")
  435. const sandbox = AbsolutePath.make("C:\\Repo\\Thing\\sandbox")
  436. const directory = "C:\\Repo\\Thing\\packages\\api"
  437. const sessionID = SessionSchema.ID.make("ses_codec")
  438. expect(() =>
  439. Effect.runSync(
  440. db
  441. .insert(ProjectTable)
  442. .values({
  443. id: ProjectV2.ID.make("invalid_path"),
  444. worktree: AbsolutePath.make("not-absolute"),
  445. sandboxes: [],
  446. time_created: 1,
  447. time_updated: 1,
  448. })
  449. .run(),
  450. ),
  451. ).toThrow()
  452. yield* db
  453. .insert(ProjectTable)
  454. .values({
  455. id: projectID,
  456. worktree,
  457. sandboxes: [sandbox],
  458. time_created: 1,
  459. time_updated: 1,
  460. })
  461. .run()
  462. yield* db
  463. .insert(SessionTable)
  464. .values({
  465. id: sessionID,
  466. project_id: projectID,
  467. slug: "codec",
  468. directory,
  469. path: "packages\\api",
  470. title: "Codec",
  471. version: "test",
  472. time_created: 1,
  473. time_updated: 1,
  474. })
  475. .run()
  476. expect(
  477. yield* db.get<{ worktree: string; sandboxes: string }>(
  478. sql`SELECT worktree, sandboxes FROM project WHERE id = ${projectID}`,
  479. ),
  480. ).toEqual({
  481. worktree: "C:/Repo/Thing",
  482. sandboxes: JSON.stringify(["C:/Repo/Thing/sandbox"]),
  483. })
  484. expect(
  485. yield* db.get<{ directory: string; path: string }>(
  486. sql`SELECT directory, path FROM session WHERE id = ${sessionID}`,
  487. ),
  488. ).toEqual({
  489. directory: "C:/Repo/Thing/packages/api",
  490. path: "packages/api",
  491. })
  492. const project = yield* db.select().from(ProjectTable).where(eq(ProjectTable.worktree, worktree)).get()
  493. const session = yield* db.select().from(SessionTable).where(eq(SessionTable.directory, directory)).get()
  494. expect(project?.worktree).toBe(worktree)
  495. expect(project?.sandboxes).toEqual([sandbox])
  496. expect(session?.directory).toBe(directory)
  497. expect(session?.path).toBe("packages/api")
  498. expect((yield* db.select().from(SessionTable).where(eq(SessionTable.path, "packages\\api")).get())?.id).toBe(
  499. sessionID,
  500. )
  501. const moved = AbsolutePath.make("D:\\Moved\\Thing")
  502. const updated = yield* db
  503. .update(ProjectTable)
  504. .set({ worktree: moved, sandboxes: [moved] })
  505. .where(eq(ProjectTable.id, projectID))
  506. .returning()
  507. .get()
  508. expect(updated?.worktree).toBe(moved)
  509. expect(updated?.sandboxes).toEqual([moved])
  510. expect(
  511. yield* db.get<{ worktree: string; sandboxes: string }>(
  512. sql`SELECT worktree, sandboxes FROM project WHERE id = ${projectID}`,
  513. ),
  514. ).toEqual({ worktree: "D:/Moved/Thing", sandboxes: JSON.stringify(["D:/Moved/Thing"]) })
  515. expect(
  516. (yield* db
  517. .select()
  518. .from(ProjectTable)
  519. .where(inArray(ProjectTable.worktree, [moved]))
  520. .get())?.id,
  521. ).toBe(projectID)
  522. yield* db.run(sql`UPDATE project SET worktree = ${"not-absolute"} WHERE id = ${projectID}`)
  523. expect(() =>
  524. Effect.runSync(db.select().from(ProjectTable).where(eq(ProjectTable.id, projectID)).get()),
  525. ).toThrow()
  526. }),
  527. )
  528. })
  529. test("imports existing drizzle migration state", async () => {
  530. await run(
  531. Effect.gen(function* () {
  532. const db = yield* makeDb
  533. yield* db.run(
  534. sql`CREATE TABLE __drizzle_migrations (id INTEGER PRIMARY KEY, hash text NOT NULL, created_at numeric, name text, applied_at TEXT)`,
  535. )
  536. yield* db.run(sql`
  537. INSERT INTO __drizzle_migrations (hash, created_at, name, applied_at)
  538. VALUES ('hash', 1, '20260127222353_familiar_lady_ursula', ${new Date().toISOString()})
  539. `)
  540. yield* DatabaseMigration.applyOnly(db, [])
  541. expect(yield* db.get(sql`SELECT id FROM migration`)).toEqual({ id: "20260127222353_familiar_lady_ursula" })
  542. }),
  543. )
  544. })
  545. test("does not replay a migrated session metadata column", async () => {
  546. await run(
  547. Effect.gen(function* () {
  548. const db = yield* makeDb
  549. yield* db.run(sql`CREATE TABLE session (id text PRIMARY KEY, metadata text)`)
  550. yield* db.run(
  551. sql`CREATE TABLE __drizzle_migrations (id INTEGER PRIMARY KEY, hash text NOT NULL, created_at numeric, name text, applied_at TEXT)`,
  552. )
  553. yield* db.run(sql`
  554. INSERT INTO __drizzle_migrations (hash, created_at, name, applied_at)
  555. VALUES ('hash', 1, '20260511173437_session-metadata', ${new Date().toISOString()})
  556. `)
  557. yield* DatabaseMigration.applyOnly(db, [sessionMetadataMigration])
  558. expect(yield* db.all(sql`SELECT id FROM migration`)).toEqual([{ id: "20260511173437_session-metadata" }])
  559. }),
  560. )
  561. })
  562. test("accepts the temporary replacement session metadata migration id", async () => {
  563. await run(
  564. Effect.gen(function* () {
  565. const db = yield* makeDb
  566. yield* db.run(sql`CREATE TABLE session (id text PRIMARY KEY, metadata text)`)
  567. yield* db.run(sql`CREATE TABLE migration (id TEXT PRIMARY KEY, time_completed INTEGER NOT NULL)`)
  568. yield* db.run(sql`INSERT INTO migration (id, time_completed) VALUES ('20260530232709_lovely_romulus', 1)`)
  569. yield* DatabaseMigration.applyOnly(db, [sessionMetadataMigration])
  570. expect(yield* db.all(sql`SELECT id FROM migration ORDER BY id`)).toEqual([
  571. { id: "20260511173437_session-metadata" },
  572. { id: "20260530232709_lovely_romulus" },
  573. ])
  574. }),
  575. )
  576. })
  577. test("skips drizzle import when migration table already has state", async () => {
  578. await run(
  579. Effect.gen(function* () {
  580. const db = yield* makeDb
  581. yield* db.run(sql`CREATE TABLE migration (id TEXT PRIMARY KEY, time_completed INTEGER NOT NULL)`)
  582. yield* db.run(sql`INSERT INTO migration (id, time_completed) VALUES ('existing', 1)`)
  583. yield* db.run(
  584. sql`CREATE TABLE __drizzle_migrations (id INTEGER PRIMARY KEY, hash text NOT NULL, created_at numeric, name text, applied_at TEXT)`,
  585. )
  586. yield* db.run(sql`
  587. INSERT INTO __drizzle_migrations (hash, created_at, name, applied_at)
  588. VALUES ('hash', 1, '20260127222353_familiar_lady_ursula', ${new Date().toISOString()})
  589. `)
  590. yield* DatabaseMigration.applyOnly(db, [])
  591. expect(yield* db.all(sql`SELECT id FROM migration ORDER BY id`)).toEqual([{ id: "existing" }])
  592. }),
  593. )
  594. })
  595. })