mattermost-mobile-upstream/android
yasser khan f5e7f9982b
fix(android): open every SQLite connection in WAL, not just the JSI one (#10117)
* fix(android): open every SQLite connection in WAL, not just the JSI one

#10109 re-enabled `pragma journal_mode = WAL` in WatermelonDB's shared
Database.cpp. Android runs WatermelonDB through JSI -- watermelondb-jsi is in
settings.gradle and WatermelonDBJSIPackage is registered in MainApplication --
so that pragma applies there, and both databases are created with `jsi: true`.

But the app also opens those same files from native Kotlin, on the push
notification path, and every one of those connections was opened without WAL:

  - DatabaseHelper.setDefaultDatabase -> WMDatabase.getInstance(path, context),
    whose 2-arg overload this repo's patch had stripped ENABLE_WRITE_AHEAD_LOGGING
    from (app.db)
  - getDatabaseForServer -> WMDatabase.buildDatabase(..., CREATE_IF_NECESSARY),
    hardcoded here, for every server database
  - WMDatabaseDriver's constructor, stripped by the same patch, for the bridge
    path WatermelonDB falls back to when JSI is unavailable

Journal mode is a property of the database file, not of a connection. When
Android's SQLiteConnection opens a database without ENABLE_WRITE_AHEAD_LOGGING it
issues its own `PRAGMA journal_mode` on open and drags the file back out of WAL
underneath the live JSI connection. Two connections disagreeing about the journal
is what produced `watermelondb.sqlite: (11) database disk image is malformed`
while adding a server -- the first write to app.db.

The paths provably collide: the patched WMDatabase.getInstance rewrites
`file:///…/files/app.db` to `/…/files/databases/app.db`, which is exactly what
the JS side opens as `${Paths.document.uri}/databases/app.db`.

This was consistent before #10109 because the patch disabled WAL in two
independent places -- the Java flags and the C++ pragma -- so everything was
non-WAL and agreed. #10109 re-enabled only the C++ half. The coherent states are
all-on or all-off; this makes it all-on, so #10109 is kept.

Reverting the two patch hunks rather than editing them leaves upstream's flags
untouched, which is why the diff removes them instead of rewriting them.

Not verified on a device here; it needs an Android build against the WAL-on repro.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* Address review: trim WAL comment and regenerate watermelondb patch

Regenerate @nozbe+watermelondb+0.28.1-0.patch via patch-package after
restoring ENABLE_WRITE_AHEAD_LOGGING in node_modules, instead of
hand-editing the patch. Trim the verbose comment in getDatabaseForServer.

Co-authored-by: yasser khan <attitude3cena.yf@gmail.com>

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
Co-authored-by: Cursor Agent <cursoragent@cursor.com>
2026-09-03 18:06:02 +05:30
..
app fix(android): open every SQLite connection in WAL, not just the JSI one (#10117) 2026-09-03 18:06:02 +05:30
gradle/wrapper Migrate from RNN to Expo router (#9402) 2026-05-13 17:44:55 +08:00
build.gradle Migrate from RNN to Expo router (#9402) 2026-05-13 17:44:55 +08:00
buildscript-gradle.lockfile Migrate from RNN to Expo router (#9402) 2026-05-13 17:44:55 +08:00
gradle.properties fix: app bugs surfaced by Detox RF migration (#9789) 2026-06-09 13:25:42 +05:30
gradlew Migrate from RNN to Expo router (#9402) 2026-05-13 17:44:55 +08:00
gradlew.bat Migrate from RNN to Expo router (#9402) 2026-05-13 17:44:55 +08:00
settings-gradle.lockfile Push react native to 0.73.6 (#7863) 2024-04-22 12:44:39 +02:00
settings.gradle fix: app bugs surfaced by Detox RF migration (#9789) 2026-06-09 13:25:42 +05:30