Adding implementation to DB for purge and cleanup APIs of SQS, to make sure the DB is cleared of any unused tables or rows.
This commit is contained in:
parent
a2893ec2cc
commit
8189952f0b
|
@ -249,25 +249,53 @@ export class DatabaseImpl implements Database {
|
|||
})
|
||||
}
|
||||
|
||||
async _sqlQuery<T>(
|
||||
url: string,
|
||||
method: "POST" | "GET",
|
||||
body?: any
|
||||
): Promise<T> {
|
||||
const args: { url: string; method: string; cookie: string; body?: any } = {
|
||||
url: `${this.couchInfo.sqlUrl}/${url}`,
|
||||
method,
|
||||
cookie: this.couchInfo.cookie,
|
||||
}
|
||||
if (body) {
|
||||
args.body = body
|
||||
}
|
||||
const response = await directCouchUrlCall(body)
|
||||
if (response.status > 300) {
|
||||
throw new Error(await response.text())
|
||||
}
|
||||
return (await response.json()) as T
|
||||
}
|
||||
|
||||
async sql<T extends Document>(
|
||||
sql: string,
|
||||
parameters?: SqlQueryBinding
|
||||
): Promise<T[]> {
|
||||
const dbName = this.name
|
||||
const url = `/${dbName}/${SQLITE_DESIGN_DOC_ID}`
|
||||
const response = await directCouchUrlCall({
|
||||
url: `${this.couchInfo.sqlUrl}/${url}`,
|
||||
method: "POST",
|
||||
cookie: this.couchInfo.cookie,
|
||||
body: {
|
||||
query: sql,
|
||||
args: parameters,
|
||||
},
|
||||
return await this._sqlQuery<T[]>(url, "POST", {
|
||||
query: sql,
|
||||
args: parameters,
|
||||
})
|
||||
if (response.status > 300) {
|
||||
throw new Error(await response.text())
|
||||
}
|
||||
|
||||
// checks design document is accurate (cleans up tables)
|
||||
async sqlCleanup(): Promise<void> {
|
||||
const dbName = this.name
|
||||
const url = `/${dbName}/_cleanup`
|
||||
return await this._sqlQuery<void>(url, "POST")
|
||||
}
|
||||
|
||||
// removes a document from sqlite
|
||||
async sqlPurge(docIds: string[] | string): Promise<void> {
|
||||
if (!Array.isArray(docIds)) {
|
||||
docIds = [docIds]
|
||||
}
|
||||
return (await response.json()) as T[]
|
||||
const dbName = this.name
|
||||
const url = `/${dbName}/_purge`
|
||||
return await this._sqlQuery<void>(url, "POST", { docs: docIds })
|
||||
}
|
||||
|
||||
async query<T extends Document>(
|
||||
|
|
|
@ -160,4 +160,18 @@ export class DDInstrumentedDatabase implements Database {
|
|||
return this.db.sql(sql, parameters)
|
||||
})
|
||||
}
|
||||
|
||||
sqlPurge(docIds: string[] | string): Promise<void> {
|
||||
return tracer.trace("db.sqlPurge", span => {
|
||||
span?.addTags({ db_name: this.name })
|
||||
return this.db.sqlPurge(docIds)
|
||||
})
|
||||
}
|
||||
|
||||
sqlCleanup(): Promise<void> {
|
||||
return tracer.trace("db.sqlCleanup", span => {
|
||||
span?.addTags({ db_name: this.name })
|
||||
return this.db.sqlCleanup()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -118,7 +118,7 @@ export async function addTableToSqlite(table: Table) {
|
|||
const db = context.getAppDB()
|
||||
let definition: SQLiteDefinition
|
||||
try {
|
||||
definition = await db.get(SQLITE_DESIGN_DOC_ID)
|
||||
definition = await db.get<SQLiteDefinition>(SQLITE_DESIGN_DOC_ID)
|
||||
} catch (err) {
|
||||
definition = await buildBaseDefinition()
|
||||
}
|
||||
|
@ -128,3 +128,22 @@ export async function addTableToSqlite(table: Table) {
|
|||
}
|
||||
await db.put(definition)
|
||||
}
|
||||
|
||||
export async function removeTableFromSqlite(table: Table) {
|
||||
const db = context.getAppDB()
|
||||
try {
|
||||
const definition = await db.get<SQLiteDefinition>(SQLITE_DESIGN_DOC_ID)
|
||||
if (definition.sql?.tables?.[table._id!]) {
|
||||
delete definition.sql.tables[table._id!]
|
||||
await db.put(definition)
|
||||
// make sure SQS is cleaned up, tables removed
|
||||
await db.sqlCleanup()
|
||||
}
|
||||
} catch (err: any) {
|
||||
if (err?.status === 404) {
|
||||
return
|
||||
} else {
|
||||
throw err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -148,6 +148,8 @@ export interface Database {
|
|||
sql: string,
|
||||
parameters?: SqlQueryBinding
|
||||
): Promise<T[]>
|
||||
sqlPurge(docIds: string[] | string): Promise<void>
|
||||
sqlCleanup(): Promise<void>
|
||||
allDocs<T extends Document | RowValue>(
|
||||
params: DatabaseQueryOpts
|
||||
): Promise<AllDocsResponse<T>>
|
||||
|
|
Loading…
Reference in New Issue