|
| 1 | +import { describe, expect, test } from "bun:test" |
| 2 | +import { Database } from "bun:sqlite" |
| 3 | +import { drizzle } from "drizzle-orm/bun-sqlite" |
| 4 | +import { migrate } from "drizzle-orm/bun-sqlite/migrator" |
| 5 | +import { readFileSync, readdirSync } from "fs" |
| 6 | +import path from "path" |
| 7 | + |
| 8 | +const target = "20260507164347_add_workspace_time" |
| 9 | + |
| 10 | +function migrations() { |
| 11 | + return readdirSync(path.join(import.meta.dirname, "../../migration"), { withFileTypes: true }) |
| 12 | + .filter((entry) => entry.isDirectory()) |
| 13 | + .map((entry) => ({ |
| 14 | + name: entry.name, |
| 15 | + timestamp: Number(entry.name.split("_")[0]), |
| 16 | + sql: readFileSync(path.join(import.meta.dirname, "../../migration", entry.name, "migration.sql"), "utf-8"), |
| 17 | + })) |
| 18 | + .sort((a, b) => a.timestamp - b.timestamp) |
| 19 | +} |
| 20 | + |
| 21 | +describe("workspace time migration", () => { |
| 22 | + test("migrates existing workspace rows", () => { |
| 23 | + const sqlite = new Database(":memory:") |
| 24 | + const db = drizzle({ client: sqlite }) |
| 25 | + const entries = migrations() |
| 26 | + const index = entries.findIndex((entry) => entry.name === target) |
| 27 | + |
| 28 | + expect(index).toBeGreaterThan(0) |
| 29 | + |
| 30 | + migrate(db, entries.slice(0, index)) |
| 31 | + sqlite.run( |
| 32 | + "INSERT INTO project (id, worktree, vcs, name, time_created, time_updated, sandboxes) VALUES (?, ?, ?, ?, ?, ?, ?)", |
| 33 | + ["project_1", "/tmp/project", "git", "project", 1, 1, "[]"], |
| 34 | + ) |
| 35 | + sqlite.run( |
| 36 | + "INSERT INTO workspace (id, type, name, branch, directory, extra, project_id) VALUES (?, ?, ?, ?, ?, ?, ?)", |
| 37 | + ["workspace_1", "local", "main", "main", "/tmp/project", null, "project_1"], |
| 38 | + ) |
| 39 | + |
| 40 | + expect(() => migrate(db, entries.slice(index))).not.toThrow() |
| 41 | + expect(sqlite.query("SELECT time_used FROM workspace WHERE id = ?").get("workspace_1")).toEqual({ time_used: 0 }) |
| 42 | + }) |
| 43 | +}) |
0 commit comments