Adding cloud -> self host and self host -> cloud view migrations.

This commit is contained in:
mike12345567 2021-09-30 12:17:27 +01:00
parent ccd2913857
commit b3cda1cf64
2 changed files with 48 additions and 26 deletions

View File

@ -5,7 +5,6 @@ const {
generateRowID, generateRowID,
DocumentTypes, DocumentTypes,
InternalTables, InternalTables,
generateMemoryViewID,
} = require("../../../db/utils") } = require("../../../db/utils")
const userController = require("../user") const userController = require("../user")
const { const {
@ -20,7 +19,12 @@ const { fullSearch, paginatedSearch } = require("./internalSearch")
const { getGlobalUsersFromMetadata } = require("../../../utilities/global") const { getGlobalUsersFromMetadata } = require("../../../utilities/global")
const inMemoryViews = require("../../../db/inMemoryView") const inMemoryViews = require("../../../db/inMemoryView")
const env = require("../../../environment") const env = require("../../../environment")
const { migrateToInMemoryView } = require("../view/utils") const {
migrateToInMemoryView,
migrateToDesignView,
getFromDesignDoc,
getFromMemoryDoc,
} = require("../view/utils")
const CALCULATION_TYPES = { const CALCULATION_TYPES = {
SUM: "sum", SUM: "sum",
@ -74,33 +78,24 @@ async function getRawTableData(ctx, db, tableId) {
} }
async function getView(db, viewName) { async function getView(db, viewName) {
let viewInfo let mainGetter = env.SELF_HOSTED ? getFromDesignDoc : getFromMemoryDoc
async function getFromDesignDoc() { let secondaryGetter = env.SELF_HOSTED ? getFromMemoryDoc : getFromDesignDoc
const designDoc = await db.get("_design/database") let migration = env.SELF_HOSTED ? migrateToDesignView : migrateToInMemoryView
viewInfo = designDoc.views[viewName] let viewInfo,
return viewInfo migrate = false
} try {
let migrate = false viewInfo = await mainGetter(db, viewName)
if (env.SELF_HOSTED) { } catch (err) {
viewInfo = await getFromDesignDoc() // check if it can be retrieved from design doc (needs migrated)
} else { if (err.status !== 404) {
try { viewInfo = null
viewInfo = await db.get(generateMemoryViewID(viewName)) } else {
if (viewInfo) { viewInfo = await secondaryGetter(db, viewName)
viewInfo = viewInfo.view migrate = !!viewInfo
}
} catch (err) {
// check if it can be retrieved from design doc (needs migrated)
if (err.status !== 404) {
viewInfo = null
} else {
viewInfo = await getFromDesignDoc()
migrate = !!viewInfo
}
} }
} }
if (migrate) { if (migrate) {
await migrateToInMemoryView(db, viewName) await migration(db, viewName)
} }
if (!viewInfo) { if (!viewInfo) {
throw "View does not exist." throw "View does not exist."

View File

@ -107,3 +107,30 @@ exports.migrateToInMemoryView = async (db, viewName) => {
await db.put(designDoc) await db.put(designDoc)
await exports.saveView(db, null, viewName, view) await exports.saveView(db, null, viewName, view)
} }
exports.migrateToDesignView = async (db, viewName) => {
let view = await db.get(generateMemoryViewID(viewName))
const designDoc = await db.get("_design/database")
designDoc.views[viewName] = view.view
await db.put(designDoc)
await db.remove(view._id, view._rev)
}
exports.getFromDesignDoc = async (db, viewName) => {
const designDoc = await db.get("_design/database")
let view = designDoc.views[viewName]
if (view == null) {
throw { status: 404, message: "Unable to get view" }
}
return view
}
exports.getFromMemoryDoc = async (db, viewName) => {
let view = await db.get(generateMemoryViewID(viewName))
if (view) {
view = view.view
} else {
throw { status: 404, message: "Unable to get view" }
}
return view
}