Skip to content

Commit a614b78

Browse files
committed
tui: upgrade database migration system to drizzle migrator
Replaces custom migration system with drizzle-orm's built-in migrator, bundling migrations at build-time instead of runtime generation. This reduces bundle complexity and provides better integration with drizzle's migration tracking.
1 parent b9f5a34 commit a614b78

10 files changed

Lines changed: 260 additions & 190 deletions

File tree

AGENTS.md

Lines changed: 57 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,93 @@
1-
- To regenerate the JavaScript SDK, run `./packages/sdk/js/script/build.ts`.
2-
- ALWAYS USE PARALLEL TOOLS WHEN APPLICABLE.
3-
- The default branch in this repo is `dev`.
1+
# Agent Instructions
2+
3+
## Quick Reference
4+
5+
- Default branch: `dev`
6+
- Regenerate JS SDK: `./packages/sdk/js/script/build.ts`
7+
- Always use parallel tools when applicable
48

59
## Style Guide
610

11+
### General Principles
12+
713
- Keep things in one function unless composable or reusable
8-
- Avoid unnecessary destructuring. Instead of `const { a, b } = obj`, use `obj.a` and `obj.b` to preserve context
914
- Avoid `try`/`catch` where possible
1015
- Avoid using the `any` type
11-
- Prefer single word variable names where possible
12-
- Use Bun APIs when possible, like `Bun.file()`
16+
- Use Bun APIs when possible (e.g., `Bun.file()` instead of `fs.existsSync()`)
17+
- For sync file reads, use `readFileSync` from `fs` (Bun.file is async-only)
18+
- Avoid generated file artifacts - prefer build-time `define` globals for bundled data
1319

14-
### Avoid let statements
20+
### Naming
1521

16-
We don't like `let` statements, especially combined with if/else statements.
17-
Prefer `const`.
22+
Prefer single word variable names. Only use multiple words if necessary.
23+
24+
```ts
25+
// Good
26+
const foo = 1
27+
const bar = 2
28+
29+
// Bad
30+
const fooBar = 1
31+
const barBaz = 2
32+
```
1833

19-
Good:
34+
Reduce total variable count by inlining when a value is only used once.
2035

2136
```ts
22-
const foo = condition ? 1 : 2
37+
// Good
38+
const journal = await Bun.file(path.join(dir, "journal.json")).json()
39+
40+
// Bad
41+
const journalPath = path.join(dir, "journal.json")
42+
const journal = await Bun.file(journalPath).json()
2343
```
2444

25-
Bad:
45+
### Destructuring
46+
47+
Avoid unnecessary destructuring. Use dot notation to preserve context.
2648

2749
```ts
28-
let foo
50+
// Good
51+
obj.a
52+
obj.b
53+
54+
// Bad
55+
const { a, b } = obj
56+
```
57+
58+
### Variables
59+
60+
Prefer `const` over `let`. Use ternaries or early returns instead of reassignment.
2961

62+
```ts
63+
// Good
64+
const foo = condition ? 1 : 2
65+
66+
// Bad
67+
let foo
3068
if (condition) foo = 1
3169
else foo = 2
3270
```
3371

34-
### Avoid else statements
35-
36-
Prefer early returns or using an `iife` to avoid else statements.
72+
### Control Flow
3773

38-
Good:
74+
Avoid `else` statements. Prefer early returns.
3975

4076
```ts
77+
// Good
4178
function foo() {
4279
if (condition) return 1
4380
return 2
4481
}
45-
```
46-
47-
Bad:
4882

49-
```ts
83+
// Bad
5084
function foo() {
5185
if (condition) return 1
5286
else return 2
5387
}
5488
```
5589

56-
### Prefer single word naming
57-
58-
Try your best to find a single word name for your variables, functions, etc.
59-
Only use multiple words if you cannot.
60-
61-
Good:
62-
63-
```ts
64-
const foo = 1
65-
const bar = 2
66-
const baz = 3
67-
```
68-
69-
Bad:
70-
71-
```ts
72-
const fooBar = 1
73-
const barBaz = 2
74-
const bazFoo = 3
75-
```
76-
7790
## Testing
7891

