Page MenuHomePhabricator

[lib] Update DB version while running a migration
ClosedPublic

Authored by tomek on May 8 2024, 5:13 AM.
Tags
None
Referenced Files
F3513451: D11937.diff
Sun, Dec 22, 12:24 AM
F3513062: D11937.id40944.diff
Sat, Dec 21, 10:02 PM
F3513061: D11937.id40855.diff
Sat, Dec 21, 10:02 PM
F3513060: D11937.id40221.diff
Sat, Dec 21, 10:02 PM
F3513059: D11937.id39926.diff
Sat, Dec 21, 10:02 PM
F3513048: D11937.id.diff
Sat, Dec 21, 10:01 PM
F3513042: D11937.diff
Sat, Dec 21, 10:01 PM
Unknown Object (File)
Nov 18 2024, 9:41 PM
Subscribers

Details

Summary

As a part of new migrations / backup approach, we have to store a version in SQLite. Updating this version should happen in the same transaction as all the other ops. In this new approach, we're introducing a new migrations spec - the functions return not only the state but also the ops that should be applied on the DB. Running the ops becomes a responsibility of createAsyncMigrate, which also adds one op that updates the DB version. From this point we shouldn't add any legacy migration. Also, store versions should match between the platforms.

https://linear.app/comm/issue/ENG-7006/back-up-redux-version

Depends on D11936

Test Plan

Checked on both web and native:
created one legacy and one new migration, checked if both of them were run and that the DB version was updated in the store (killed and reopened the app, and then checked syncedMetadataStore).

Diff Detail

Repository
rCOMM Comm
Branch
transactions
Lint
No Lint Coverage
Unit
No Test Coverage

Event Timeline

tomek requested review of this revision.May 8 2024, 5:30 AM
native/redux/persist.js
1305–1308

Can you write a comment explaining what this is for?

web/redux/persist.js
526

Why is this not 18?

web/redux/persist.js
526

This is explained in ENG-6904

Restoration from an older backup (...) We will need to run Redux Persist migrations after restoration (...) We also need to store redux persist version to know where to start

I talked privately about this diff with @tomek and from what I can gather this diff correctly does what it is supposed to do so there is no reason not to accept it. However now there are two points that developers must be aware of:

  • Since this diff every SQLite migration altering db schema will need to be exposed as JSI that will be awaited inside migration lambda independently of ops. There are numerous ways to make it work. We can:
    • create plain JSI calls in CommCoreModule for every SQLite migration.
    • create separate TurboModule for migration JSI's
    • create one JSI in CommCoreModule that would take a number and mapping from number to C++ lambda directly inside SQLiteQueryExecutor. This one is probably the best as it is easily portable to web.
  • Since this diff every migration must be present on both native and web. Even if migration doesn't do anything on web (it is native specific) we have to create empty mock migration in the same way @tomek does in this diff.
This revision is now accepted and ready to land.May 9 2024, 2:49 AM
kamil requested changes to this revision.May 10 2024, 6:29 AM

Looks good, one question about error handling

lib/shared/create-async-migrate.js
103

nit: feels like toString() is more suitable

web/redux/persist.js
507–510

This is just a mock but it still processing store ops to bump verion, shouldn't we somehow use handleReduxMigrationFailure here?

Maybe there is a way to make it part of the spec so developers will not have to worry about failures each time adding migration?

This revision now requires changes to proceed.May 10 2024, 6:29 AM
tomek marked 3 inline comments as done.

Handle migration errors. This required a lot of changes in types.

lib/types/redux-types.js
179 ↗(On Diff #40221)

This prop is present in both web and native AppState and putting it here simplifies createAsyncMigrate function significantly. Haven't found any downside of putting it here.

web/redux/persist.js
507–510

It's a little more than a mock - we use this migration to bump the version.

Maybe there is a way to make it part of the spec so developers will not have to worry about failures each time adding migration?

Yeah, makes sense. Going to add the error handling.

This revision is now accepted and ready to land.May 16 2024, 10:08 AM