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:
parent
3e8de308a3
commit
2ca6d807e3
|
|
@ -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" }])
|
||||
})
|
||||
})
|
||||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -38,3 +38,5 @@ export const dbSchemaV7 = {
|
|||
...dbSchemaV6,
|
||||
inboxes: "&id",
|
||||
}
|
||||
|
||||
export const dbSchemaV8 = dbSchemaV7
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue