@@ -26,7 +26,7 @@ export class DatabaseInUseError extends Schema.TaggedError<DatabaseInUseError>()
2626 { path : Schema . String , cause : Schema . Defect ( ) } ,
2727) {
2828 override get message ( ) {
29- return "Stop the T3 Code server and desktop app before compacting the database." ;
29+ return "Stop the T3 Code server and desktop app, including any version before 0.1.0, before repairing the database." ;
3030 }
3131}
3232
@@ -99,7 +99,7 @@ export class DatabaseMaintenance extends Context.Service<
9999 */
100100 readonly removeLegacyDatabase : (
101101 databasePath : string ,
102- ) => Effect . Effect < void , LegacyDatabaseRemoveError > ;
102+ ) => Effect . Effect < void , DatabaseInUseError | LegacyDatabaseRemoveError > ;
103103 }
104104> ( ) ( "t3/persistence/DatabaseMaintenance" ) { }
105105
@@ -121,6 +121,26 @@ const readStorage = Effect.gen(function* () {
121121 } ;
122122} ) ;
123123
124+ /**
125+ * Holds an exclusive lock on the connection's database. In WAL mode this is
126+ * refused while any other connection has the file open, even an idle one,
127+ * which is how a running server or desktop app is detected.
128+ */
129+ const lockExclusively = ( databasePath : string ) =>
130+ Effect . gen ( function * ( ) {
131+ const sql = yield * SqlClient . SqlClient ;
132+ yield * sql `PRAGMA busy_timeout = 0` ;
133+ yield * sql `PRAGMA locking_mode = EXCLUSIVE` ;
134+ yield * sql `BEGIN EXCLUSIVE` . pipe (
135+ Effect . mapError ( ( cause ) =>
136+ isSqlError ( cause ) && cause . reason . _tag === "LockTimeoutError"
137+ ? new DatabaseInUseError ( { path : databasePath , cause } )
138+ : cause ,
139+ ) ,
140+ ) ;
141+ yield * sql `COMMIT` ;
142+ } ) ;
143+
124144const make = Effect . gen ( function * ( ) {
125145 const fs = yield * FileSystem . FileSystem ;
126146 const path = yield * Path . Path ;
@@ -159,19 +179,8 @@ const make = Effect.gen(function* () {
159179 const beforeBytes = Number ( ( yield * fs . stat ( databasePath ) ) . size ) ;
160180 const result = yield * Effect . gen ( function * ( ) {
161181 const sql = yield * SqlClient . SqlClient ;
162- // Hold an exclusive lock through compaction and VACUUM. In WAL mode this
163- // is refused while any other connection has the file open, even an idle
164- // one, which is how a running server or desktop app is detected.
165- yield * sql `PRAGMA busy_timeout = 0` ;
166- yield * sql `PRAGMA locking_mode = EXCLUSIVE` ;
167- yield * sql `BEGIN EXCLUSIVE` . pipe (
168- Effect . mapError ( ( cause ) =>
169- isSqlError ( cause ) && cause . reason . _tag === "LockTimeoutError"
170- ? new DatabaseInUseError ( { path : databasePath , cause } )
171- : cause ,
172- ) ,
173- ) ;
174- yield * sql `COMMIT` ;
182+ // Held through compaction and VACUUM.
183+ yield * lockExclusively ( databasePath ) ;
175184 const maintenance = yield * ProjectionMaintenance . ProjectionMaintenanceV2 ;
176185 if ( ! ( yield * maintenance . verify ) . valid ) {
177186 return yield * new DatabaseProjectionsInvalidError ( { path : databasePath } ) ;
@@ -213,14 +222,23 @@ const make = Effect.gen(function* () {
213222 const removeLegacyDatabase = Effect . fn ( "DatabaseMaintenance.removeLegacyDatabase" ) (
214223 function * ( databasePath : string ) {
215224 const legacy = legacyPath ( databasePath ) ;
216- for ( const suffix of [ "-wal" , "-shm" , "" ] ) {
217- yield * fs . remove ( legacy + suffix , { force : true } ) ;
218- }
225+ if ( ! ( yield * fs . exists ( legacy ) ) ) return ;
226+ // A version before 0.1.0 may still be running against it. Keep the lock
227+ // while deleting, and delete the database before its WAL so a failure
228+ // never leaves it without committed transactions.
229+ yield * Effect . gen ( function * ( ) {
230+ yield * lockExclusively ( legacy ) ;
231+ for ( const suffix of [ "" , "-wal" , "-shm" ] ) {
232+ yield * fs . remove ( legacy + suffix , { force : true } ) ;
233+ }
234+ } ) . pipe ( Effect . provide ( NodeSqliteClient . layer ( { filename : legacy } ) ) ) ;
219235 } ,
220236 ( effect , databasePath ) =>
221237 effect . pipe (
222- Effect . mapError (
223- ( cause ) => new LegacyDatabaseRemoveError ( { path : legacyPath ( databasePath ) , cause } ) ,
238+ Effect . mapError ( ( cause ) =>
239+ cause . _tag === "DatabaseInUseError"
240+ ? cause
241+ : new LegacyDatabaseRemoveError ( { path : legacyPath ( databasePath ) , cause } ) ,
224242 ) ,
225243 ) ,
226244 ) ;
0 commit comments