From 957c2ba70312e0ce0c362478fe64baeac79ff62e Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Thu, 26 May 2022 16:39:47 +0100 Subject: [PATCH] Updating migration name. --- .../functions/{quotas1.ts => quotas2.ts} | 4 +-- .../functions/tests/quotas1.spec.js | 2 +- .../functions/usageQuotas/syncAppRows.ts | 28 +++++++++++++++++++ .../functions/usageQuotas/syncRows.ts | 16 ++--------- packages/server/src/migrations/index.ts | 6 ++-- 5 files changed, 37 insertions(+), 19 deletions(-) rename packages/server/src/migrations/functions/{quotas1.ts => quotas2.ts} (84%) create mode 100644 packages/server/src/migrations/functions/usageQuotas/syncAppRows.ts diff --git a/packages/server/src/migrations/functions/quotas1.ts b/packages/server/src/migrations/functions/quotas2.ts similarity index 84% rename from packages/server/src/migrations/functions/quotas1.ts rename to packages/server/src/migrations/functions/quotas2.ts index 500aa68f51..457a23840a 100644 --- a/packages/server/src/migrations/functions/quotas1.ts +++ b/packages/server/src/migrations/functions/quotas2.ts @@ -1,6 +1,6 @@ import { runQuotaMigration } from "./usageQuotas" import * as syncApps from "./usageQuotas/syncApps" -import * as syncRows from "./usageQuotas/syncRows" +import * as syncAppRows from "./usageQuotas/syncAppRows" /** * Date: @@ -15,6 +15,6 @@ import * as syncRows from "./usageQuotas/syncRows" export const run = async () => { await runQuotaMigration(async () => { await syncApps.run() - await syncRows.run() + await syncAppRows.run() }) } diff --git a/packages/server/src/migrations/functions/tests/quotas1.spec.js b/packages/server/src/migrations/functions/tests/quotas1.spec.js index 693c40ab16..64b186ab92 100644 --- a/packages/server/src/migrations/functions/tests/quotas1.spec.js +++ b/packages/server/src/migrations/functions/tests/quotas1.spec.js @@ -6,7 +6,7 @@ const syncRows = jest.fn() jest.mock("../usageQuotas/syncApps", () => ({ run: syncApps }) ) jest.mock("../usageQuotas/syncRows", () => ({ run: syncRows }) ) -const migration = require("../quotas1") +const migration = require("../quotas2") describe("run", () => { let config = new TestConfig(false) diff --git a/packages/server/src/migrations/functions/usageQuotas/syncAppRows.ts b/packages/server/src/migrations/functions/usageQuotas/syncAppRows.ts new file mode 100644 index 0000000000..a15a498920 --- /dev/null +++ b/packages/server/src/migrations/functions/usageQuotas/syncAppRows.ts @@ -0,0 +1,28 @@ +import { getTenantId } from "@budibase/backend-core/tenancy" +import { getAllApps } from "@budibase/backend-core/db" +import { getUniqueRows } from "../../../utilities/usageQuota/rows" +import { quotas, QuotaUsageType, StaticQuotaName } from "@budibase/pro" + +export const run = async () => { + // get all rows in all apps + // @ts-ignore + const allApps = await getAllApps({ all: true }) + // @ts-ignore + const appIds = allApps ? allApps.map((app: { appId: any }) => app.appId) : [] + const { appRows } = await getUniqueRows(appIds) + const counts: { [key: string]: number } = {} + let rowCount = 0 + Object.entries(appRows).forEach(([appId, rows]) => { + counts[appId] = rows.length + rowCount += rows.length + }) + + // sync row count + const tenantId = getTenantId() + console.log(`[Tenant: ${tenantId}] Syncing row count: ${rowCount}`) + await quotas.setUsagePerApp( + counts, + StaticQuotaName.ROWS, + QuotaUsageType.STATIC + ) +} diff --git a/packages/server/src/migrations/functions/usageQuotas/syncRows.ts b/packages/server/src/migrations/functions/usageQuotas/syncRows.ts index ad234f9c85..c7cc13737b 100644 --- a/packages/server/src/migrations/functions/usageQuotas/syncRows.ts +++ b/packages/server/src/migrations/functions/usageQuotas/syncRows.ts @@ -2,7 +2,6 @@ import { getTenantId } from "@budibase/backend-core/tenancy" import { getAllApps } from "@budibase/backend-core/db" import { getUniqueRows } from "../../../utilities/usageQuota/rows" import { quotas, QuotaUsageType, StaticQuotaName } from "@budibase/pro" -import { setUsagePerApp } from "../../../../../../../budibase-pro/packages/pro/src/db/quotas" export const run = async () => { // get all rows in all apps @@ -10,20 +9,11 @@ export const run = async () => { const allApps = await getAllApps({ all: true }) // @ts-ignore const appIds = allApps ? allApps.map((app: { appId: any }) => app.appId) : [] - const { appRows } = await getUniqueRows(appIds) - const counts: { [key: string]: number } = {} - let rowCount = 0 - Object.entries(appRows).forEach(([appId, rows]) => { - counts[appId] = rows.length - rowCount += rows.length - }) + 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}`) - await quotas.setUsagePerApp( - counts, - StaticQuotaName.ROWS, - QuotaUsageType.STATIC - ) + await quotas.setUsage(rowCount, StaticQuotaName.ROWS, QuotaUsageType.STATIC) } diff --git a/packages/server/src/migrations/index.ts b/packages/server/src/migrations/index.ts index 6d63d81544..4d1ef7f5ca 100644 --- a/packages/server/src/migrations/index.ts +++ b/packages/server/src/migrations/index.ts @@ -5,7 +5,7 @@ const { // migration functions import * as userEmailViewCasing from "./functions/userEmailViewCasing" -import * as quota1 from "./functions/quotas1" +import * as quota2 from "./functions/quotas2" import * as appUrls from "./functions/appUrls" import * as developerQuota from "./functions/developerQuota" import * as publishedAppsQuota from "./functions/publishedAppsQuota" @@ -41,8 +41,8 @@ export const MIGRATIONS: Migration[] = [ }, { type: MIGRATION_TYPES.GLOBAL, - name: "quotas_1", - fn: quota1.run, + name: "quotas_2", + fn: quota2.run, }, { type: MIGRATION_TYPES.APP,