Some typing improvements, as well as getting deletion/setup working a bit better.
This commit is contained in:
parent
654905ea90
commit
a2d4f8523c
|
@ -264,11 +264,16 @@ export class DatabaseImpl implements Database {
|
|||
if (body) {
|
||||
args.body = body
|
||||
}
|
||||
const response = await directCouchUrlCall(args)
|
||||
if (response.status > 300) {
|
||||
throw new Error(await response.text())
|
||||
}
|
||||
return (await response.json()) as T
|
||||
return this.performCall(() => {
|
||||
return async () => {
|
||||
const response = await directCouchUrlCall(args)
|
||||
const json = await response.json()
|
||||
if (response.status > 300) {
|
||||
throw json
|
||||
}
|
||||
return json as T
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
async sql<T extends Document>(
|
||||
|
|
|
@ -62,7 +62,13 @@ export class DDInstrumentedDatabase implements Database {
|
|||
): Promise<DocumentDestroyResponse> {
|
||||
return tracer.trace("db.remove", span => {
|
||||
span?.addTags({ db_name: this.name, doc_id: id })
|
||||
return this.db.remove(id, rev)
|
||||
if (typeof id === "object") {
|
||||
return this.db.remove(id)
|
||||
} else if (rev) {
|
||||
return this.db.remove(id, rev)
|
||||
} else {
|
||||
throw new Error("No revision supplied for removal")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
@ -492,7 +492,7 @@ export class UserDB {
|
|||
|
||||
await platform.users.removeUser(dbUser)
|
||||
|
||||
await db.remove(userId, dbUser._rev)
|
||||
await db.remove(userId, dbUser._rev!)
|
||||
|
||||
const creatorsToDelete = (await isCreator(dbUser)) ? 1 : 0
|
||||
await UserDB.quotas.removeUsers(1, creatorsToDelete)
|
||||
|
|
|
@ -60,6 +60,7 @@ import sdk from "../../sdk"
|
|||
import { builderSocket } from "../../websockets"
|
||||
import { sdk as sharedCoreSDK } from "@budibase/shared-core"
|
||||
import * as appMigrations from "../../appMigrations"
|
||||
import { cleanupApp } from "../../sdk/app/tables/internal/sqs"
|
||||
|
||||
// utility function, need to do away with this
|
||||
async function getLayouts() {
|
||||
|
@ -589,6 +590,9 @@ async function destroyApp(ctx: UserCtx) {
|
|||
}
|
||||
|
||||
async function preDestroyApp(ctx: UserCtx) {
|
||||
if (env.SQS_SEARCH_ENABLE) {
|
||||
await sdk.tables.sqs.cleanupApp()
|
||||
}
|
||||
const { rows } = await getUniqueRows([ctx.params.appId])
|
||||
ctx.rowCount = rows.length
|
||||
}
|
||||
|
|
|
@ -325,7 +325,7 @@ class TableSaveFunctions {
|
|||
user: this.user,
|
||||
})
|
||||
if (env.SQS_SEARCH_ENABLE) {
|
||||
await sdk.tables.sqs.addTableToSqlite(table)
|
||||
await sdk.tables.sqs.addTable(table)
|
||||
}
|
||||
return table
|
||||
}
|
||||
|
@ -519,7 +519,7 @@ export async function internalTableCleanup(table: Table, rows?: Row[]) {
|
|||
await AttachmentCleanup.tableDelete(table, rows)
|
||||
}
|
||||
if (env.SQS_SEARCH_ENABLE) {
|
||||
await sdk.tables.sqs.removeTableFromSqlite(table)
|
||||
await sdk.tables.sqs.removeTable(table)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -19,7 +19,7 @@ import {
|
|||
sqlOutputProcessing,
|
||||
} from "../../../../api/controllers/row/utils"
|
||||
import sdk from "../../../index"
|
||||
import { context } from "@budibase/backend-core"
|
||||
import { context, SQLITE_DESIGN_DOC_ID } from "@budibase/backend-core"
|
||||
import {
|
||||
CONSTANT_INTERNAL_ROW_COLS,
|
||||
SQS_DATASOURCE_INTERNAL,
|
||||
|
@ -195,6 +195,10 @@ export async function search(
|
|||
}
|
||||
} catch (err: any) {
|
||||
const msg = typeof err === "string" ? err : err.message
|
||||
if (err.status === 404 && err.message?.includes(SQLITE_DESIGN_DOC_ID)) {
|
||||
await sdk.tables.sqs.syncDefinition()
|
||||
return search(options, table)
|
||||
}
|
||||
throw new Error(`Unable to search by SQL - ${msg}`, { cause: err })
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,7 +15,9 @@ import {
|
|||
generateJunctionTableID,
|
||||
} from "../../../../db/utils"
|
||||
|
||||
const BASIC_SQLITE_DOC: SQLiteDefinition = {
|
||||
type PreSaveSQLiteDefinition = Omit<SQLiteDefinition, "_rev">
|
||||
|
||||
const BASIC_SQLITE_DOC: PreSaveSQLiteDefinition = {
|
||||
_id: SQLITE_DESIGN_DOC_ID,
|
||||
language: "sqlite",
|
||||
sql: {
|
||||
|
@ -102,7 +104,7 @@ function mapTable(table: Table): SQLiteTables {
|
|||
}
|
||||
|
||||
// nothing exists, need to iterate though existing tables
|
||||
async function buildBaseDefinition(): Promise<SQLiteDefinition> {
|
||||
async function buildBaseDefinition(): Promise<PreSaveSQLiteDefinition> {
|
||||
const tables = await tablesSdk.getAllInternalTables()
|
||||
const definition = cloneDeep(BASIC_SQLITE_DOC)
|
||||
for (let table of tables) {
|
||||
|
@ -114,9 +116,15 @@ async function buildBaseDefinition(): Promise<SQLiteDefinition> {
|
|||
return definition
|
||||
}
|
||||
|
||||
export async function addTableToSqlite(table: Table) {
|
||||
export async function syncDefinition(): Promise<void> {
|
||||
const db = context.getAppDB()
|
||||
let definition: SQLiteDefinition
|
||||
const definition = await buildBaseDefinition()
|
||||
await db.put(definition)
|
||||
}
|
||||
|
||||
export async function addTable(table: Table) {
|
||||
const db = context.getAppDB()
|
||||
let definition: PreSaveSQLiteDefinition | SQLiteDefinition
|
||||
try {
|
||||
definition = await db.get<SQLiteDefinition>(SQLITE_DESIGN_DOC_ID)
|
||||
} catch (err) {
|
||||
|
@ -129,7 +137,7 @@ export async function addTableToSqlite(table: Table) {
|
|||
await db.put(definition)
|
||||
}
|
||||
|
||||
export async function removeTableFromSqlite(table: Table) {
|
||||
export async function removeTable(table: Table) {
|
||||
const db = context.getAppDB()
|
||||
try {
|
||||
const definition = await db.get<SQLiteDefinition>(SQLITE_DESIGN_DOC_ID)
|
||||
|
@ -147,3 +155,18 @@ export async function removeTableFromSqlite(table: Table) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function cleanupApp() {
|
||||
const db = context.getAppDB()
|
||||
if (!(await db.exists())) {
|
||||
throw new Error("Cleanup must be preformed before app deletion.")
|
||||
}
|
||||
try {
|
||||
const definition = await db.get<SQLiteDefinition>(SQLITE_DESIGN_DOC_ID)
|
||||
// delete the design document
|
||||
await db.remove(SQLITE_DESIGN_DOC_ID, definition._rev)
|
||||
await db.sqlCleanup()
|
||||
} catch (err: any) {
|
||||
throw new Error(`Unable to cleanup SQS files - ${err.message}`)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,6 +20,7 @@ export type SQLiteTables = Record<
|
|||
|
||||
export interface SQLiteDefinition {
|
||||
_id: string
|
||||
_rev: string
|
||||
language: string
|
||||
sql: {
|
||||
tables: SQLiteTables
|
||||
|
|
|
@ -135,10 +135,8 @@ export interface Database {
|
|||
ids: string[],
|
||||
opts?: { allowMissing?: boolean }
|
||||
): Promise<T[]>
|
||||
remove(
|
||||
id: string | Document,
|
||||
rev?: string
|
||||
): Promise<Nano.DocumentDestroyResponse>
|
||||
remove(idOrDoc: Document): Promise<Nano.DocumentDestroyResponse>
|
||||
remove(idOrDoc: string, rev: string): Promise<Nano.DocumentDestroyResponse>
|
||||
put(
|
||||
document: AnyDocument,
|
||||
opts?: DatabasePutOpts
|
||||
|
|
Loading…
Reference in New Issue