Skip to content

[WellSQL Migration] Migrate WCProductVariationModel to Room #14027

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

Merged
merged 28 commits into from
May 14, 2025

Conversation

ParaskP7
Copy link
Contributor

@ParaskP7 ParaskP7 commented May 8, 2025

Closes: AINFRA-124

FYI: This is a feature branch of two PRs (context):


Description

This PR migrates WCProductVariationModel to Room.

FYI: For more info see this PR's description as well.


Testing information

Smoke test any feature that uses WCProductVariationModel.

FYI: For more info see this PR's testing information as well.


Images/gif

image

  • I have considered if this change warrants release notes and have added them to RELEASE-NOTES.txt if necessary. Use the "[Internal]" label for non-user-facing changes.

Reviewer (or Author, in the case of optional code reviews):

Please make sure these conditions are met before approving the PR, or request changes if the PR needs improvement:

  • The PR is small and has a clear, single focus, or a valid explanation is provided in the description. If needed, please request to split it into smaller PRs.
  • Ensure Adequate Unit Test Coverage: The changes are reasonably covered by unit tests or an explanation is provided in the PR description.
  • Manual Testing: The author listed all the tests they ran, including smoke tests when needed (e.g., for refactorings). The reviewer confirmed that the PR works as expected on big (tablet) and small (phone) in case of UI changes, and no regressions are added.

wzieba and others added 23 commits April 24, 2025 16:55
`:libs:fluxc-plugin:assembleDebug` passes
…e to app model mapper, make query methods suspendable
To reduce chance of similar mistake in the future, I propose using `LocalId/RemoteId` wrappers.
2 tests are failing right now
…ven`

Room and WellSql have different defaults for sort. In this test, the order doesn't matter as all items have `menuOrder=0`
Don't use incorrect `$original.id` for description: instead, use simple string to avoid any confusion.

Close database on test end
…rn default values

Previously, functions like `VariationRepository#getProductVariationList` or `ProductDetailRepository#getCachedVariationCount` were *not* suspend. This allowed Mockito to return default values for both of the methods, respectively: empty list and 0.

