Extract ConnectDatabaseSchemaManager And Add Connect DB Test Hook#3789
Conversation
…tern Mirrors AppDatabaseSchemaManager: schema version and table creation live in a dedicated manager class, keeping DatabaseConnectOpenHelper thin (open/upgrade only). - ConnectDatabaseSchemaManager: holds DB_VERSION_CONNECT (public) and initializeSchema(IDatabase), replacing the inline onCreate logic - DatabaseConnectOpenHelper.onCreate: delegates to schema manager; all model imports removed Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Introduces a virtual getConnectDbOpenHelper(Context) method on CommCareApplication so CommCareTestApplication can override it to return an in-memory unencrypted DB, removing the need to statically mock ConnectDatabaseHelper in unit tests. - CommCareApplication: adds getConnectDbOpenHelper, encapsulating the passphrase lookup and EncryptedDatabaseAdapter construction - ConnectDatabaseHelper: delegates DB open to the hook instead of constructing directly - DatabaseConnectOpenHelperMock: in-memory SQLiteOpenHelper backed by ConnectDatabaseSchemaManager for test schema parity - CommCareTestApplication: overrides the hook to return an in-memory UnencryptedDatabaseAdapter backed by DatabaseConnectOpenHelperMock Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #3789 +/- ##
============================================
+ Coverage 25.86% 25.95% +0.08%
- Complexity 4393 4406 +13
============================================
Files 951 953 +2
Lines 57222 57334 +112
Branches 6813 6823 +10
============================================
+ Hits 14803 14879 +76
- Misses 40589 40623 +34
- Partials 1830 1832 +2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
📝 WalkthroughWalkthroughThis PR centralizes creation of the Connect database open helper. A new Estimated code review effort: 3 (Moderate) | ~25 minutes Sequence Diagram(s)sequenceDiagram
participant ConnectDatabaseHelper
participant CommCareApplication
participant ConnectDatabaseUtils
participant UserSandboxUtils
participant DatabaseConnectOpenHelper
participant ConnectDatabaseSchemaManager
ConnectDatabaseHelper->>CommCareApplication: getConnectDbOpenHelper(context)
CommCareApplication->>ConnectDatabaseUtils: getConnectDbPassphrase()
ConnectDatabaseUtils-->>CommCareApplication: passphrase
CommCareApplication->>CommCareApplication: validate passphrase (throw if empty)
CommCareApplication->>UserSandboxUtils: derive SQLCipher key(passphrase)
UserSandboxUtils-->>CommCareApplication: key
CommCareApplication->>DatabaseConnectOpenHelper: create with key
DatabaseConnectOpenHelper->>ConnectDatabaseSchemaManager: initializeSchema(adapter) [on create]
ConnectDatabaseSchemaManager-->>DatabaseConnectOpenHelper: tables created
DatabaseConnectOpenHelper-->>CommCareApplication: EncryptedDatabaseAdapter
CommCareApplication-->>ConnectDatabaseHelper: IDatabase handle
Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
app/unit-tests/src/org/commcare/CommCareTestApplication.java (1)
344-348: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueUnused
contextparameter.
getConnectDbOpenHelperreceives acontextparameter but constructsDatabaseConnectOpenHelperMock(this)instead, ignoring the passed-in context. SinceCommCareTestApplicationextendsContext, this works today, but it silently diverges from the parameter contract and could mask bugs if the caller ever passes a different context (e.g., an Activity context) expecting it to be used.♻️ Suggested fix
`@Override` public IDatabase getConnectDbOpenHelper(Context context) { - return new UnencryptedDatabaseAdapter(new DatabaseConnectOpenHelperMock(this)); + return new UnencryptedDatabaseAdapter(new DatabaseConnectOpenHelperMock(context)); }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@app/unit-tests/src/org/commcare/CommCareTestApplication.java` around lines 344 - 348, `getConnectDbOpenHelper` is ignoring its `context` argument and always using `this`, which diverges from the method contract. Update `CommCareTestApplication.getConnectDbOpenHelper(Context)` to pass the provided context through to `DatabaseConnectOpenHelperMock` (and the `UnencryptedDatabaseAdapter` path if needed) so the implementation matches the parameter and does not hide caller-specific context behavior.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@app/unit-tests/src/org/commcare/CommCareTestApplication.java`:
- Around line 344-348: `getConnectDbOpenHelper` is ignoring its `context`
argument and always using `this`, which diverges from the method contract.
Update `CommCareTestApplication.getConnectDbOpenHelper(Context)` to pass the
provided context through to `DatabaseConnectOpenHelperMock` (and the
`UnencryptedDatabaseAdapter` path if needed) so the implementation matches the
parameter and does not hide caller-specific context behavior.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: abfde014-9ae8-4ef8-9d98-8efbf53c13e8
📒 Files selected for processing (6)
app/src/org/commcare/CommCareApplication.javaapp/src/org/commcare/connect/database/ConnectDatabaseHelper.javaapp/src/org/commcare/models/database/connect/ConnectDatabaseSchemaManager.ktapp/src/org/commcare/models/database/connect/DatabaseConnectOpenHelper.javaapp/unit-tests/src/org/commcare/CommCareTestApplication.javaapp/unit-tests/src/org/commcare/models/database/connect/DatabaseConnectOpenHelperMock.kt
Product Description
No user-visible changes. This is a pure internal infrastructure refactor.
Technical Summary
Mirrors the pattern used by
AppDatabaseSchemaManager/DatabaseGlobalOpenHelperfor the Connect DB.This became a blocker for the other PR I am working on, as most tests started failing when trying to access the `getConnectStorage" method as a result of new task parsing code. The 2 approaches I had to resolve it is to mock the DB for every test or to provide a test app wide mock. I chose the later and it also helps ConnectDB aligns better with the pattern we follow for other databases in the app. This will also remove any need of mocking the DB in unit tests getting us closer to more integrated testing.
Safety Assurance
Safety story
What gives confidence:
Automated test coverage
CommCareTestApplication.getConnectDbOpenHelperis exercised by any Robolectric test that opens the Connect DB. The schema manager is validated indirectly by those tests succeeding against the in-memory DB.Review 🐟 by 🐟