fix: handle tipUsers migration for older feed versions (#1384)

* fix: handle tipUsers migration for older feed versions

* chore: clean code

* test: add migrationTipUser test for feed service

* chore: clean code

* fix: implement upgradeToV8 for tipUsers migration and add tests

* refactor: remove legacy migration function and its tests

* chore: clean code
This commit is contained in:
Whitewater 2024-11-04 20:54:38 -08:00 committed by GitHub
parent 3e8de308a3
commit 2ca6d807e3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 58 additions and 8 deletions

View File

@ -0,0 +1,36 @@
import { afterEach } from "node:test"
import { describe, expect, it } from "vitest"
import { browserDB } from "./db"
describe("upgradeToV8", () => {
afterEach(async () => {
await browserDB.delete()
})
it("should set tipUsers to an empty array if tipUsers is not an array", async () => {
const insertFeeds = [
{ id: 1, tipUsers: {} },
{ id: 2, tipUsers: null },
{ id: 3, tipUsers: [{ name: "user1" }] },
]
// @ts-expect-error
await browserDB.feeds.bulkAdd(insertFeeds)
const feeds = await browserDB.feeds.toArray()
expect(feeds.length).toEqual(3)
expect(feeds[0].tipUsers).toEqual(insertFeeds[0].tipUsers)
expect(feeds[1].tipUsers).toEqual(insertFeeds[1].tipUsers)
expect(feeds[2].tipUsers).toEqual(insertFeeds[2].tipUsers)
await browserDB.transaction("rw", [browserDB.feeds], async (tx) => {
await browserDB.upgradeToV8(tx)
})
const feedsAfterMigrate = await browserDB.feeds.toArray()
expect(feedsAfterMigrate.length).toEqual(3)
expect(feedsAfterMigrate[0].tipUsers).toEqual([])
expect(feedsAfterMigrate[1].tipUsers).toEqual(null)
expect(feedsAfterMigrate[2].tipUsers).toEqual([{ name: "user1" }])
})
})

View File

@ -10,6 +10,7 @@ import {
dbSchemaV5,
dbSchemaV6,
dbSchemaV7,
dbSchemaV8,
} from "./db_schema"
import type { DB_Cleaner } from "./schemas/cleaner"
import type { DB_Entry, DB_EntryRelated } from "./schemas/entry"
@ -49,6 +50,7 @@ class BrowserDB extends Dexie {
this.version(5).stores(dbSchemaV5)
this.version(6).stores(dbSchemaV6)
this.version(7).stores(dbSchemaV7)
this.version(8).stores(dbSchemaV8).upgrade(this.upgradeToV8)
this.entries = this.table("entries")
this.feeds = this.table("feeds")
@ -64,6 +66,15 @@ class BrowserDB extends Dexie {
const session = trans.table("feedUnreads")
session.delete("feedId")
}
async upgradeToV8(trans: Transaction) {
// Fix https://github.com/RSSNext/Follow/issues/1308
const session = trans.table("feeds")
return session.toCollection().modify((feed) => {
if (!feed.tipUsers || Array.isArray(feed.tipUsers)) return
feed.tipUsers = []
})
}
}
export const browserDB = new BrowserDB()

View File

@ -38,3 +38,5 @@ export const dbSchemaV7 = {
...dbSchemaV6,
inboxes: "&id",
}
export const dbSchemaV8 = dbSchemaV7

View File

@ -9,14 +9,15 @@ declare global {
__app_is_upgraded__: boolean
}
}
export const doMigration = () => {
export const doMigration = async () => {
const lastVersion = localStorage.getItem(appVersionKey)
if (lastVersion && lastVersion !== APP_VERSION) {
appLog(`Upgrade from ${lastVersion} to ${APP_VERSION}`)
window.__app_is_upgraded__ = true
// NOTE: Add migration logic here
if (!lastVersion || lastVersion === APP_VERSION) {
localStorage.setItem(appVersionKey, APP_VERSION)
return
}
localStorage.setItem(appVersionKey, APP_VERSION)
// NOTE: Add migration logic here
appLog(`Upgrade from ${lastVersion} to ${APP_VERSION}`)
window.__app_is_upgraded__ = true
}