From 06d6d84b556d8070d4c524ce6b6051e23413e22f Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Thu, 16 May 2024 13:27:54 +0100 Subject: [PATCH] Addressing PR comments. --- packages/backend-core/src/db/Replication.ts | 5 +--- .../backend-core/src/db/couch/DatabaseImpl.ts | 8 +++-- .../backend-core/src/db/instrumentation.ts | 29 +++++++++---------- .../server/src/sdk/app/tables/internal/sqs.ts | 2 +- packages/types/src/sdk/db.ts | 4 +-- 5 files changed, 22 insertions(+), 26 deletions(-) diff --git a/packages/backend-core/src/db/Replication.ts b/packages/backend-core/src/db/Replication.ts index 7271662f0f..617269df10 100644 --- a/packages/backend-core/src/db/Replication.ts +++ b/packages/backend-core/src/db/Replication.ts @@ -5,13 +5,12 @@ import { DocumentType } from "@budibase/types" enum ReplicationDirection { TO_PRODUCTION = "toProduction", TO_DEV = "toDev", - UNKNOWN = "unknown", } class Replication { source: PouchDB.Database target: PouchDB.Database - direction: ReplicationDirection + direction: ReplicationDirection | undefined constructor({ source, target }: { source: string; target: string }) { this.source = getPouchDB(source) @@ -26,8 +25,6 @@ class Replication { target.startsWith(DocumentType.APP_DEV) ) { this.direction = ReplicationDirection.TO_DEV - } else { - this.direction = ReplicationDirection.UNKNOWN } } diff --git a/packages/backend-core/src/db/couch/DatabaseImpl.ts b/packages/backend-core/src/db/couch/DatabaseImpl.ts index 711ff8b127..8194d1aabf 100644 --- a/packages/backend-core/src/db/couch/DatabaseImpl.ts +++ b/packages/backend-core/src/db/couch/DatabaseImpl.ts @@ -323,14 +323,16 @@ export class DatabaseImpl implements Database { } // checks design document is accurate (cleans up tables) - async sqlCleanup(): Promise { + // this will check the design document and remove anything from + // disk which is not supposed to be there + async sqlDiskCleanup(): Promise { const dbName = this.name const url = `/${dbName}/_cleanup` return await this._sqlQuery(url, "POST") } // removes a document from sqlite - async sqlPurge(docIds: string[] | string): Promise { + async sqlPurgeDocument(docIds: string[] | string): Promise { if (!Array.isArray(docIds)) { docIds = [docIds] } @@ -359,7 +361,7 @@ export class DatabaseImpl implements Database { ) await this.remove(SQLITE_DESIGN_DOC_ID, definition._rev) } finally { - await this.sqlCleanup() + await this.sqlDiskCleanup() } } return await this.nano().db.destroy(this.name) diff --git a/packages/backend-core/src/db/instrumentation.ts b/packages/backend-core/src/db/instrumentation.ts index ef1c912e0b..dc0fd67b37 100644 --- a/packages/backend-core/src/db/instrumentation.ts +++ b/packages/backend-core/src/db/instrumentation.ts @@ -56,19 +56,16 @@ export class DDInstrumentedDatabase implements Database { }) } + remove(idOrDoc: Document): Promise + remove(idOrDoc: string, rev?: string): Promise remove( - id: string | Document, - rev?: string | undefined + idOrDoc: string | Document, + rev?: string ): Promise { return tracer.trace("db.remove", span => { - span?.addTags({ db_name: this.name, doc_id: id }) - 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") - } + span?.addTags({ db_name: this.name, doc_id: idOrDoc }) + const id: string = typeof idOrDoc === "object" ? idOrDoc._id! : idOrDoc + return this.db.remove(id, rev) }) } @@ -167,17 +164,17 @@ export class DDInstrumentedDatabase implements Database { }) } - sqlPurge(docIds: string[] | string): Promise { - return tracer.trace("db.sqlPurge", span => { + sqlPurgeDocument(docIds: string[] | string): Promise { + return tracer.trace("db.sqlPurgeDocument", span => { span?.addTags({ db_name: this.name }) - return this.db.sqlPurge(docIds) + return this.db.sqlPurgeDocument(docIds) }) } - sqlCleanup(): Promise { - return tracer.trace("db.sqlCleanup", span => { + sqlDiskCleanup(): Promise { + return tracer.trace("db.sqlDiskCleanup", span => { span?.addTags({ db_name: this.name }) - return this.db.sqlCleanup() + return this.db.sqlDiskCleanup() }) } } diff --git a/packages/server/src/sdk/app/tables/internal/sqs.ts b/packages/server/src/sdk/app/tables/internal/sqs.ts index 6e326f896f..5ecfd9692e 100644 --- a/packages/server/src/sdk/app/tables/internal/sqs.ts +++ b/packages/server/src/sdk/app/tables/internal/sqs.ts @@ -146,7 +146,7 @@ export async function removeTable(table: Table) { delete definition.sql.tables[table._id!] await db.put(definition) // make sure SQS is cleaned up, tables removed - await db.sqlCleanup() + await db.sqlDiskCleanup() } } catch (err: any) { if (err?.status === 404) { diff --git a/packages/types/src/sdk/db.ts b/packages/types/src/sdk/db.ts index 4a4343e59f..7ad740ad05 100644 --- a/packages/types/src/sdk/db.ts +++ b/packages/types/src/sdk/db.ts @@ -146,8 +146,8 @@ export interface Database { sql: string, parameters?: SqlQueryBinding ): Promise - sqlPurge(docIds: string[] | string): Promise - sqlCleanup(): Promise + sqlPurgeDocument(docIds: string[] | string): Promise + sqlDiskCleanup(): Promise allDocs( params: DatabaseQueryOpts ): Promise>