2022-03-10 02:23:17 +01:00
|
|
|
const Path = require('path')
|
2022-07-06 02:53:01 +02:00
|
|
|
const fs = require('../libs/fsExtra')
|
2022-06-08 03:15:00 +02:00
|
|
|
const njodb = require('../libs/njodb')
|
2022-03-10 02:23:17 +01:00
|
|
|
|
|
|
|
const { SupportedEbookTypes } = require('./globals')
|
2022-03-18 01:10:47 +01:00
|
|
|
const { PlayMethod } = require('./constants')
|
2022-03-17 15:04:10 +01:00
|
|
|
const { getId } = require('./index')
|
2023-01-06 00:45:27 +01:00
|
|
|
const { filePathToPOSIX } = require('./fileUtils')
|
2022-03-17 15:04:10 +01:00
|
|
|
const Logger = require('../Logger')
|
|
|
|
|
2022-03-18 23:09:17 +01:00
|
|
|
const Library = require('../objects/Library')
|
2022-03-10 02:23:17 +01:00
|
|
|
const LibraryItem = require('../objects/LibraryItem')
|
2022-03-17 01:15:25 +01:00
|
|
|
const Book = require('../objects/mediaTypes/Book')
|
|
|
|
|
2022-03-10 02:23:17 +01:00
|
|
|
const BookMetadata = require('../objects/metadata/BookMetadata')
|
2022-03-17 01:15:25 +01:00
|
|
|
const FileMetadata = require('../objects/metadata/FileMetadata')
|
|
|
|
|
2022-03-10 02:23:17 +01:00
|
|
|
const AudioFile = require('../objects/files/AudioFile')
|
|
|
|
const EBookFile = require('../objects/files/EBookFile')
|
|
|
|
const LibraryFile = require('../objects/files/LibraryFile')
|
|
|
|
const AudioMetaTags = require('../objects/metadata/AudioMetaTags')
|
2022-03-17 01:15:25 +01:00
|
|
|
|
|
|
|
const Author = require('../objects/entities/Author')
|
|
|
|
const Series = require('../objects/entities/Series')
|
|
|
|
|
2022-03-26 17:59:34 +01:00
|
|
|
const MediaProgress = require('../objects/user/MediaProgress')
|
2022-03-16 01:28:54 +01:00
|
|
|
const PlaybackSession = require('../objects/PlaybackSession')
|
2022-03-16 00:57:15 +01:00
|
|
|
|
|
|
|
const { isObject } = require('.')
|
|
|
|
const User = require('../objects/user/User')
|
2022-03-10 02:23:17 +01:00
|
|
|
|
|
|
|
var authorsToAdd = []
|
2022-03-13 19:47:36 +01:00
|
|
|
var existingDbAuthors = []
|
2022-03-10 02:23:17 +01:00
|
|
|
var seriesToAdd = []
|
2022-03-13 19:47:36 +01:00
|
|
|
var existingDbSeries = []
|
2022-03-10 02:23:17 +01:00
|
|
|
|
|
|
|
// Load old audiobooks
|
|
|
|
async function loadAudiobooks() {
|
|
|
|
var audiobookPath = Path.join(global.ConfigPath, 'audiobooks')
|
|
|
|
|
2022-04-10 17:05:05 +02:00
|
|
|
Logger.debug(`[dbMigration] loadAudiobooks path ${audiobookPath}`)
|
2022-03-10 02:23:17 +01:00
|
|
|
var pathExists = await fs.pathExists(audiobookPath)
|
|
|
|
if (!pathExists) {
|
2022-04-10 17:05:05 +02:00
|
|
|
Logger.debug(`[dbMigration] loadAudiobooks path does not exist ${audiobookPath}`)
|
2022-03-10 02:23:17 +01:00
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
|
|
|
var audiobooksDb = new njodb.Database(audiobookPath)
|
|
|
|
return audiobooksDb.select(() => true).then((results) => {
|
2022-04-10 17:05:05 +02:00
|
|
|
Logger.debug(`[dbMigration] loadAudiobooks select results ${results.data.length}`)
|
|
|
|
return results.data
|
2022-03-10 02:23:17 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeAuthorsFromOldAb(authorsList) {
|
|
|
|
return authorsList.filter(a => !!a).map(authorName => {
|
|
|
|
var existingAuthor = authorsToAdd.find(a => a.name.toLowerCase() === authorName.toLowerCase())
|
|
|
|
if (existingAuthor) {
|
|
|
|
return existingAuthor.toJSONMinimal()
|
|
|
|
}
|
2022-03-13 19:47:36 +01:00
|
|
|
var existingDbAuthor = existingDbAuthors.find(a => a.name.toLowerCase() === authorName.toLowerCase())
|
|
|
|
if (existingDbAuthor) {
|
|
|
|
return existingDbAuthor.toJSONMinimal()
|
|
|
|
}
|
2022-03-10 02:23:17 +01:00
|
|
|
|
|
|
|
var newAuthor = new Author()
|
|
|
|
newAuthor.setData({ name: authorName })
|
|
|
|
authorsToAdd.push(newAuthor)
|
2022-04-10 17:05:05 +02:00
|
|
|
// Logger.debug(`>>> Created new author named "${authorName}"`)
|
2022-03-10 02:23:17 +01:00
|
|
|
return newAuthor.toJSONMinimal()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeSeriesFromOldAb({ series, volumeNumber }) {
|
|
|
|
var existingSeries = seriesToAdd.find(s => s.name.toLowerCase() === series.toLowerCase())
|
|
|
|
if (existingSeries) {
|
|
|
|
return [existingSeries.toJSONMinimal(volumeNumber)]
|
|
|
|
}
|
2022-03-13 19:47:36 +01:00
|
|
|
var existingDbSeriesItem = existingDbSeries.find(s => s.name.toLowerCase() === series.toLowerCase())
|
|
|
|
if (existingDbSeriesItem) {
|
|
|
|
return [existingDbSeriesItem.toJSONMinimal(volumeNumber)]
|
|
|
|
}
|
2022-03-10 02:23:17 +01:00
|
|
|
var newSeries = new Series()
|
|
|
|
newSeries.setData({ name: series })
|
|
|
|
seriesToAdd.push(newSeries)
|
|
|
|
Logger.info(`>>> Created new series named "${series}"`)
|
|
|
|
return [newSeries.toJSONMinimal(volumeNumber)]
|
|
|
|
}
|
|
|
|
|
|
|
|
function getRelativePath(srcPath, basePath) {
|
2023-01-06 00:45:27 +01:00
|
|
|
srcPath = filePathToPOSIX(srcPath)
|
|
|
|
basePath = filePathToPOSIX(basePath)
|
2022-03-10 02:23:17 +01:00
|
|
|
return srcPath.replace(basePath, '')
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeFilesFromOldAb(audiobook) {
|
|
|
|
var libraryFiles = []
|
|
|
|
var ebookFiles = []
|
|
|
|
|
2022-04-10 17:05:05 +02:00
|
|
|
var _audioFiles = audiobook.audioFiles || []
|
|
|
|
var audioFiles = _audioFiles.map((af) => {
|
2022-03-10 02:23:17 +01:00
|
|
|
var fileMetadata = new FileMetadata(af)
|
|
|
|
fileMetadata.path = af.fullPath
|
|
|
|
fileMetadata.relPath = getRelativePath(af.fullPath, audiobook.fullPath)
|
|
|
|
|
|
|
|
var newLibraryFile = new LibraryFile()
|
|
|
|
newLibraryFile.ino = af.ino
|
|
|
|
newLibraryFile.metadata = fileMetadata.clone()
|
|
|
|
newLibraryFile.addedAt = af.addedAt
|
|
|
|
newLibraryFile.updatedAt = Date.now()
|
|
|
|
libraryFiles.push(newLibraryFile)
|
|
|
|
|
|
|
|
var audioMetaTags = new AudioMetaTags(af.metadata || {}) // Old metaTags was named metadata
|
|
|
|
delete af.metadata
|
|
|
|
|
|
|
|
var newAudioFile = new AudioFile(af)
|
|
|
|
newAudioFile.metadata = fileMetadata
|
|
|
|
newAudioFile.metaTags = audioMetaTags
|
|
|
|
newAudioFile.updatedAt = Date.now()
|
|
|
|
return newAudioFile
|
|
|
|
})
|
|
|
|
|
2022-04-10 17:05:05 +02:00
|
|
|
var _otherFiles = audiobook.otherFiles || []
|
|
|
|
_otherFiles.forEach((file) => {
|
2022-03-10 02:23:17 +01:00
|
|
|
var fileMetadata = new FileMetadata(file)
|
|
|
|
fileMetadata.path = file.fullPath
|
|
|
|
fileMetadata.relPath = getRelativePath(file.fullPath, audiobook.fullPath)
|
|
|
|
|
|
|
|
var newLibraryFile = new LibraryFile()
|
|
|
|
newLibraryFile.ino = file.ino
|
|
|
|
newLibraryFile.metadata = fileMetadata.clone()
|
|
|
|
newLibraryFile.addedAt = file.addedAt
|
|
|
|
newLibraryFile.updatedAt = Date.now()
|
|
|
|
libraryFiles.push(newLibraryFile)
|
|
|
|
|
|
|
|
var formatExt = (file.ext || '').slice(1)
|
|
|
|
if (SupportedEbookTypes.includes(formatExt)) {
|
|
|
|
var newEBookFile = new EBookFile()
|
|
|
|
newEBookFile.ino = file.ino
|
|
|
|
newEBookFile.metadata = fileMetadata
|
|
|
|
newEBookFile.ebookFormat = formatExt
|
|
|
|
newEBookFile.addedAt = file.addedAt
|
|
|
|
newEBookFile.updatedAt = Date.now()
|
|
|
|
ebookFiles.push(newEBookFile)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return {
|
|
|
|
libraryFiles,
|
|
|
|
ebookFiles,
|
|
|
|
audioFiles
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-19 16:13:10 +01:00
|
|
|
// Metadata path was changed to /metadata/items make sure cover is using new path
|
|
|
|
function cleanOldCoverPath(coverPath) {
|
|
|
|
if (!coverPath) return null
|
|
|
|
var oldMetadataPath = Path.posix.join(global.MetadataPath, 'books')
|
|
|
|
if (coverPath.startsWith(oldMetadataPath)) {
|
|
|
|
const newMetadataPath = Path.posix.join(global.MetadataPath, 'items')
|
|
|
|
return coverPath.replace(oldMetadataPath, newMetadataPath)
|
|
|
|
}
|
|
|
|
return coverPath
|
|
|
|
}
|
|
|
|
|
2022-03-10 02:23:17 +01:00
|
|
|
function makeLibraryItemFromOldAb(audiobook) {
|
|
|
|
var libraryItem = new LibraryItem()
|
2022-03-26 17:59:34 +01:00
|
|
|
libraryItem.id = audiobook.id
|
2022-03-10 02:23:17 +01:00
|
|
|
libraryItem.ino = audiobook.ino
|
|
|
|
libraryItem.libraryId = audiobook.libraryId
|
|
|
|
libraryItem.folderId = audiobook.folderId
|
|
|
|
libraryItem.path = audiobook.fullPath
|
|
|
|
libraryItem.relPath = audiobook.path
|
|
|
|
libraryItem.mtimeMs = audiobook.mtimeMs || 0
|
|
|
|
libraryItem.ctimeMs = audiobook.ctimeMs || 0
|
|
|
|
libraryItem.birthtimeMs = audiobook.birthtimeMs || 0
|
|
|
|
libraryItem.addedAt = audiobook.addedAt
|
2022-03-17 01:15:25 +01:00
|
|
|
libraryItem.updatedAt = audiobook.lastUpdate
|
2022-03-10 02:23:17 +01:00
|
|
|
libraryItem.lastScan = audiobook.lastScan
|
|
|
|
libraryItem.scanVersion = audiobook.scanVersion
|
|
|
|
libraryItem.isMissing = audiobook.isMissing
|
2022-03-11 01:45:02 +01:00
|
|
|
libraryItem.mediaType = 'book'
|
2022-03-10 02:23:17 +01:00
|
|
|
|
|
|
|
var bookEntity = new Book()
|
|
|
|
var bookMetadata = new BookMetadata(audiobook.book)
|
2022-03-14 01:34:31 +01:00
|
|
|
bookMetadata.publishedYear = audiobook.book.publishYear || null
|
2022-03-10 02:23:17 +01:00
|
|
|
if (audiobook.book.narrator) {
|
2022-04-10 17:05:05 +02:00
|
|
|
bookMetadata.narrators = (audiobook.book.narrator || '').split(', ')
|
2022-03-10 02:23:17 +01:00
|
|
|
}
|
|
|
|
// Returns array of json minimal authors
|
2022-04-10 17:05:05 +02:00
|
|
|
bookMetadata.authors = makeAuthorsFromOldAb((audiobook.book.authorFL || '').split(', '))
|
2022-03-10 02:23:17 +01:00
|
|
|
|
|
|
|
// Returns array of json minimal series
|
|
|
|
if (audiobook.book.series) {
|
|
|
|
bookMetadata.series = makeSeriesFromOldAb(audiobook.book)
|
|
|
|
}
|
|
|
|
|
2022-04-03 23:01:59 +02:00
|
|
|
bookEntity.libraryItemId = libraryItem.id
|
2022-03-10 02:23:17 +01:00
|
|
|
bookEntity.metadata = bookMetadata
|
2022-03-19 16:13:10 +01:00
|
|
|
bookEntity.coverPath = cleanOldCoverPath(audiobook.book.coverFullPath)
|
2022-03-10 02:23:17 +01:00
|
|
|
bookEntity.tags = [...audiobook.tags]
|
|
|
|
|
|
|
|
var payload = makeFilesFromOldAb(audiobook)
|
2022-03-26 17:59:34 +01:00
|
|
|
bookEntity.audioFiles = payload.audioFiles
|
|
|
|
bookEntity.chapters = []
|
|
|
|
if (audiobook.chapters && audiobook.chapters.length) {
|
|
|
|
bookEntity.chapters = audiobook.chapters.map(c => ({ ...c }))
|
2022-03-10 02:23:17 +01:00
|
|
|
}
|
2022-03-26 17:59:34 +01:00
|
|
|
bookEntity.missingParts = audiobook.missingParts || []
|
2022-03-10 02:23:17 +01:00
|
|
|
|
2022-03-26 17:59:34 +01:00
|
|
|
if (payload.ebookFiles.length) {
|
|
|
|
bookEntity.ebookFile = payload.ebookFiles[0]
|
|
|
|
}
|
2022-03-17 01:15:25 +01:00
|
|
|
|
2022-03-11 01:45:02 +01:00
|
|
|
libraryItem.media = bookEntity
|
2022-03-10 02:23:17 +01:00
|
|
|
libraryItem.libraryFiles = payload.libraryFiles
|
|
|
|
return libraryItem
|
|
|
|
}
|
|
|
|
|
2022-03-16 00:57:15 +01:00
|
|
|
async function migrateLibraryItems(db) {
|
|
|
|
Logger.info(`==== Starting Library Item migration ====`)
|
2022-03-10 02:23:17 +01:00
|
|
|
|
|
|
|
var audiobooks = await loadAudiobooks()
|
|
|
|
if (!audiobooks.length) {
|
|
|
|
Logger.info(`>>> No audiobooks in db, no migration necessary`)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger.info(`>>> Loaded old audiobook data with ${audiobooks.length} records`)
|
|
|
|
|
|
|
|
if (db.libraryItems.length) {
|
|
|
|
Logger.info(`>>> Some library items already loaded ${db.libraryItems.length} items | ${db.series.length} series | ${db.authors.length} authors`)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-13 19:47:36 +01:00
|
|
|
if (db.authors && db.authors.length) {
|
|
|
|
existingDbAuthors = db.authors
|
|
|
|
}
|
|
|
|
if (db.series && db.series.length) {
|
|
|
|
existingDbSeries = db.series
|
|
|
|
}
|
|
|
|
|
2022-03-10 02:23:17 +01:00
|
|
|
var libraryItems = audiobooks.map((ab) => makeLibraryItemFromOldAb(ab))
|
|
|
|
|
|
|
|
Logger.info(`>>> ${libraryItems.length} Library Items made`)
|
2022-04-23 13:25:16 +02:00
|
|
|
await db.bulkInsertEntities('libraryItem', libraryItems)
|
2022-03-10 02:23:17 +01:00
|
|
|
if (authorsToAdd.length) {
|
|
|
|
Logger.info(`>>> ${authorsToAdd.length} Authors made`)
|
2022-04-23 13:25:16 +02:00
|
|
|
await db.bulkInsertEntities('author', authorsToAdd)
|
2022-03-10 02:23:17 +01:00
|
|
|
}
|
|
|
|
if (seriesToAdd.length) {
|
|
|
|
Logger.info(`>>> ${seriesToAdd.length} Series made`)
|
|
|
|
await db.insertEntities('series', seriesToAdd)
|
|
|
|
}
|
2022-03-13 19:47:36 +01:00
|
|
|
existingDbSeries = []
|
|
|
|
existingDbAuthors = []
|
|
|
|
authorsToAdd = []
|
|
|
|
seriesToAdd = []
|
2022-03-16 00:57:15 +01:00
|
|
|
Logger.info(`==== Library Item migration complete ====`)
|
|
|
|
}
|
|
|
|
|
2022-03-17 01:15:25 +01:00
|
|
|
function cleanUserObject(db, userObj) {
|
2022-03-16 00:57:15 +01:00
|
|
|
var cleanedUserPayload = {
|
|
|
|
...userObj,
|
2022-03-26 17:59:34 +01:00
|
|
|
mediaProgress: [],
|
2022-03-16 00:57:15 +01:00
|
|
|
bookmarks: []
|
|
|
|
}
|
|
|
|
|
2022-03-26 17:59:34 +01:00
|
|
|
// UserAudiobookData is now MediaProgress and AudioBookmarks separated
|
2022-03-16 00:57:15 +01:00
|
|
|
if (userObj.audiobooks) {
|
|
|
|
for (const audiobookId in userObj.audiobooks) {
|
|
|
|
if (isObject(userObj.audiobooks[audiobookId])) {
|
|
|
|
// Bookmarks now live on User.js object instead of inside UserAudiobookData
|
|
|
|
if (userObj.audiobooks[audiobookId].bookmarks) {
|
|
|
|
const cleanedBookmarks = userObj.audiobooks[audiobookId].bookmarks.map((bm) => {
|
2022-03-26 17:59:34 +01:00
|
|
|
bm.libraryItemId = audiobookId
|
2022-03-16 00:57:15 +01:00
|
|
|
return bm
|
|
|
|
})
|
|
|
|
cleanedUserPayload.bookmarks = cleanedUserPayload.bookmarks.concat(cleanedBookmarks)
|
|
|
|
}
|
|
|
|
|
2022-04-10 17:05:05 +02:00
|
|
|
var userAudiobookData = userObj.audiobooks[audiobookId]
|
2022-03-26 17:59:34 +01:00
|
|
|
var liProgress = new MediaProgress() // New Progress Object
|
|
|
|
liProgress.id = userAudiobookData.audiobookId
|
2022-03-16 00:57:15 +01:00
|
|
|
liProgress.libraryItemId = userAudiobookData.audiobookId
|
2022-03-18 21:31:46 +01:00
|
|
|
liProgress.duration = userAudiobookData.totalDuration
|
2022-03-17 18:25:12 +01:00
|
|
|
liProgress.isFinished = !!userAudiobookData.isRead
|
2022-03-16 00:57:15 +01:00
|
|
|
Object.keys(liProgress.toJSON()).forEach((key) => {
|
|
|
|
if (userAudiobookData[key] !== undefined) {
|
|
|
|
liProgress[key] = userAudiobookData[key]
|
|
|
|
}
|
|
|
|
})
|
2022-03-26 17:59:34 +01:00
|
|
|
cleanedUserPayload.mediaProgress.push(liProgress.toJSON())
|
2022-03-16 00:57:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const user = new User(cleanedUserPayload)
|
|
|
|
return db.usersDb.update((record) => record.id === user.id, () => user).then((results) => {
|
|
|
|
Logger.debug(`[dbMigration] Updated User: ${results.updated} | Selected: ${results.selected}`)
|
|
|
|
return true
|
|
|
|
}).catch((error) => {
|
|
|
|
Logger.error(`[dbMigration] Update User Failed: ${error}`)
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function cleanSessionObj(db, userListeningSession) {
|
|
|
|
var newPlaybackSession = new PlaybackSession(userListeningSession)
|
2022-03-18 02:28:04 +01:00
|
|
|
newPlaybackSession.id = getId('play')
|
2022-03-16 00:57:15 +01:00
|
|
|
newPlaybackSession.mediaType = 'book'
|
|
|
|
newPlaybackSession.updatedAt = userListeningSession.lastUpdate
|
2022-03-26 17:59:34 +01:00
|
|
|
newPlaybackSession.libraryItemId = userListeningSession.audiobookId
|
2022-03-18 01:10:47 +01:00
|
|
|
newPlaybackSession.playMethod = PlayMethod.TRANSCODE
|
2022-03-16 00:57:15 +01:00
|
|
|
|
|
|
|
// We only have title to transfer over nicely
|
|
|
|
var bookMetadata = new BookMetadata()
|
|
|
|
bookMetadata.title = userListeningSession.audiobookTitle || ''
|
|
|
|
newPlaybackSession.mediaMetadata = bookMetadata
|
|
|
|
|
2022-03-18 19:44:29 +01:00
|
|
|
return db.sessionsDb.update((record) => record.id === userListeningSession.id, () => newPlaybackSession).then((results) => true).catch((error) => {
|
2022-03-16 00:57:15 +01:00
|
|
|
Logger.error(`[dbMigration] Update Session Failed: ${error}`)
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async function migrateUserData(db) {
|
|
|
|
Logger.info(`==== Starting User migration ====`)
|
|
|
|
|
2022-03-18 23:09:17 +01:00
|
|
|
// Libraries with previous mediaType of "podcast" moved to "book"
|
|
|
|
// because migrating those items to podcast objects will be a nightmare
|
|
|
|
// users will need to create a new library for podcasts
|
|
|
|
var availableIcons = ['database', 'audiobook', 'book', 'comic', 'podcast']
|
|
|
|
const libraries = await db.librariesDb.select((result) => (result.mediaType != 'book' || !availableIcons.includes(result.icon)))
|
|
|
|
.then((results) => results.data.map(lib => new Library(lib)))
|
|
|
|
if (!libraries.length) {
|
|
|
|
Logger.info('[dbMigration] No libraries found needing migration')
|
|
|
|
} else {
|
|
|
|
for (const library of libraries) {
|
|
|
|
Logger.info(`>> Migrating library "${library.name}" with media type "${library.mediaType}"`)
|
|
|
|
await db.librariesDb.update((record) => record.id === library.id, () => library).then(() => true).catch((error) => {
|
|
|
|
Logger.error(`[dbMigration] Update library failed: ${error}`)
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-03-16 00:57:15 +01:00
|
|
|
const userObjects = await db.usersDb.select((result) => result.audiobooks != undefined).then((results) => results.data)
|
|
|
|
if (!userObjects.length) {
|
|
|
|
Logger.warn('[dbMigration] No users found needing migration')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var userCount = 0
|
|
|
|
for (const userObj of userObjects) {
|
|
|
|
Logger.info(`[dbMigration] Migrating User "${userObj.username}"`)
|
|
|
|
var success = await cleanUserObject(db, userObj)
|
|
|
|
if (!success) {
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 500))
|
|
|
|
Logger.warn(`[dbMigration] Second attempt Migrating User "${userObj.username}"`)
|
|
|
|
success = await cleanUserObject(db, userObj)
|
|
|
|
if (!success) {
|
|
|
|
throw new Error('Db migration failed migrating users')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
userCount++
|
|
|
|
}
|
|
|
|
|
|
|
|
var sessionCount = 0
|
|
|
|
const userListeningSessions = await db.sessionsDb.select((result) => result.audiobookId != undefined).then((results) => results.data)
|
|
|
|
if (userListeningSessions.length) {
|
|
|
|
|
|
|
|
for (const session of userListeningSessions) {
|
|
|
|
var success = await cleanSessionObj(db, session)
|
|
|
|
if (!success) {
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 500))
|
|
|
|
Logger.warn(`[dbMigration] Second attempt Migrating Session "${session.id}"`)
|
|
|
|
success = await cleanSessionObj(db, session)
|
|
|
|
if (!success) {
|
|
|
|
Logger.error(`[dbMigration] Failed to migrate session "${session.id}"`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (success) sessionCount++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger.info(`==== User migration complete (${userCount} Users, ${sessionCount} Sessions) ====`)
|
2022-03-10 02:23:17 +01:00
|
|
|
}
|
2022-03-19 16:13:10 +01:00
|
|
|
|
|
|
|
async function checkUpdateMetadataPath() {
|
|
|
|
var bookMetadataPath = Path.posix.join(global.MetadataPath, 'books') // OLD
|
|
|
|
if (!(await fs.pathExists(bookMetadataPath))) {
|
|
|
|
Logger.debug(`[dbMigration] No need to update books metadata path`)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var itemsMetadataPath = Path.posix.join(global.MetadataPath, 'items')
|
|
|
|
await fs.rename(bookMetadataPath, itemsMetadataPath)
|
|
|
|
Logger.info(`>>> Renamed metadata dir from /metadata/books to /metadata/items`)
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.migrate = async (db) => {
|
|
|
|
await checkUpdateMetadataPath()
|
|
|
|
// Before DB Load clean data
|
|
|
|
await migrateUserData(db)
|
|
|
|
await db.init()
|
|
|
|
// After DB Load
|
|
|
|
await migrateLibraryItems(db)
|
|
|
|
// TODO: Eventually remove audiobooks db when stable
|
|
|
|
}
|