After recent changed, both of this functions were refactored to suspend, which caused Mockito to return `null` for them (see: mockito/mockito-kotlin#342)

This comment brings back the previous behavior by mocking mentioned methods to return default values.
…to_room

[1/2] `WCProductVariationModel` to Room: working app
…to_room_tests

[2/2] `WCProductVariationModel` to Room: unit tests adjustments
@ParaskP7 ParaskP7 added this to the 22.4 milestone May 8, 2025
@ParaskP7 ParaskP7 added status: do not merge Dependent on another PR, ready for review but not ready for merge. type: technical debt Represents or solves tech debt of the project. labels May 8, 2025
@dangermattic
Copy link
Collaborator

dangermattic commented May 8, 2025

2 Warnings
⚠️ This PR is larger than 300 lines of changes. Please consider splitting it into smaller PRs for easier and faster reviews.
⚠️ This PR is assigned to the milestone 22.4. This milestone is due in less than 2 days.
Please make sure to get it merged by then or assign it to a milestone with a later deadline.

Generated by 🚫 Danger

@ParaskP7 ParaskP7 requested a review from Copilot May 8, 2025 12:25
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR migrates the WCProductVariationModel to use Room for persistence and updates related DAOs, database configuration, model mapping, and tests.

  • Introduces a new Room DAO (ProductVariationsDao) and updates WCAndroidDatabase to include WCProductVariationModel.
  • Refactors model creation and mapping methods to use Kotlin’s data class immutability via copy.
  • Updates tests and dependency injections accordingly to support the new Room-based implementation.

Reviewed Changes

Copilot reviewed 27 out of 27 changed files in this pull request and generated no comments.

File Description
libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/persistence/dao/ProductVariationsDao.kt New Room DAO for WCProductVariationModel providing CRUD operations.
libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/persistence/WCAndroidDatabase.kt Updates database version and schema to include WCProductVariationModel.
libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/model/WCProductVariationModel.kt Migrates the variation model to a Room entity using immutable data class copy.
Various files in network, repository, and tests Updates existing mapping, repository functions, and tests to work with the new immutable model.
Comments suppressed due to low confidence (2)

WooCommerce/src/main/kotlin/com/woocommerce/android/ui/products/details/ProductDetailCardBuilder.kt:522

  • Using runBlocking here can block the thread and potentially affect UI responsiveness. Consider refactoring this code to employ a fully suspendable or asynchronous approach to avoid potential deadlocks.
runBlocking { variationRepository.getProductVariationList(product.remoteId).all { ... } }

WooCommerce/src/test/kotlin/com/woocommerce/android/ui/products/ProductTestUtils.kt:142

  • [nitpick] Ensure that the toAppModel() conversion retains all required fields and that the hardcoded currency value is appropriate across different locales. Review the conversion logic for consistency with updated Room model semantics.
return WCProductVariationModel(2).copy(...).toAppModel().also { it.priceWithCurrency = "$10.00" }

@wpmobilebot
Copy link
Collaborator

wpmobilebot commented May 8, 2025

📲 You can test the changes from this Pull Request in WooCommerce-Wear Android by scanning the QR code below to install the corresponding build.
App Name WooCommerce-Wear Android
Platform⌚️ Wear OS
FlavorJalapeno
Build TypeDebug
Commit7d02c49
Direct Downloadwoocommerce-wear-prototype-build-pr14027-7d02c49.apk

@codecov-commenter
Copy link

codecov-commenter commented May 8, 2025

Codecov Report

Attention: Patch coverage is 20.00000% with 200 lines in your changes missing coverage. Please review.

Project coverage is 38.34%. Comparing base (e688aaa) to head (2d9650b).

Files with missing lines Patch % Lines
.../com/woocommerce/android/model/ProductVariation.kt 3.84% 50 Missing ⚠️
...rg/wordpress/android/fluxc/store/WCProductStore.kt 0.00% 48 Missing ⚠️
...st/wpcom/wc/product/ProductVariationApiResponse.kt 0.00% 47 Missing ⚠️
...network/rest/wpcom/wc/product/ProductRestClient.kt 0.00% 38 Missing ⚠️
...id/ui/products/details/ProductDetailCardBuilder.kt 16.66% 5 Missing ⚠️
...rdpress/android/fluxc/persistence/WellSqlConfig.kt 0.00% 4 Missing ⚠️
...erce/android/model/SubscriptionProductVariation.kt 0.00% 2 Missing ⚠️
...rk/rest/wpcom/wc/product/ProductVariationMapper.kt 0.00% 2 Missing ⚠️
...roducts/details/ProductDetailBottomSheetBuilder.kt 0.00% 1 Missing ⚠️
...roid/ui/products/variations/VariationRepository.kt 0.00% 1 Missing ⚠️
... and 2 more
Additional details and impacted files
@@             Coverage Diff              @@
##              trunk   #14027      +/-   ##
============================================
+ Coverage     38.32%   38.34%   +0.02%     
- Complexity     9546     9566      +20     
============================================
  Files          2123     2124       +1     
  Lines        116926   116874      -52     
  Branches      15009    15003       -6     
============================================
+ Hits          44810    44817       +7     
+ Misses        68004    67944      -60     
- Partials       4112     4113       +1     

☔ View full report in Codecov by Sentry.
📢 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.

ParaskP7 added 3 commits May 8, 2025 16:06
It looks like as part of 1df49aa an
oversight occurred where, instead of 'result.remoteProductId',
'payload.remoteProductId' got used.

I am not sure if that was deliberate or not, and since there was no
specific comment about that, it is better to treat that as an oversight.
This commit serves as an explicit nudge to reviewers, and the original
auhtor, to verify this change with that extra 'fix' commit.
This follows the same pattern applied to 'WCProductModel' and it seems
that this pattern is the one mostly used. This pattern is similar to
using a 'value' class to create a more domain-specific type instead of
primitive types to help with data class construction clarity.

Also, related to: 316ef5f
@ParaskP7 ParaskP7 removed the status: do not merge Dependent on another PR, ready for review but not ready for merge. label May 8, 2025
@ParaskP7 ParaskP7 marked this pull request as ready for review May 8, 2025 18:48
@ParaskP7
Copy link
Contributor Author

ParaskP7 commented May 8, 2025

👋 @malinajirka , I am adding you as an extra reviewer to this, additional to myself, me taking this to the finish line for @wzieba (who is currently AFK). I am doing that because I want someone from the product team to do a final review on this, test this as thoroughly as possible and signing this change for merging.

With that, I want to give you some extra hints on where I would love you to put some extra focus:

  1. This 9f715a4 commit and more specifically the new primaryKeys constraint (see comment). 👀
  2. This 9d12e9d commit and more specifically the new attributesToJson() function (see discussion). 👀
  3. This extra a440b6b commit (of mine) that is supposed to be fixing an oversight. 🤔
  4. This extra 375d22a commit (of mine) that refactors the entity to utilize LocalId/RemoteId in favor of Int/Long.
    • PS: I am actually wondering what's the best practice you all follow atm on this. 🤔
  5. This testing related comment of mine and whether this problem happens on your side too. 🧪

💭 Last but not least, and maybe more importantly, I wanted to clarify the behavior of this migration. I want to clarify that with this kind of migrations we are okay dropping an existing table (wp-fluxc.WCProductVariationModel), along with its entries, and recreating the table (wc-android-database.ProductVariationEntity), but without any entries (empty). With such a migration, all previous table entries will be gone and added back (partially or in full) only when a user navigates to that specific screen.

  • Firstly, I guess that since [1/2] WCProductModel to Room: working app #13935 has been already merged, this is an acceptable migration, and actually, maybe the only way to do such a migration.
  • Nevertheless, I just want to take this opportunity to clarify this, and furthermore, to ask if you can foresee any trouble with such a migration (me thinking of online/offline sync, etc).

Once more, thank you so much in advance for all the help and insights on this! 🙏

@ParaskP7 ParaskP7 requested a review from malinajirka May 8, 2025 19:18
@malinajirka
Copy link
Contributor

Thanks for working on this @ParaskP7!

I'll also invite @hichamboushaba and @kidinov since they reviewed the previous PRs so likely know what to look for. Moreover, I'm wrapping up a project with a target date set to next friday and I'm a bit behind, so it might take me a bit before I find time for this review (I'd estimate it to Wednesday the earliest).

