Skip to content

Commit

Permalink
Merge pull request #1255 from nyaruka/migration_fix
Browse files Browse the repository at this point in the history
Fix migrating directly from 13.2 to 13.5
  • Loading branch information
rowanseymour committed May 17, 2024
2 parents afa7527 + f411b35 commit b593277
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 9 deletions.
20 changes: 11 additions & 9 deletions flows/definition/migrations/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
85 changes: 85 additions & 0 deletions flows/definition/migrations/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

0 comments on commit b593277

Please sign in to comment.