audiobookshelf/server/utils/migrations/dbMigration.js

231 lines
6.8 KiB
JavaScript
Raw Normal View History

const uuidv4 = require("uuid").v4
2023-03-15 23:42:35 +01:00
const package = require('../../../package.json')
const Logger = require('../../Logger')
const Database = require('../../Database')
const oldDbFiles = require('./oldDbFiles')
const oldDbIdMap = {
users: {},
libraries: {},
libraryFolders: {},
libraryItems: {},
books: {},
tags: {}
}
const newRecords = {
User: [],
Library: [],
LibraryFolder: [],
LibrarySetting: [],
LibraryItem: [],
Book: [],
BookChapter: [],
Tag: [],
BookTag: [],
Podcast: []
}
2023-03-15 23:42:35 +01:00
function migrateBook(oldLibraryItem, LibraryItem) {
2023-03-15 23:42:35 +01:00
const oldBook = oldLibraryItem.media
const Book = {
id: uuidv4(),
2023-03-15 23:42:35 +01:00
title: oldBook.metadata.title,
subtitle: oldBook.metadata.subtitle,
publishedYear: oldBook.metadata.publishedYear,
publishedDate: oldBook.metadata.publishedDate,
publisher: oldBook.metadata.publisher,
description: oldBook.metadata.description,
isbn: oldBook.metadata.isbn,
asin: oldBook.metadata.asin,
language: oldBook.metadata.language,
explicit: !!oldBook.metadata.explicit,
lastCoverSearchQuery: oldBook.lastCoverSearchQuery,
lastCoverSearch: oldBook.lastCoverSearch,
LibraryItemId: LibraryItem.id,
createdAt: LibraryItem.createdAt,
updatedAt: LibraryItem.updatedAt
}
2023-03-15 23:42:35 +01:00
oldDbIdMap.books[oldLibraryItem.id] = Book.id
newRecords.Book.push(Book)
2023-03-15 23:42:35 +01:00
// TODO: Handle cover image record
// TODO: Handle EBook record
// Logger.info(`[dbMigration] migrateBook: Book migrated "${Book.title}" (${Book.id})`)
2023-03-15 23:42:35 +01:00
const oldTags = oldBook.tags || []
for (const oldTag of oldTags) {
let tagId = null
if (oldDbIdMap[oldTag]) {
tagId = oldDbIdMap[oldTag]
} else {
const Tag = {
id: uuidv4(),
2023-03-15 23:42:35 +01:00
name: oldTag
}
2023-03-15 23:42:35 +01:00
tagId = Tag.id
newRecords.Tag.push(Tag)
2023-03-15 23:42:35 +01:00
}
newRecords.BookTag.push({
id: uuidv4(),
2023-03-15 23:42:35 +01:00
BookId: Book.id,
TagId: tagId
})
}
for (const oldChapter of oldBook.chapters) {
const BookChapter = {
id: uuidv4(),
2023-03-15 23:42:35 +01:00
index: oldChapter.id,
start: oldChapter.start,
end: oldChapter.end,
title: oldChapter.title,
BookId: Book.id
}
newRecords.BookChapter.push(BookChapter)
2023-03-15 23:42:35 +01:00
}
}
function migratePodcast(oldLibraryItem, LibraryItem) {
// TODO: Migrate podcast
}
function migrateLibraryItems(oldLibraryItems) {
2023-03-15 23:42:35 +01:00
for (const oldLibraryItem of oldLibraryItems) {
// Logger.info(`[dbMigration] migrateLibraryItems: Migrating library item "${oldLibraryItem.media.metadata.title}" (${oldLibraryItem.id})`)
2023-03-15 23:42:35 +01:00
const LibraryId = oldDbIdMap.libraryFolders[oldLibraryItem.folderId]
if (!LibraryId) {
Logger.error(`[dbMigration] migrateLibraryItems: Old library folder id not found "${oldLibraryItem.folderId}"`)
continue
}
const LibraryItem = {
id: uuidv4(),
2023-03-15 23:42:35 +01:00
ino: oldLibraryItem.ino,
path: oldLibraryItem.path,
relPath: oldLibraryItem.relPath,
mediaType: oldLibraryItem.mediaType,
isFile: !!oldLibraryItem.isFile,
isMissing: !!oldLibraryItem.isMissing,
isInvalid: !!oldLibraryItem.isInvalid,
mtime: oldLibraryItem.mtimeMs,
ctime: oldLibraryItem.ctimeMs,
birthtime: oldLibraryItem.birthtimeMs,
lastScan: oldLibraryItem.lastScan,
lastScanVersion: oldLibraryItem.scanVersion,
createdAt: oldLibraryItem.addedAt,
updatedAt: oldLibraryItem.updatedAt,
LibraryId
}
oldDbIdMap.libraryItems[oldLibraryItem.id] = LibraryItem.id
newRecords.LibraryItem.push(LibraryItem)
2023-03-15 23:42:35 +01:00
// Logger.info(`[dbMigration] migrateLibraryItems: LibraryItem "${LibraryItem.path}" migrated (${LibraryItem.id})`)
2023-03-15 23:42:35 +01:00
if (oldLibraryItem.mediaType === 'book') {
migrateBook(oldLibraryItem, LibraryItem)
} else if (oldLibraryItem.mediaType === 'podcast') {
migratePodcast(oldLibraryItem, LibraryItem)
2023-03-15 23:42:35 +01:00
}
}
}
function migrateLibraries(oldLibraries) {
2023-03-15 23:42:35 +01:00
for (const oldLibrary of oldLibraries) {
// Logger.info(`[dbMigration] migrateLibraries: Migrating library "${oldLibrary.name}" (${oldLibrary.id})`)
2023-03-15 23:42:35 +01:00
const Library = {
id: uuidv4(),
2023-03-15 23:42:35 +01:00
name: oldLibrary.name,
displayOrder: oldLibrary.displayOrder,
icon: oldLibrary.icon || null,
mediaType: oldLibrary.mediaType || null,
provider: oldLibrary.provider,
createdAt: oldLibrary.createdAt,
updatedAt: oldLibrary.lastUpdate
}
2023-03-15 23:42:35 +01:00
oldDbIdMap.libraries[oldLibrary.id] = Library.id
newRecords.Library.push(Library)
2023-03-15 23:42:35 +01:00
const oldLibrarySettings = oldLibrary.settings || {}
for (const oldSettingsKey in oldLibrarySettings) {
const LibrarySetting = {
id: uuidv4(),
2023-03-15 23:42:35 +01:00
key: oldSettingsKey,
value: oldLibrarySettings[oldSettingsKey],
LibraryId: Library.id
}
newRecords.LibrarySetting.push(LibrarySetting)
// Logger.info(`[dbMigration] migrateLibraries: LibrarySetting "${LibrarySetting.key}" migrated (${LibrarySetting.id})`)
2023-03-15 23:42:35 +01:00
}
// Logger.info(`[dbMigration] migrateLibraries: Library "${Library.name}" migrated (${Library.id})`)
2023-03-15 23:42:35 +01:00
for (const oldFolder of oldLibrary.folders) {
// Logger.info(`[dbMigration] migrateLibraries: Migrating folder "${oldFolder.fullPath}" (${oldFolder.id})`)
2023-03-15 23:42:35 +01:00
const LibraryFolder = {
id: uuidv4(),
2023-03-15 23:42:35 +01:00
path: oldFolder.fullPath,
LibraryId: Library.id,
createdAt: oldFolder.addedAt
}
2023-03-15 23:42:35 +01:00
oldDbIdMap.libraryFolders[oldFolder.id] = LibraryFolder.id
newRecords.LibraryFolder.push(LibraryFolder)
2023-03-15 23:42:35 +01:00
// Logger.info(`[dbMigration] migrateLibraries: LibraryFolder "${LibraryFolder.path}" migrated (${LibraryFolder.id})`)
2023-03-15 23:42:35 +01:00
}
}
}
function migrateUsers(oldUsers) {
2023-03-15 23:42:35 +01:00
for (const oldUser of oldUsers) {
// Logger.info(`[dbMigration] migrateUsers: Migrating user "${oldUser.username}" (${oldUser.id})`)
2023-03-15 23:42:35 +01:00
const User = {
id: uuidv4(),
2023-03-15 23:42:35 +01:00
username: oldUser.username,
pash: oldUser.pash || null,
type: oldUser.type || null,
token: oldUser.token || null,
isActive: !!oldUser.isActive,
lastSeen: oldUser.lastSeen || null,
createdAt: oldUser.createdAt || Date.now()
}
2023-03-15 23:42:35 +01:00
oldDbIdMap.users[oldUser.id] = User.id
newRecords.User.push(User)
2023-03-15 23:42:35 +01:00
// Logger.info(`[dbMigration] migrateUsers: User "${User.username}" migrated (${User.id})`)
2023-03-15 23:42:35 +01:00
// for (const oldMediaProgress of oldUser.mediaProgress) {
// const MediaProgress = {
2023-03-15 23:42:35 +01:00
// }
2023-03-15 23:42:35 +01:00
// }
}
}
module.exports.migrate = async () => {
2023-03-15 23:50:47 +01:00
Logger.info(`[dbMigration] Starting migration`)
2023-03-15 23:42:35 +01:00
const data = await oldDbFiles.init()
2023-03-15 23:50:47 +01:00
const start = Date.now()
migrateLibraries(data.libraries)
migrateLibraryItems(data.libraryItems)
migrateUsers(data.users)
for (const model in newRecords) {
Logger.info(`[dbMigration] Creating ${newRecords[model].length} ${model} records`)
await Database.models[model].bulkCreate(newRecords[model])
}
2023-03-15 23:50:47 +01:00
const elapsed = Date.now() - start
Logger.info(`[dbMigration] Migration complete. Elapsed ${(elapsed / 1000).toFixed(2)}s`)
2023-03-15 23:42:35 +01:00
}