@ParaskP7
Copy link
Contributor Author

ParaskP7 commented May 9, 2025

Thanks for all the help @malinajirka , the heads-up on this and for inviting others to review this, much appreciated! 🙇

@kidinov kidinov removed their request for review May 9, 2025 09:07
@ParaskP7
Copy link
Contributor Author

👋 team (@malinajirka @hichamboushaba @kidinov), just a friendly ping as a reminder on this, thank you! 🙏

@malinajirka malinajirka self-assigned this May 14, 2025
@malinajirka
Copy link
Contributor

This extra a440b6b commit (of mine) that is supposed to be fixing an oversight. 🤔

I think this change is fine, but just to double-check my understanding. I believe the remoteProductId between payload and result is always identical, right? Or have you fixed it based on some bug you discovered?

This extra 375d22a commit (of mine) that refactors the entity to utilize LocalId/RemoteId in favor of Int/Long.
PS: I am actually wondering what's the best practice you all follow atm on this. 🤔

Thanks, that's a nice improvement! The wrappers were introduced as part of the introduction of List Management component in WPAndroid (for posts) and later in WCAndroid (for orders). I'm not aware of any guidelines/best-practices regarding the usage - for models that are using the list management component, this is required, for other models, it's nice to have.

This testing related #13982 (comment) of mine and whether this problem happens on your side too. 🧪

I can confirm the byteBuddy exception is happening on my side as well 🤔. Having said that, it's happening even when I run the command on trunk, so I don't think it's related this PR.

💭 Last but not least, and maybe more importantly, I wanted to clarify the behavior of this migration. I want to clarify that with this kind of migrations we are okay dropping an existing table (wp-fluxc.WCProductVariationModel), along with its entries, and recreating the table (wc-android-database.ProductVariationEntity), but without any entries (empty). With such a migration, all previous table entries will be gone and added back (partially or in full) only when a user navigates to that specific screen.

Editing products and variations is supported in the app, but we do not support offline => the user needs to explicitly save the changes while being online => AFAIK it's fine to recreate the table.


I have tested various flows in the app (variable product creation, editing, order creation with variations in the cart, order detail with variations in the cart, POS) and it all seems to be working as expected. I also tested "migration" - dropping the database after already having some data in there, and I also didn't discover any issues. (I discovered an unrelated issue on Attributes subscreen and reported it to Linear)

