Skip to content

Commit

Permalink
FIX: Make links and icons setting migration more resilient
Browse files Browse the repository at this point in the history
This commit updates the migrations to assume that the old setting value
may be invalid so that we don't end up with a new setting which fails
the objects schema validation.

This is a follow up to 7e304e0 which
added migrations for the `links` and `icons` settings. We have
gotten reports of errors happening with the migrations because the value of the old
setting before migration did not follow the "informal" format required
previously. Since there was nothing enforcing the format for the value
previously, we need to assume the worst possible cases in our
migrations.
  • Loading branch information
tgxworld committed Apr 25, 2024
1 parent e5d3a91 commit 0fb54ed
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 37 deletions.
32 changes: 14 additions & 18 deletions migrations/settings/0001-migrate-links-setting.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,25 @@ export default function migrate(settings) {
const oldSetting = settings.get("links");

if (oldSetting) {
const newSetting = oldSetting.split("|").map((link) => {
let [name, url, target] = link.split(",").map((s) => s.trim());

if (["_blank", "_self", "_parent", "_top"].indexOf(target) === -1) {
target = "_blank";
}
const newLinks = [];

const newLink = {
name,
url,
target
}
oldSetting.split("|").forEach((link) => {
let [name, url, target] = link.split(",").map((s) => s.trim());

Object.keys(newLink).forEach((key) => {
if (newLink[key] === undefined) {
delete newLink[key];
if (name && url) {
if (["_blank", "_self", "_parent", "_top"].indexOf(target) === -1) {
target = "_blank";
}
});

return newLink;
})
newLinks.push({
name,
url,
target,
});
}
});

settings.set("links", newSetting);
settings.set("links", newLinks);
}

return settings;
Expand Down
32 changes: 14 additions & 18 deletions migrations/settings/0002-migrate-icons-setting.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,25 @@ export default function migrate(settings) {
const oldSetting = settings.get("icons");

if (oldSetting) {
const newSetting = oldSetting.split("|").map((link) => {
let [iconName, url, target] = link.split(",").map((s) => s.trim());

if (["_blank", "_self", "_parent", "_top"].indexOf(target) === -1) {
target = "_blank";
}
const newIcons = [];

const newLink = {
icon_name: iconName,
url,
target
}
oldSetting.split("|").map((link) => {
let [iconName, url, target] = link.split(",").map((s) => s.trim());

Object.keys(newLink).forEach((key) => {
if (newLink[key] === undefined) {
delete newLink[key];
if (iconName && url) {
if (["_blank", "_self", "_parent", "_top"].indexOf(target) === -1) {
target = "_blank";
}
});

return newLink;
})
newIcons.push({
icon_name: iconName,
url,
target,
});
}
});

settings.set("icons", newSetting);
settings.set("icons", newIcons);
}

return settings;
Expand Down
21 changes: 21 additions & 0 deletions test/unit/migrations/settings/0001-migrate-links-setting-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,27 @@ import migrate from "../../../../migrations/settings/0001-migrate-links-setting"
module(
"Unit | Migrations | Settings | 0001-migrate-links-setting",
function () {
test("migrate when old setting is of an invalid format", function (assert) {
const settings = new Map(
Object.entries({
links: "some||another",
})
);

const result = migrate(settings);

const expectedResult = new Map(
Object.entries({
links: [],
})
);

assert.deepEqual(
Object.fromEntries(result.entries()),
Object.fromEntries(expectedResult.entries())
);
});

test("migrate", function (assert) {
const settings = new Map(
Object.entries({
Expand Down
26 changes: 25 additions & 1 deletion test/unit/migrations/settings/0002-migrate-icons-setting-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,27 @@ import migrate from "../../../../migrations/settings/0002-migrate-icons-setting"
module(
"Unit | Migrations | Settings | 0002-migrate-icons-setting",
function () {
test("migrate when old setting value is of an invalid format", function (assert) {
const settings = new Map(
Object.entries({
icons: "some||another",
})
);

const result = migrate(settings);

const expectedResult = new Map(
Object.entries({
icons: [],
})
);

assert.deepEqual(
Object.fromEntries(result.entries()),
Object.fromEntries(expectedResult.entries())
);
});

test("migrate", function (assert) {
const settings = new Map(
Object.entries({
Expand Down Expand Up @@ -36,7 +57,10 @@ module(
})
);

assert.deepEqual(result, expectedResult);
assert.deepEqual(
Object.fromEntries(result.entries()),
Object.fromEntries(expectedResult.entries())
);
});
}
);

0 comments on commit 0fb54ed

Please sign in to comment.