From f411b35f765c2d187824d7a8f1283489e058b697 Mon Sep 17 00:00:00 2001 From: Rowan Seymour Date: Fri, 17 May 2024 08:12:46 -0500 Subject: [PATCH] Fix migrating directly from 13.2 to 13.5 --- flows/definition/migrations/base.go | 20 +++--- flows/definition/migrations/base_test.go | 85 ++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 9 deletions(-) diff --git a/flows/definition/migrations/base.go b/flows/definition/migrations/base.go index 0f832fcd5..68acd9540 100644 --- a/flows/definition/migrations/base.go +++ b/flows/definition/migrations/base.go @@ -94,22 +94,24 @@ func migrate(data []byte, from *semver.Version, to *semver.Version, cfg *Config) // sorted by earliest first sort.SliceStable(versions, func(i, j int) bool { return versions[i].LessThan(versions[j]) }) - migrated, err := ReadFlow(data) - if err != nil { - return nil, err - } - for _, version := range versions { - migrated, err = registered[version](migrated, cfg) + // we read the flow each time to ensure what we pass to the migration function uses the types it expects + flow, err := ReadFlow(data) + if err != nil { + return nil, err + } + + flow, err = registered[version](flow, cfg) if err != nil { return nil, errors.Wrapf(err, "unable to migrate to version %s", version.String()) } - migrated["spec_version"] = version.String() + flow["spec_version"] = version.String() + + data = jsonx.MustMarshal(flow) } - // finally marshal back to JSON - return jsonx.Marshal(migrated) + return data, nil } // Clone clones the given flow definition by replacing all UUIDs using the provided mapping and diff --git a/flows/definition/migrations/base_test.go b/flows/definition/migrations/base_test.go index af866a340..d62e736b4 100644 --- a/flows/definition/migrations/base_test.go +++ b/flows/definition/migrations/base_test.go @@ -237,3 +237,88 @@ func TestCloneOlderVersion(t *testing.T) { "nodes": [] }`), cloneJSON, "cloned flow mismatch") } + +func TestMultiVersionMigration(t *testing.T) { + migrated, err := migrations.MigrateToLatest([]byte(`{ + "uuid": "e91308d7-7c80-4b0b-9840-7a3484158c8e", + "name": "Test", + "spec_version": "13.2.0", + "type": "messaging", + "revision": 0, + "expire_after_minutes": 1440, + "language": "eng", + "nodes": [ + { + "actions": [ + { + "attachments": [], + "quick_replies": [ + "1", + "2" + ], + "templating": { + "template": { + "name": "daily_interaction", + "uuid": "b4533c2d-d00d-4294-8f82-4027ed4c2b96" + }, + "uuid": "656b5c50-7c71-4257-9db1-2fa9a3deb84d", + "variables": [ + "@results.name" + ] + }, + "text": "BLAAAH", + "type": "send_msg", + "uuid": "929932aa-8414-4458-9504-f60e42395ca2" + } + ], + "exits": [ + { + "destination_uuid": "45091f3b-1b8a-4ae5-81eb-11426339e864", + "uuid": "cdc71a39-6429-4edc-8439-d956084e5581" + } + ], + "uuid": "12d205d2-4697-411a-9ec4-818ae4471598" + } + ] + }`), migrations.DefaultConfig) + require.NoError(t, err) + + expected := fmt.Sprintf(`{ + "uuid": "e91308d7-7c80-4b0b-9840-7a3484158c8e", + "name": "Test", + "spec_version": "%s", + "type": "messaging", + "revision": 0, + "expire_after_minutes": 1440, + "language": "eng", + "nodes": [ + { + "actions": [ + { + "attachments": [], + "quick_replies": [ + "1", + "2" + ], + "template": { + "name": "daily_interaction", + "uuid": "b4533c2d-d00d-4294-8f82-4027ed4c2b96" + }, + "template_variables": ["@results.name"], + "text": "BLAAAH", + "type": "send_msg", + "uuid": "929932aa-8414-4458-9504-f60e42395ca2" + } + ], + "exits": [ + { + "destination_uuid": "45091f3b-1b8a-4ae5-81eb-11426339e864", + "uuid": "cdc71a39-6429-4edc-8439-d956084e5581" + } + ], + "uuid": "12d205d2-4697-411a-9ec4-818ae4471598" + } + ] + }`, definition.CurrentSpecVersion) + test.AssertEqualJSON(t, []byte(expected), migrated, "flow migration mismatch") +}