diff --git a/packages/backend-core/src/migrations/index.js b/packages/backend-core/src/migrations/index.js index 4708776ec3..6b8eb3a95c 100644 --- a/packages/backend-core/src/migrations/index.js +++ b/packages/backend-core/src/migrations/index.js @@ -1,5 +1,5 @@ const { DocumentTypes } = require("../db/constants") -const { getGlobalDB } = require("../tenancy") +const { getGlobalDB, getTenantId } = require("../tenancy") exports.MIGRATION_DBS = { GLOBAL_DB: "GLOBAL_DB", @@ -29,6 +29,7 @@ exports.getMigrationsDoc = async db => { } exports.migrateIfRequired = async (migrationDb, migrationName, migrateFn) => { + const tenantId = getTenantId() try { let db if (migrationDb === exports.MIGRATION_DBS.GLOBAL_DB) { @@ -49,15 +50,18 @@ exports.migrateIfRequired = async (migrationDb, migrationName, migrateFn) => { return } - console.log(`Performing migration: ${migrationName}`) + console.log(`[Tenant: ${tenantId}] Performing migration: ${migrationName}`) await migrateFn() - console.log(`Migration complete: ${migrationName}`) + console.log(`[Tenant: ${tenantId}] Migration complete: ${migrationName}`) // mark as complete doc[migrationName] = Date.now() await db.put(doc) } catch (err) { - console.error(`Error performing migration: ${migrationName}: `, err) + console.error( + `[Tenant: ${tenantId}] Error performing migration: ${migrationName}: `, + err + ) throw err } } diff --git a/packages/server/src/middleware/usageQuota.js b/packages/server/src/middleware/usageQuota.js index 8e3daa6268..4bafa75132 100644 --- a/packages/server/src/middleware/usageQuota.js +++ b/packages/server/src/middleware/usageQuota.js @@ -142,12 +142,14 @@ const appPostDelete = async (ctx, usageContext) => { } } -// const appPostCreate = async (ctx, usageContext) => { -// if (ctx.request) { -// const rowCount = await getUniqueRows([ctx.appId]).length -// await usageQuota.update(usageQuota.Properties.ROW, -rowCount) -// } -// } +const appPostCreate = async ctx => { + // app import & template creation + if (ctx.request.body.useTemplate === "true") { + const rows = await getUniqueRows([ctx.response.body.appId]) + const rowCount = rows ? rows.length : 0 + await usageQuota.update(usageQuota.Properties.ROW, rowCount) + } +} const PRE_DELETE = { [usageQuota.Properties.APPS]: appPreDelete, @@ -160,5 +162,5 @@ const POST_DELETE = { const PRE_CREATE = {} const POST_CREATE = { - // [usageQuota.Properties.APPS]: appPostCreate, + [usageQuota.Properties.APPS]: appPostCreate, } diff --git a/packages/server/src/migrations/usageQuotas/syncApps.js b/packages/server/src/migrations/usageQuotas/syncApps.js index c285bafe4e..ee106129e6 100644 --- a/packages/server/src/migrations/usageQuotas/syncApps.js +++ b/packages/server/src/migrations/usageQuotas/syncApps.js @@ -1,4 +1,4 @@ -const { getGlobalDB } = require("@budibase/backend-core/tenancy") +const { getGlobalDB, getTenantId } = require("@budibase/backend-core/tenancy") const { getAllApps } = require("@budibase/backend-core/db") const CouchDB = require("../../db") const { getUsageQuotaDoc } = require("../../utilities/usageQuota") @@ -10,6 +10,8 @@ exports.run = async () => { const appCount = devApps ? devApps.length : 0 // sync app count + const tenantId = getTenantId() + console.log(`[Tenant: ${tenantId}] Syncing app count: ${appCount}`) const usageDoc = await getUsageQuotaDoc(db) usageDoc.usageQuota.apps = appCount await db.put(usageDoc) diff --git a/packages/server/src/migrations/usageQuotas/syncRows.js b/packages/server/src/migrations/usageQuotas/syncRows.js index 73f6f1477d..7990f405de 100644 --- a/packages/server/src/migrations/usageQuotas/syncRows.js +++ b/packages/server/src/migrations/usageQuotas/syncRows.js @@ -1,4 +1,4 @@ -const { getGlobalDB } = require("@budibase/backend-core/tenancy") +const { getGlobalDB, getTenantId } = require("@budibase/backend-core/tenancy") const { getAllApps } = require("@budibase/backend-core/db") const CouchDB = require("../../db") const { getUsageQuotaDoc } = require("../../utilities/usageQuota") @@ -10,9 +10,12 @@ exports.run = async () => { const allApps = await getAllApps(CouchDB, { all: true }) const appIds = allApps ? allApps.map(app => app.appId) : [] const rows = await getUniqueRows(appIds) + const rowCount = rows ? rows.length : 0 // sync row count + const tenantId = getTenantId() + console.log(`[Tenant: ${tenantId}] Syncing row count: ${rowCount}`) const usageDoc = await getUsageQuotaDoc(db) - usageDoc.usageQuota.rows = rows.length + usageDoc.usageQuota.rows = rowCount await db.put(usageDoc) } diff --git a/packages/server/src/utilities/tests/usageQuota/usageQuota.spec.js b/packages/server/src/utilities/tests/usageQuota/usageQuota.spec.js index 764d509b5e..dcd7578f59 100644 --- a/packages/server/src/utilities/tests/usageQuota/usageQuota.spec.js +++ b/packages/server/src/utilities/tests/usageQuota/usageQuota.spec.js @@ -2,7 +2,7 @@ const getTenantId = jest.fn() jest.mock("@budibase/backend-core/tenancy", () => ({ getTenantId })) -const usageQuota = require("../usageQuota") +const usageQuota = require("../../usageQuota") const env = require("../../../environment") class TestConfiguration { diff --git a/packages/server/src/utilities/usageQuota/rows.js b/packages/server/src/utilities/usageQuota/rows.js index 19c3e5b8bf..67ad07410d 100644 --- a/packages/server/src/utilities/usageQuota/rows.js +++ b/packages/server/src/utilities/usageQuota/rows.js @@ -59,6 +59,7 @@ exports.getUniqueRows = async appIds => { try { appRows.push(await getAppRows(appId)) } catch (e) { + console.error(e) // don't error out if we can't count the app rows, just continue } }