Skip to content

Extract ConnectDatabaseSchemaManager And Add Connect DB Test Hook#3789

Merged
shubham1g5 merged 2 commits into
masterfrom
connect_db_wiring_change
Jul 6, 2026
Merged

Extract ConnectDatabaseSchemaManager And Add Connect DB Test Hook#3789
shubham1g5 merged 2 commits into
masterfrom
connect_db_wiring_change

Conversation

@shubham1g5

@shubham1g5 shubham1g5 commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Product Description

No user-visible changes. This is a pure internal infrastructure refactor.

Technical Summary

Mirrors the pattern used by AppDatabaseSchemaManager / DatabaseGlobalOpenHelper for 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:

  • Correctness relies entirely on the refactor being a structural rename with no logic changes
  • I would imagine the set of unit tests plus integrations tests is siffucient to flag any issues caused by the refactor here.
  • Verified basic functionality like Personal ID signup and Opportunity screens locally

Automated test coverage

CommCareTestApplication.getConnectDbOpenHelper is 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 🐟

shubham1g5 and others added 2 commits July 3, 2026 18:18
…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>
@shubham1g5 shubham1g5 self-assigned this Jul 3, 2026
@codecov

codecov Bot commented Jul 3, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 25.95%. Comparing base (392c0b1) to head (09f6484).
⚠️ Report is 164 commits behind head on master.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@coderabbitai

coderabbitai Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

This PR centralizes creation of the Connect database open helper. A new getConnectDbOpenHelper(Context) method is added to CommCareApplication (production, encrypted via SQLCipher) and CommCareTestApplication (test mock, unencrypted). Connect DB table/schema creation logic is extracted from DatabaseConnectOpenHelper into a new ConnectDatabaseSchemaManager Kotlin object exposing DB_NAME, DB_VERSION_CONNECT, and initializeSchema. ConnectDatabaseHelper now delegates to CommCareApplication's new method instead of manually handling the passphrase and adapter. A new DatabaseConnectOpenHelperMock supports test schema initialization.

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
Loading

Possibly related PRs

  • dimagi/commcare-android#3043: Overlaps with centralized Connect DB versioning and onCreate initialization changes to DatabaseConnectOpenHelper/schema creation.
  • dimagi/commcare-android#3121: Both touch the Connect DB open/error-handling path in ConnectDatabaseHelper around passphrase and database opening.

Suggested reviewers: OrangeAndGreen

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly reflects the main refactor: extracting the Connect schema manager and adding a Connect DB test hook.
Description check ✅ Passed The description covers the required product, technical, safety story, and automated test coverage sections; only the labels/review checklist is missing.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch connect_db_wiring_change

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
app/unit-tests/src/org/commcare/CommCareTestApplication.java (1)

344-348: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Unused context parameter.

getConnectDbOpenHelper receives a context parameter but constructs DatabaseConnectOpenHelperMock(this) instead, ignoring the passed-in context. Since CommCareTestApplication extends Context, 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

📥 Commits

Reviewing files that changed from the base of the PR and between beb94f9 and 09f6484.

📒 Files selected for processing (6)
  • app/src/org/commcare/CommCareApplication.java
  • app/src/org/commcare/connect/database/ConnectDatabaseHelper.java
  • app/src/org/commcare/models/database/connect/ConnectDatabaseSchemaManager.kt
  • app/src/org/commcare/models/database/connect/DatabaseConnectOpenHelper.java
  • app/unit-tests/src/org/commcare/CommCareTestApplication.java
  • app/unit-tests/src/org/commcare/models/database/connect/DatabaseConnectOpenHelperMock.kt

@shubham1g5 shubham1g5 marked this pull request as ready for review July 3, 2026 13:09
@shubham1g5 shubham1g5 added the skip-integration-tests Skip android tests. label Jul 6, 2026
@shubham1g5 shubham1g5 merged commit 1558afc into master Jul 6, 2026
42 of 44 checks passed
@shubham1g5 shubham1g5 deleted the connect_db_wiring_change branch July 6, 2026 04:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

skip-integration-tests Skip android tests.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants