diff --git a/packages/backend-core/src/db/Replication.ts b/packages/backend-core/src/db/Replication.ts index 735c2fa86e..7271662f0f 100644 --- a/packages/backend-core/src/db/Replication.ts +++ b/packages/backend-core/src/db/Replication.ts @@ -1,14 +1,34 @@ import PouchDB from "pouchdb" import { getPouchDB, closePouchDB } from "./couch" -import { DocumentType } from "../constants" +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 constructor({ source, target }: { source: string; target: string }) { this.source = getPouchDB(source) this.target = getPouchDB(target) + if ( + source.startsWith(DocumentType.APP_DEV) && + target.startsWith(DocumentType.APP) + ) { + this.direction = ReplicationDirection.TO_PRODUCTION + } else if ( + source.startsWith(DocumentType.APP) && + target.startsWith(DocumentType.APP_DEV) + ) { + this.direction = ReplicationDirection.TO_DEV + } else { + this.direction = ReplicationDirection.UNKNOWN + } } async close() { @@ -40,12 +60,18 @@ class Replication { } const filter = opts.filter + const direction = this.direction + const toDev = direction === ReplicationDirection.TO_DEV delete opts.filter return { ...opts, filter: (doc: any, params: any) => { - if (doc._id && doc._id.startsWith(DocumentType.AUTOMATION_LOG)) { + // don't sync design documents + if (toDev && doc._id?.startsWith("_design")) { + return false + } + if (doc._id?.startsWith(DocumentType.AUTOMATION_LOG)) { return false } if (doc._id === DocumentType.APP_METADATA) { diff --git a/packages/server/src/api/controllers/application.ts b/packages/server/src/api/controllers/application.ts index 6d28be9d8d..d0908a995e 100644 --- a/packages/server/src/api/controllers/application.ts +++ b/packages/server/src/api/controllers/application.ts @@ -591,7 +591,7 @@ async function destroyApp(ctx: UserCtx) { async function preDestroyApp(ctx: UserCtx) { if (env.SQS_SEARCH_ENABLE) { - await sdk.tables.sqs.cleanupApp() + await sdk.tables.sqs.cleanupApp(ctx.params.appId) } const { rows } = await getUniqueRows([ctx.params.appId]) ctx.rowCount = rows.length diff --git a/packages/server/src/sdk/app/tables/internal/sqs.ts b/packages/server/src/sdk/app/tables/internal/sqs.ts index 3d868b446a..e6486d3339 100644 --- a/packages/server/src/sdk/app/tables/internal/sqs.ts +++ b/packages/server/src/sdk/app/tables/internal/sqs.ts @@ -1,4 +1,8 @@ -import { context, SQLITE_DESIGN_DOC_ID } from "@budibase/backend-core" +import { + context, + SQLITE_DESIGN_DOC_ID, + db as dbCore, +} from "@budibase/backend-core" import { FieldType, RelationshipFieldMetadata, @@ -156,8 +160,8 @@ export async function removeTable(table: Table) { } } -export async function cleanupApp() { - const db = context.getAppDB() +export async function cleanupApp(appId: string) { + const db = dbCore.getDB(appId) if (!(await db.exists())) { throw new Error("Cleanup must be preformed before app deletion.") }