79-
You MUST avoid using `mocks` as much as possible.
80-
Tests MUST test actual implementation, do not duplicate logic into a test.
92+
- Avoid mocks as much as possible
93+
- Test actual implementation, do not duplicate logic into tests

bun.lock

Lines changed: 115 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/opencode/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"lint": "echo 'Running lint checks...' && bun test --coverage",
1616
"format": "echo 'Formatting code...' && bun run --prettier --write src/**/*.ts",
1717
"docs": "echo 'Generating documentation...' && find src -name '*.ts' -exec echo 'Processing: {}' \\;",
18-
"deploy": "echo 'Deploying application...' && bun run build && echo 'Deployment completed successfully'"
18+
"deploy": "echo 'Deploying application...' && bun run build && echo 'Deployment completed successfully'",
19+
"db": "bun drizzle-kit"
1920
},
2021
"bin": {
2122
"opencode": "./bin/opencode"
@@ -25,7 +26,7 @@
2526
},
2627
"devDependencies": {
2728
"@babel/core": "7.28.4",
28-
"drizzle-kit": "0.31.0",
29+
"drizzle-kit": "1.0.0-beta.12-a5629fb",
2930
"@octokit/webhooks-types": "7.6.1",
3031
"@opencode-ai/script": "workspace:*",
3132
"@parcel/watcher-darwin-arm64": "2.5.1",
@@ -98,7 +99,7 @@
9899
"clipboardy": "4.0.0",
99100
"decimal.js": "10.5.0",
100101
"diff": "catalog:",
101-
"drizzle-orm": "0.44.2",
102+
"drizzle-orm": "1.0.0-beta.12-a5629fb",
102103
"fuzzysort": "3.1.0",
103104
"gray-matter": "4.0.3",
104105
"hono": "catalog:",

packages/opencode/script/build.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ await Bun.write(
2525
)
2626
console.log("Generated models-snapshot.ts")
2727

28+
// Load migrations from journal
29+
const journal = (await Bun.file(path.join(dir, "migration/meta/_journal.json")).json()) as {
30+
entries: { tag: string; when: number }[]
31+
}
32+
const migrations = await Promise.all(
33+
journal.entries.map(async (entry) => {
34+
const sql = await Bun.file(path.join(dir, `migration/${entry.tag}.sql`)).text()
35+
return { sql, timestamp: entry.when }
36+
}),
37+
)
38+
console.log(`Loaded ${migrations.length} migrations`)
39+
2840
const singleFlag = process.argv.includes("--single")
2941
const baselineFlag = process.argv.includes("--baseline")
3042
const skipInstall = process.argv.includes("--skip-install")
@@ -156,6 +168,7 @@ for (const item of targets) {
156168
entrypoints: ["./src/index.ts", parserWorker, workerPath],
157169
define: {
158170
OPENCODE_VERSION: `'${Script.version}'`,
171+
OPENCODE_MIGRATIONS: JSON.stringify(migrations),
159172
OTUI_TREE_SITTER_WORKER_PATH: bunfsRoot + workerRelativePath,
160173
OPENCODE_WORKER_PATH: workerPath,
161174
OPENCODE_CHANNEL: `'${Script.channel}'`,

packages/opencode/script/generate-migrations.ts

Lines changed: 0 additions & 49 deletions
This file was deleted.

packages/opencode/src/cli/cmd/database.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,6 @@ const ExportCommand = cmd({
129129
stats.shares++
130130
}
131131

132-
// Create migration marker so this can be imported back
133-
await Bun.write(path.join(outDir, "migration"), Date.now().toString())
134-
135132
UI.println(`Exported to ${outDir}:`)
136133
UI.println(` ${stats.projects} projects`)
137134
UI.println(` ${stats.sessions} sessions`)

0 commit comments

Comments
 (0)