Great job Wojtek and Petros! Thank you for the super clear description and heads-up about changes that might have a higher potential of introducing regressions! 🎖️ 🙇

@ParaskP7
Copy link
Contributor Author

Awesome, thanks for this review and all the extra context you provided as part of it @malinajirka , much appreciated! 🙇 ❤️ 🚀

I think this change is fine, but just to double-check my understanding. I believe the remoteProductId between payload and result is always identical, right? Or have you fixed it based on some bug you discovered?

About the remoteProductId between payload and result being identical, I guess so, but I didn't want to introduce a code change, thus I reverted the change as a "code" fix. 😊

FYI: I (indeed) didn't discover any bug.

Thanks, that's a nice improvement! The wrappers were introduced as part of the introduction of List Management component in WPAndroid (for posts) and later in WCAndroid (for orders). I'm not aware of any guidelines/best-practices regarding the usage - for models that are using the list management component, this is required, for other models, it's nice to have.

Interesting, good to know, didn't have (or forgot) this knowledge about the list management component and all. 🔖

I can confirm the byteBuddy exception is happening on my side as well 🤔. Having said that, it's happening even when I run the command on trunk, so I don't think it's related this PR.

Yea, I have the feeling this all started when #13975 merged to trunk 3 weeks ago.

Editing products and variations is supported in the app, but we do not support offline => the user needs to explicitly save the changes while being online => AFAIK it's fine to recreate the table.

Good to know! 👍

Btw, of all the "legacy" WellSQL tables we still have to migrate, do you maybe have any idea if any such table would require a more careful migration, like migrating its data too (due to offline support, etc)? 🤔


I have tested various flows in the app (variable product creation, editing, order creation with variations in the cart, order detail with variations in the cart, POS) and it all seems to be working as expected. I also tested "migration" - dropping the database after already having some data in there, and I also didn't discover any issues. (I discovered an unrelated issue on Attributes subscreen and reported it to Linear)

Thanks for all the testing! 🙇 🧪 🎉

Great job Wojtek and Petros! Thank you for the super clear description and heads-up about changes that might have a higher potential of introducing regressions! 🎖️ 🙇

❤️

@ParaskP7
Copy link
Contributor Author

👋 @wzieba and just an FYI that I just updated this branch with the latest change from trunk. If CI ✅ then we should be ready to merge this. 🚀

Now that you're back, let me know if you want to take one last time at the changes before merging, or want to take it over from here. 🙏

@malinajirka
Copy link
Contributor

malinajirka commented May 14, 2025

Btw, of all the "legacy" WellSQL tables we still have to migrate, do you maybe have any idea if any such table would require a more careful migration, like migrating its data too (due to offline support, etc)? 🤔

AFAIK, all our tables are cache only, so they should all be safe to drop - I asked for double confirmation here p1747214664229459-slack-C6H8C3G23.

Yea, I have the feeling this all started when #13975 merged to trunk 3 weeks ago.

Ohh, I see 🤔.

@ParaskP7
Copy link
Contributor Author

AFAIK, all our tables are cache only, so they should all be safe to drop.

Good to know @malinajirka , thanks once more! 🥇

@wzieba
Copy link
Contributor

wzieba commented May 14, 2025

Now that you're back, let me know if you want to take one last time at the changes before merging, or want to take it over from here. 🙏

All fine on my end, thank you for applying improvements!

@malinajirka
Copy link
Contributor

@ParaskP7 One more thing regarding if it's safe to drop some tables. @wzieba made a good point that SiteModel might potentially cause troubles and I think AccountModel + WCUserModel might as well.

@ParaskP7
Copy link
Contributor Author

@ParaskP7 One more thing regarding if it's safe to drop some tables. @wzieba made a good point that SiteModel might potentially cause troubles and I think AccountModel + WCUserModel might as well.

Noted @malinajirka , thanks! 🔖

PS: In the meanwhile, if you think of any other tables that could potentially cause troubles, let us know! 🙏

@ParaskP7 ParaskP7 merged commit fe58200 into trunk May 14, 2025
17 checks passed
@ParaskP7 ParaskP7 deleted the feature/product_variation_to_room branch May 14, 2025 10:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: technical debt Represents or solves tech debt of the project.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants