@@ -12,8 +12,10 @@ import z from "zod"
1212import path from "path"
1313import { readFileSync , readdirSync , existsSync } from "fs"
1414import * as schema from "./schema"
15+ import { Installation } from "../installation"
16+ import { Flag } from "../flag/flag"
1517
16- declare const OPENCODE_MIGRATIONS : { sql : string ; timestamp : number } [ ] | undefined
18+ declare const OPENCODE_MIGRATIONS : { sql : string ; timestamp : number ; name : string } [ ] | undefined
1719
1820export const NotFoundError = NamedError . create (
1921 "NotFoundError" ,
@@ -25,13 +27,20 @@ export const NotFoundError = NamedError.create(
2527const log = Log . create ( { service : "db" } )
2628
2729export namespace Database {
28- export const Path = path . join ( Global . Path . data , "opencode.db" )
30+ export const Path = ( ( ) => {
31+ const name =
32+ Installation . CHANNEL !== "latest" && ! Flag . OPENCODE_DISABLE_CHANNEL_DB
33+ ? `opencode-${ Installation . CHANNEL } .db`
34+ : "opencode.db"
35+ return path . join ( Global . Path . data , name )
36+ } ) ( )
37+
2938 type Schema = typeof schema
3039 export type Transaction = SQLiteTransaction < "sync" , void , Schema >
3140
3241 type Client = SQLiteBunDatabase < Schema >
3342
34- type Journal = { sql : string ; timestamp : number } [ ]
43+ type Journal = { sql : string ; timestamp : number ; name : string } [ ]
3544
3645 const state = {
3746 sqlite : undefined as BunDatabase | undefined ,
@@ -62,6 +71,7 @@ export namespace Database {
6271 return {
6372 sql : readFileSync ( file , "utf-8" ) ,
6473 timestamp : time ( name ) ,
74+ name,
6575 }
6676 } )
6777 . filter ( Boolean ) as Journal
@@ -70,9 +80,9 @@ export namespace Database {
7080 }
7181
7282 export const Client = lazy ( ( ) => {
73- log . info ( "opening database" , { path : path . join ( Global . Path . data , "opencode.db" ) } )
83+ log . info ( "opening database" , { path : Path } )
7484
75- const sqlite = new BunDatabase ( path . join ( Global . Path . data , "opencode.db" ) , { create : true } )
85+ const sqlite = new BunDatabase ( Path , { create : true } )
7686 state . sqlite = sqlite
7787
7888 sqlite . run ( "PRAGMA journal_mode = WAL" )
@@ -143,7 +153,7 @@ export namespace Database {
143153 } catch ( err ) {
144154 if ( err instanceof Context . NotFound ) {
145155 const effects : ( ( ) => void | Promise < void > ) [ ] = [ ]
146- const result = Client ( ) . transaction ( ( tx ) => {
156+ const result = ( Client ( ) . transaction as any ) ( ( tx : TxOrDb ) => {
147157 return ctx . provide ( { tx, effects } , ( ) => callback ( tx ) )
148158 } )
149159 for ( const effect of effects ) effect ( )
0 commit comments