Zoltan Demant
07/04/2025, 4:17 AMprivate val database by lazy {
scope.async { init() }
}
suspend fun database() = database.await()
The suspend fun init()
function creates the actual database and makes sure some template data is inserted the first time around. The whole point is that this happens before other areas of my app can touch the database. Ive been doing this for years and it has worked brilliantly, but recently something seems to have changed: under some scenario, the database is accessed before the template data is inserted. Ill add some more details in 🧵 in order to keep this somewhat short.Zoltan Demant
07/04/2025, 4:18 AMZoltan Demant
07/04/2025, 4:20 AMZoltan Demant
07/04/2025, 4:23 AMZoltan Demant
07/04/2025, 4:25 AMfind:
SELECT *
FROM muscleSql
WHERE type=?;
The call-site:
val row =
muscleQueries.find(type).executeAsOneOrNull()
?: error("No model found by type: $type")
Zoltan Demant
07/04/2025, 4:27 AMeygraber
07/04/2025, 4:28 AMZoltan Demant
07/04/2025, 4:30 AMI feel like this shouldnt be possible. Thats the reason why Im posting here - my only other suspicion is that somehow, the query fails, despite the underlying data being there?This, I have also upgraded sqldelight to its latest version since a few weeks back (ish). Ive also had the same logic running and working for several years before this.
eygraber
07/04/2025, 4:35 AMinit
function is doing)?Zoltan Demant
07/04/2025, 4:38 AMactual class SqlEnvironment(
private val context: Context,
) {
actual fun driver(
name: String,
schema: SqlSchema<Value<Unit>>,
): SqlDriver = AndroidSqliteDriver(schema, context, name)
}
The class w/ the database is injected using koin single { .. }
and the firebase logic is all run in the same process as the rest of the app.eygraber
07/04/2025, 4:46 AMname
constant or is it derived from something?
Do you have a consistent repro for this?Zoltan Demant
07/04/2025, 4:50 AMeygraber
07/04/2025, 4:54 AMZoltan Demant
07/04/2025, 5:01 AMeygraber
07/04/2025, 5:08 AMinit
creates the driver and database, and then inserts template data if it doesn't already exist, and then returns the database?Zoltan Demant
07/04/2025, 5:15 AMeygraber
07/04/2025, 5:19 AMinit
?Zoltan Demant
07/04/2025, 5:38 AMZoltan Demant
07/04/2025, 5:40 AMeygraber
07/04/2025, 5:47 AMZoltan Demant
07/04/2025, 5:51 AMdatabase.transaction { .. }
? If so - yes, all edits to the database happen in this way.eygraber
07/04/2025, 5:55 AMsuspend fun database()
?Zoltan Demant
07/04/2025, 5:58 AMeygraber
07/04/2025, 6:06 AMmuscleQueries.find
is being called on an instance returned from database()
?Zoltan Demant
07/04/2025, 6:07 AMeygraber
07/04/2025, 6:10 AMZoltan Demant
07/04/2025, 6:13 AMeygraber
07/04/2025, 6:18 AMtype
is not actually present (either it wasn't part of the data that was inserted, the insert failed, something removed it, etc...) when find
runs.
I can't think of how SqlDelight would cause this, but it's hard to say without seeing the whole picture.Zoltan Demant
07/04/2025, 6:28 AMtype
specifically points to one of the template ones.Zoltan Demant
07/04/2025, 6:29 AMZoltan Demant
07/04/2025, 6:31 AMZoltan Demant
07/04/2025, 6:32 AMZoltan Demant
07/04/2025, 6:33 AMZoltan Demant
07/04/2025, 6:34 AMZoltan Demant
07/04/2025, 6:34 AMeygraber
07/04/2025, 6:35 AMtype
you query for was present before calling find
?
While corruption is technically possible, it would be very very unlikely.Zoltan Demant
07/04/2025, 6:35 AMZoltan Demant
07/04/2025, 6:38 AMZoltan Demant
07/04/2025, 6:43 AMeygraber
07/04/2025, 6:51 AMZoltan Demant
07/04/2025, 9:42 AMfind
call to fail?
Ive ran through the logic a billion times today without it ever crashing. But, I hate that I cant figure out what was causing it 😅eygraber
07/04/2025, 5:12 PM