HomePhabricator
Diffusion Comm 31b35e4732a6

[lib] Generate converters from validators

Description

[lib] Generate converters from validators

Summary:
https://linear.app/comm/issue/ENG-3995/create-functions-for-migrating-the-client-stores

We need to migrate client db stores, and redux persist. We can't use validators themselves because they may be changed in the future. This diffs adds a script that takes a validator and generated a function (as a string containing js code) that converts the type described by this validator. This way the migration code will remain static even if the user updates the app. The generated function are kept in lib/_generated/migration-utils.js. The only manual change (other than pasting the generating functions) in this file is adding the required imports.

Example of the generated code for the linkStore:

export function convertInviteLinksStoreToNewIDSchema(
  input: InviteLinksStore,
): InviteLinksStore {
  return {
    ...input,
    links: Object.fromEntries(
      entries(input.links).map(([key, value]) => [
        '256|' + key,
        {
          ...value,
          primaryLink:
            value.primaryLink !== null && value.primaryLink !== undefined
              ? {
                  ...value.primaryLink,
                  role: '256|' + value.primaryLink.role,
                  communityID: '256|' + value.primaryLink.communityID,
                }
              : value.primaryLink,
        },
      ]),
    ),
  };
}

Test Plan: Tested with the rest of the diffs, by running schema id migrations on clients using the generated functions.

Reviewers: kamil, tomek, ashoat

Reviewed By: tomek

Subscribers: bartek, atul, ashoat

Differential Revision: https://phab.comm.dev/D8355

Details

Provenance
Michal Gniadek <michal.gniadek@swmansion.com>Authored on Jun 27 2023, 8:56 AM
Reviewer
tomek
Differential Revision
D8355: [lib] Generate converters from validators
Parents
rCOMM041958e44a76: [keyserver] Remove target blank
Branches
Unknown
Tags
Unknown

Event Timeline

Michal Gniadek <michal.gniadek@swmansion.com> committed rCOMM31b35e4732a6: [lib] Generate converters from validators (authored by Michal Gniadek <michal.gniadek@swmansion.com>).Jul 13 2023, 2:16 AM