database-migration.test.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import { describe, expect, test } from "bun:test"
  2. import { $ } from "bun"
  3. import { fileURLToPath } from "url"
  4. import { SqliteClient } from "@effect/sql-sqlite-bun"
  5. import { EffectDrizzleSqlite } from "@opencode-ai/effect-drizzle-sqlite"
  6. import { Effect } from "effect"
  7. import { sql } from "drizzle-orm"
  8. import { DatabaseMigration } from "@opencode-ai/core/database/migration"
  9. import sessionUsageMigration from "@opencode-ai/core/database/migration/20260510033149_session_usage"
  10. import sessionMetadataMigration from "@opencode-ai/core/database/migration/20260511173437_session-metadata"
  11. import type { SqlClient as SqlClientService } from "effect/unstable/sql/SqlClient"
  12. const run = <A, E>(effect: Effect.Effect<A, E, SqlClientService>) =>
  13. Effect.runPromise(
  14. effect.pipe(Effect.provide(SqliteClient.layer({ filename: ":memory:", disableWAL: true })), Effect.scoped),
  15. )
  16. const makeDb = EffectDrizzleSqlite.makeWithDefaults()
  17. describe("DatabaseMigration", () => {
  18. if (process.platform === "linux") {
  19. test("declared schema has no ungenerated migrations", async () => {
  20. const result = await $`bun ${fileURLToPath(new URL("../script/migration.ts", import.meta.url))} --check`
  21. .quiet()
  22. .nothrow()
  23. expect(result.exitCode, result.stderr.toString()).toBe(0)
  24. expect(result.stdout.toString()).toContain("No schema changes, nothing to migrate")
  25. }, 30_000)
  26. }
  27. test("applies tracked migrations to an empty database", async () => {
  28. await run(
  29. Effect.gen(function* () {
  30. const db = yield* makeDb
  31. yield* DatabaseMigration.apply(db)
  32. expect(yield* db.get(sql`SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'session'`)).toEqual({
  33. name: "session",
  34. })
  35. expect(yield* db.get(sql`SELECT count(*) as count FROM migration`)).toEqual({ count: 21 })
  36. }),
  37. )
  38. })
  39. test("runs session usage backfill in order with schema changes", async () => {
  40. await run(
  41. Effect.gen(function* () {
  42. const db = yield* makeDb
  43. yield* db.run(sql`CREATE TABLE session (id text PRIMARY KEY, time_updated integer NOT NULL)`)
  44. yield* db.run(sql`CREATE TABLE message (id text PRIMARY KEY, session_id text NOT NULL, data text NOT NULL)`)
  45. yield* db.run(sql`INSERT INTO session (id, time_updated) VALUES ('session_1', 1)`)
  46. yield* db.run(
  47. 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}}}')`,
  48. )
  49. yield* DatabaseMigration.applyOnly(db, [sessionUsageMigration])
  50. expect(
  51. yield* db.get(
  52. sql`SELECT cost, tokens_input, tokens_output, tokens_reasoning, tokens_cache_read, tokens_cache_write FROM session WHERE id = 'session_1'`,
  53. ),
  54. ).toEqual({
  55. cost: 1.25,
  56. tokens_input: 2,
  57. tokens_output: 3,
  58. tokens_reasoning: 4,
  59. tokens_cache_read: 5,
  60. tokens_cache_write: 6,
  61. })
  62. }),
  63. )
  64. })
  65. test("imports existing drizzle migration state", async () => {
  66. await run(
  67. Effect.gen(function* () {
  68. const db = yield* makeDb
  69. yield* db.run(
  70. sql`CREATE TABLE __drizzle_migrations (id INTEGER PRIMARY KEY, hash text NOT NULL, created_at numeric, name text, applied_at TEXT)`,
  71. )
  72. yield* db.run(sql`
  73. INSERT INTO __drizzle_migrations (hash, created_at, name, applied_at)
  74. VALUES ('hash', 1, '20260127222353_familiar_lady_ursula', ${new Date().toISOString()})
  75. `)
  76. yield* DatabaseMigration.applyOnly(db, [])
  77. expect(yield* db.get(sql`SELECT id FROM migration`)).toEqual({ id: "20260127222353_familiar_lady_ursula" })
  78. }),
  79. )
  80. })
  81. test("does not replay a migrated session metadata column", async () => {
  82. await run(
  83. Effect.gen(function* () {
  84. const db = yield* makeDb
  85. yield* db.run(sql`CREATE TABLE session (id text PRIMARY KEY, metadata text)`)
  86. yield* db.run(
  87. sql`CREATE TABLE __drizzle_migrations (id INTEGER PRIMARY KEY, hash text NOT NULL, created_at numeric, name text, applied_at TEXT)`,
  88. )
  89. yield* db.run(sql`
  90. INSERT INTO __drizzle_migrations (hash, created_at, name, applied_at)
  91. VALUES ('hash', 1, '20260511173437_session-metadata', ${new Date().toISOString()})
  92. `)
  93. yield* DatabaseMigration.applyOnly(db, [sessionMetadataMigration])
  94. expect(yield* db.all(sql`SELECT id FROM migration`)).toEqual([{ id: "20260511173437_session-metadata" }])
  95. }),
  96. )
  97. })
  98. test("accepts the temporary replacement session metadata migration id", async () => {
  99. await run(
  100. Effect.gen(function* () {
  101. const db = yield* makeDb
  102. yield* db.run(sql`CREATE TABLE session (id text PRIMARY KEY, metadata text)`)
  103. yield* db.run(sql`CREATE TABLE migration (id TEXT PRIMARY KEY, time_completed INTEGER NOT NULL)`)
  104. yield* db.run(sql`INSERT INTO migration (id, time_completed) VALUES ('20260530232709_lovely_romulus', 1)`)
  105. yield* DatabaseMigration.applyOnly(db, [sessionMetadataMigration])
  106. expect(yield* db.all(sql`SELECT id FROM migration ORDER BY id`)).toEqual([
  107. { id: "20260511173437_session-metadata" },
  108. { id: "20260530232709_lovely_romulus" },
  109. ])
  110. }),
  111. )
  112. })
  113. test("skips drizzle import when migration table already has state", async () => {
  114. await run(
  115. Effect.gen(function* () {
  116. const db = yield* makeDb
  117. yield* db.run(sql`CREATE TABLE migration (id TEXT PRIMARY KEY, time_completed INTEGER NOT NULL)`)
  118. yield* db.run(sql`INSERT INTO migration (id, time_completed) VALUES ('existing', 1)`)
  119. yield* db.run(
  120. sql`CREATE TABLE __drizzle_migrations (id INTEGER PRIMARY KEY, hash text NOT NULL, created_at numeric, name text, applied_at TEXT)`,
  121. )
  122. yield* db.run(sql`
  123. INSERT INTO __drizzle_migrations (hash, created_at, name, applied_at)
  124. VALUES ('hash', 1, '20260127222353_familiar_lady_ursula', ${new Date().toISOString()})
  125. `)
  126. yield* DatabaseMigration.applyOnly(db, [])
  127. expect(yield* db.all(sql`SELECT id FROM migration ORDER BY id`)).toEqual([{ id: "existing" }])
  128. }),
  129. )
  130. })
  131. })