Skip to content

Commit

Permalink
Fix database upgrade from v1 to v3
Browse files Browse the repository at this point in the history
  • Loading branch information
blmage committed Feb 18, 2021
1 parent bf777f5 commit 3e5fa5e
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 40 deletions.
2 changes: 1 addition & 1 deletion dist/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Duolingo Solution Viewer",
"version": "4.4.0",
"version": "4.4.1",
"description": "Provides access to the lists of solutions for Duolingo's translation / listening challenges, and restores the correction of typos.",
"permissions": [
"activeTab",
Expand Down
2 changes: 1 addition & 1 deletion dist/src/background.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "A browser extension providing access to the complete lists of accepted solutions for Duolingo's translation challenges.",
"license": "MIT",
"author": "blmage",
"version": "4.4.0",
"version": "4.4.1",
"scripts": {
"build": "rollup -c --environment production",
"build-debug": "rollup -c --environment development",
Expand Down
67 changes: 31 additions & 36 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,43 +136,38 @@ database.version(2)

// Fix detection of challenges for discussions that exist in multiple languages.
// Clean up obsolete tables and unwanted challenges from the database.
database.version(3)
.stores({
[TABLE_COMMENT_CHALLENGES]: null,
[TABLE_DISCUSSION_COMMENTS]: null,
})
.upgrade(async () => {
await database.table(TABLE_DISCUSSION_CHALLENGES)
.count(async count => {
const sliceSize = 200;
const sliceCount = Math.ceil(count / sliceSize);

for (let slice = 0; slice < sliceCount; slice++) {
const newRows = [];
const obsoleteRows = [];

await database.table(TABLE_DISCUSSION_CHALLENGES)
.offset(slice * sliceSize)
.limit(sliceSize)
.each(challengeRow => {
obsoleteRows.push([
challengeRow[FIELD_DISCUSSION_ID],
challengeRow[FIELD_LOCALE],
]);

if (!Challenge.isOfType(challengeRow[FIELD_CHALLENGE], CHALLENGE_TYPE_LISTENING)) {
challengeRow[FIELD_LOCALE] = Challenge.getSolutionsLocale(challengeRow[FIELD_CHALLENGE]);
newRows.push(challengeRow);
}
});

await database.table(TABLE_DISCUSSION_CHALLENGES).bulkDelete(obsoleteRows);
await database.table(TABLE_DISCUSSION_CHALLENGES).bulkPut(newRows);
}
});
database.version(3).upgrade(async () => {
await database.table(TABLE_DISCUSSION_CHALLENGES)
.count(async count => {
const sliceSize = 200;
const sliceCount = Math.ceil(count / sliceSize);

for (let slice = 0; slice < sliceCount; slice++) {
const newRows = [];
const obsoleteRows = [];

await database.table(TABLE_DISCUSSION_CHALLENGES)
.offset(slice * sliceSize)
.limit(sliceSize)
.each(challengeRow => {
obsoleteRows.push([
challengeRow[FIELD_DISCUSSION_ID],
challengeRow[FIELD_LOCALE],
]);

if (!Challenge.isOfType(challengeRow[FIELD_CHALLENGE], CHALLENGE_TYPE_LISTENING)) {
challengeRow[FIELD_LOCALE] = Challenge.getSolutionsLocale(challengeRow[FIELD_CHALLENGE]);
newRows.push(challengeRow);
}
});

await database.table(TABLE_DISCUSSION_CHALLENGES).bulkDelete(obsoleteRows);
await database.table(TABLE_DISCUSSION_CHALLENGES).bulkPut(newRows);
}
});

await database.table(TABLE_COMMENT_DISCUSSIONS).clear();
});
await database.table(TABLE_COMMENT_DISCUSSIONS).clear();
});

/**
* @returns {number} The index of the current 30-minute slice of time.
Expand Down

0 comments on commit 3e5fa5e

Please sign in to comment.