Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FhirEngine interface method 'withTransaction' #2535

Open
wants to merge 20 commits into
base: master
Choose a base branch
from

Conversation

LZRS
Copy link
Collaborator

@LZRS LZRS commented May 6, 2024

IMPORTANT: All PRs must be linked to an issue (except for extremely trivial and straightforward changes).

Fixes #2531

Description
Support performing FhirEngine actions as a single atomic transaction

Alternative(s) considered
Have you considered any alternatives? And if so, why have you chosen the approach in this PR?

Type
Choose one: (Bug fix | Feature | Documentation | Testing | Code health | Builds | Releases | Other)

Screenshots (if applicable)

Checklist

  • I have read and acknowledged the Code of conduct.
  • I have read the Contributing page.
  • I have signed the Google Individual CLA, or I am covered by my company's Corporate CLA.
  • I have discussed my proposed solution with code owners in the linked issue(s) and we have agreed upon the general approach.
  • I have run ./gradlew spotlessApply and ./gradlew spotlessCheck to check my code follows the style guide of this project.
  • I have run ./gradlew check and ./gradlew connectedCheck to test my changes locally.
  • I have built and run the demo app(s) to verify my change fixes the issue and/or does not break the demo app(s).

To support performing FhirEngine actions as a single atomic transaction
@LZRS LZRS requested review from santosh-pingle and a team as code owners May 6, 2024 21:40
@LZRS LZRS requested a review from shelaghm May 6, 2024 21:40
Copy link
Collaborator

@aditya-07 aditya-07 left a comment

Choose a reason for hiding this comment

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

Left some suggestions for the api.

@LZRS LZRS force-pushed the 2531-fhirengine-interface branch from 13b83cb to bf90f8c Compare May 26, 2024 10:35
@LZRS LZRS force-pushed the 2531-fhirengine-interface branch from bf90f8c to 72be126 Compare May 26, 2024 10:44
@LZRS LZRS requested a review from aditya-07 May 27, 2024 09:26
LZRS added a commit to opensrp/android-fhir that referenced this pull request Jun 26, 2024
LZRS added a commit to opensrp/android-fhir that referenced this pull request Jul 2, 2024
Copy link
Collaborator

@MJ1998 MJ1998 left a comment

Choose a reason for hiding this comment

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

LGTM. @jingtang10

@LZRS LZRS requested a review from a team as a code owner August 22, 2024 07:26
@MJ1998
Copy link
Collaborator

MJ1998 commented Sep 5, 2024

Note: Sync APIs are deprecated now and once removed we probably dont need the CRUDEngine interface.

@jingtang10
Copy link
Collaborator

happy to merge once jajoo's comment's resolved.

discussed with @aditya-07 who's also ok with this.

ndegwamartin added a commit to opensrp/android-fhir that referenced this pull request Sep 10, 2024
FORK
         - With unmerged PR #9
            - WUP  #13

SDK
            - WUP google#2178
            - WUP google#2650
            - WUP google#2663
PERF
- WUP google#2669
- WUP google#2565
- WUP google#2561
- WUP google#2535
ndegwamartin added a commit to opensrp/android-fhir that referenced this pull request Oct 2, 2024
    FORK
             - With unmerged PR #9
                - WUP  #13

    SDK
                - WUP google#2178
                - WUP google#2650
                - WUP google#2663
    PERF
    - WUP google#2669
    - WUP google#2565
    - WUP google#2561
    - WUP google#2535
@LZRS LZRS force-pushed the 2531-fhirengine-interface branch from 3af9d14 to b520d30 Compare October 5, 2024 11:22
@LZRS LZRS requested a review from MJ1998 October 8, 2024 09:38
Copy link
Collaborator

@jingtang10 jingtang10 left a comment

Choose a reason for hiding this comment

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

Thanks @LZRS!

@@ -97,6 +97,10 @@ internal class FhirEngineImpl(private val database: Database, private val contex
}
}

override suspend fun withTransaction(block: suspend FhirEngine.() -> Unit) {
database.withTransaction { block.invoke(this@FhirEngineImpl) }
Copy link
Collaborator

Choose a reason for hiding this comment

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

does this work?

Suggested change
database.withTransaction { block.invoke(this@FhirEngineImpl) }
database.withTransaction { this.block() }

@@ -783,6 +788,132 @@ class FhirEngineImplTest {
assertThat(services.database.getLocalChangesCount()).isEqualTo(0)
}

@Test
fun `withTransaction saves all changes successfully in order`() = runTest {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you simplify this test case?

I think you should keep this test case to use at most 2 resources. For example a patient and an observation. this is because when you add more resources to reference each other it's not really testing anything new.

Even just create a patient and then an observation that references the patient in my opinion is enough.

The point of the transation API isn't that things happen in order.... things will be executed in order without transation. The point is really that a transaction is atomic and will be reverted as a whole. So this test case should just be simply testing that the transation api runs multiple operations. the next test case is more important.

}

@Test
fun `withTransaction reverts all changes when an error occurs`() = runTest {
Copy link
Collaborator

Choose a reason for hiding this comment

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

this test case can be done using 2 resources.

before transation:
create a patient

in a transation:
create an encoutner for the patient, update the encouter to reference a non-existent patient.

check that the encounter is also rolled back.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: PR under Review
Development

Successfully merging this pull request may close these issues.

Expose an interface to db.withtransaction through FhirEngine
4 participants