@@ -2,8 +2,8 @@ import { type SQLiteBunDatabase } from "drizzle-orm/bun-sqlite"
22import { migrate } from "drizzle-orm/bun-sqlite/migrator"
33import { type SQLiteTransaction } from "drizzle-orm/sqlite-core"
44export * from "drizzle-orm"
5+ import { RuntimeFlags } from "@/effect/runtime-flags"
56import { LocalContext } from "@/util/local-context"
6- import { lazy } from "../util/lazy"
77import { Global } from "@opencode-ai/core/global"
88import * as Log from "@opencode-ai/core/util/log"
99import { NamedError } from "@opencode-ai/core/util/error"
@@ -12,9 +12,8 @@ import { readFileSync, readdirSync, existsSync } from "fs"
1212import { Flag } from "@opencode-ai/core/flag/flag"
1313import { InstallationChannel } from "@opencode-ai/core/installation/version"
1414import { InstanceState } from "@/effect/instance-state"
15- import { iife } from "@/util/iife"
1615import { init } from "#db"
17- import { Schema } from "effect"
16+ import { Effect , Schema } from "effect"
1817
1918declare const OPENCODE_MIGRATIONS : { sql : string ; timestamp : number ; name : string } [ ] | undefined
2019
@@ -24,24 +23,29 @@ export const NotFoundError = NamedError.create("NotFoundError", {
2423
2524const log = Log . create ( { service : "db" } )
2625
27- export function getChannelPath ( ) {
28- if ( [ "latest" , "beta" , "prod" ] . includes ( InstallationChannel ) || Flag . OPENCODE_DISABLE_CHANNEL_DB )
26+ type ChannelDbFlags = Pick < RuntimeFlags . Info , "disableChannelDb" >
27+
28+ const readRuntimeFlags = ( ) =>
29+ Effect . runSync ( RuntimeFlags . Service . useSync ( ( flags ) => flags ) . pipe ( Effect . provide ( RuntimeFlags . defaultLayer ) ) )
30+
31+ export function getChannelPath ( flags : ChannelDbFlags = readRuntimeFlags ( ) ) {
32+ if ( [ "latest" , "beta" , "prod" ] . includes ( InstallationChannel ) || flags . disableChannelDb )
2933 return path . join ( Global . Path . data , "opencode.db" )
3034 const safe = InstallationChannel . replace ( / [ ^ a - z A - Z 0 - 9 . _ - ] / g, "-" )
3135 return path . join ( Global . Path . data , `opencode-${ safe } .db` )
3236}
3337
34- export const Path = iife ( ( ) => {
38+ export const getPath = ( flags ?: ChannelDbFlags ) => {
3539 if ( Flag . OPENCODE_DB ) {
3640 if ( Flag . OPENCODE_DB === ":memory:" || path . isAbsolute ( Flag . OPENCODE_DB ) ) return Flag . OPENCODE_DB
3741 return path . join ( Global . Path . data , Flag . OPENCODE_DB )
3842 }
39- return getChannelPath ( )
40- } )
43+ return getChannelPath ( flags )
44+ }
4145
4246export type Transaction = SQLiteTransaction < "sync" , void >
4347
44- type Client = SQLiteBunDatabase
48+ type Client = ReturnType < typeof init >
4549
4650type Journal = { sql : string ; timestamp : number ; name : string } [ ]
4751
@@ -85,38 +89,55 @@ function migrations(dir: string): Journal {
8589 return sql . sort ( ( a , b ) => a . timestamp - b . timestamp )
8690}
8791
88- export const Client = lazy ( ( ) => {
89- log . info ( "opening database" , { path : Path } )
90-
91- const db = init ( Path )
92-
93- db . run ( "PRAGMA journal_mode = WAL" )
94- db . run ( "PRAGMA synchronous = NORMAL" )
95- db . run ( "PRAGMA busy_timeout = 5000" )
96- db . run ( "PRAGMA cache_size = -64000" )
97- db . run ( "PRAGMA foreign_keys = ON" )
98- db . run ( "PRAGMA wal_checkpoint(PASSIVE)" )
99-
100- // Apply schema migrations
101- const entries =
102- typeof OPENCODE_MIGRATIONS !== "undefined"
103- ? OPENCODE_MIGRATIONS
104- : migrations ( path . join ( import . meta. dirname , "../../migration" ) )
105- if ( entries . length > 0 ) {
106- log . info ( "applying migrations" , {
107- count : entries . length ,
108- mode : typeof OPENCODE_MIGRATIONS !== "undefined" ? "bundled" : "dev" ,
109- } )
110- if ( Flag . OPENCODE_SKIP_MIGRATIONS ) {
111- for ( const item of entries ) {
112- item . sql = "select 1;"
92+ let client : Client | undefined
93+ let loaded = false
94+
95+ export const Client = Object . assign (
96+ ( flags ?: ChannelDbFlags ) : Client => {
97+ if ( loaded ) return client as Client
98+
99+ const dbPath = getPath ( flags )
100+ log . info ( "opening database" , { path : dbPath } )
101+
102+ const db = init ( dbPath )
103+
104+ db . run ( "PRAGMA journal_mode = WAL" )
105+ db . run ( "PRAGMA synchronous = NORMAL" )
106+ db . run ( "PRAGMA busy_timeout = 5000" )
107+ db . run ( "PRAGMA cache_size = -64000" )
108+ db . run ( "PRAGMA foreign_keys = ON" )
109+ db . run ( "PRAGMA wal_checkpoint(PASSIVE)" )
110+
111+ // Apply schema migrations
112+ const entries =
113+ typeof OPENCODE_MIGRATIONS !== "undefined"
114+ ? OPENCODE_MIGRATIONS
115+ : migrations ( path . join ( import . meta. dirname , "../../migration" ) )
116+ if ( entries . length > 0 ) {
117+ log . info ( "applying migrations" , {
118+ count : entries . length ,
119+ mode : typeof OPENCODE_MIGRATIONS !== "undefined" ? "bundled" : "dev" ,
120+ } )
121+ if ( Flag . OPENCODE_SKIP_MIGRATIONS ) {
122+ for ( const item of entries ) {
123+ item . sql = "select 1;"
124+ }
113125 }
126+ applyMigrations ( db , entries )
114127 }
115- applyMigrations ( db , entries )
116- }
117128
118- return db
119- } )
129+ client = db
130+ loaded = true
131+ return db
132+ } ,
133+ {
134+ reset : ( ) => {
135+ loaded = false
136+ client = undefined
137+ } ,
138+ loaded : ( ) => loaded ,
139+ } ,
140+ )
120141
121142export function close ( ) {
122143 if ( ! Client . loaded ( ) ) return
0 commit comments