Fixing an issue with cleanup, making sure the correct app is cleaned up on deletion.

This commit is contained in:
mike12345567 2024-05-09 18:33:29 +01:00
parent fd7f6455bd
commit a3d079f847
3 changed files with 36 additions and 6 deletions

View File

@ -1,14 +1,34 @@
import PouchDB from "pouchdb" import PouchDB from "pouchdb"
import { getPouchDB, closePouchDB } from "./couch" 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 { class Replication {
source: PouchDB.Database source: PouchDB.Database
target: PouchDB.Database target: PouchDB.Database
direction: ReplicationDirection
constructor({ source, target }: { source: string; target: string }) { constructor({ source, target }: { source: string; target: string }) {
this.source = getPouchDB(source) this.source = getPouchDB(source)
this.target = getPouchDB(target) 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() { async close() {
@ -40,12 +60,18 @@ class Replication {
} }
const filter = opts.filter const filter = opts.filter
const direction = this.direction
const toDev = direction === ReplicationDirection.TO_DEV
delete opts.filter delete opts.filter
return { return {
...opts, ...opts,
filter: (doc: any, params: any) => { 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 return false
} }
if (doc._id === DocumentType.APP_METADATA) { if (doc._id === DocumentType.APP_METADATA) {

View File

@ -591,7 +591,7 @@ async function destroyApp(ctx: UserCtx) {
async function preDestroyApp(ctx: UserCtx) { async function preDestroyApp(ctx: UserCtx) {
if (env.SQS_SEARCH_ENABLE) { 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]) const { rows } = await getUniqueRows([ctx.params.appId])
ctx.rowCount = rows.length ctx.rowCount = rows.length

View File

@ -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 { import {
FieldType, FieldType,
RelationshipFieldMetadata, RelationshipFieldMetadata,
@ -156,8 +160,8 @@ export async function removeTable(table: Table) {
} }
} }
export async function cleanupApp() { export async function cleanupApp(appId: string) {
const db = context.getAppDB() const db = dbCore.getDB(appId)
if (!(await db.exists())) { if (!(await db.exists())) {
throw new Error("Cleanup must be preformed before app deletion.") throw new Error("Cleanup must be preformed before app deletion.")
} }