Create generic quota sync migration

This commit is contained in:
Rory Powell 2022-09-28 13:13:37 +01:00
parent 79d53042d8
commit 61aafa87cb
11 changed files with 46 additions and 92 deletions

View File

@ -11,7 +11,7 @@ export const DEFINITIONS: MigrationDefinition[] = [
}, },
{ {
type: MigrationType.GLOBAL, type: MigrationType.GLOBAL,
name: MigrationName.QUOTAS_1, name: MigrationName.SYNC_QUOTAS,
}, },
{ {
type: MigrationType.APP, type: MigrationType.APP,
@ -33,8 +33,4 @@ export const DEFINITIONS: MigrationDefinition[] = [
type: MigrationType.GLOBAL, type: MigrationType.GLOBAL,
name: MigrationName.GLOBAL_INFO_SYNC_USERS, name: MigrationName.GLOBAL_INFO_SYNC_USERS,
}, },
{
type: MigrationType.GLOBAL,
name: MigrationName.PLUGIN_COUNT,
},
] ]

View File

@ -1,12 +0,0 @@
import { tenancy, logging } from "@budibase/backend-core"
import { plugins } from "@budibase/pro"
export const run = async () => {
try {
await tenancy.doInTenant(tenancy.DEFAULT_TENANT_ID, async () => {
await plugins.checkPluginQuotas()
})
} catch (err) {
logging.logAlert("Failed to update plugin quotas", err)
}
}

View File

@ -1,20 +0,0 @@
import { runQuotaMigration } from "./usageQuotas"
import * as syncApps from "./usageQuotas/syncApps"
import * as syncAppRows from "./usageQuotas/syncAppRows"
/**
* Date:
* January 2022
*
* Description:
* Synchronise the app and row quotas to the state of the db after it was
* discovered that the quota resets were still in place and the row quotas
* weren't being decremented correctly.
*/
export const run = async () => {
await runQuotaMigration(async () => {
await syncApps.run()
await syncAppRows.run()
})
}

View File

@ -0,0 +1,15 @@
import { runQuotaMigration } from "./usageQuotas"
import * as syncApps from "./usageQuotas/syncApps"
import * as syncRows from "./usageQuotas/syncRows"
import * as syncPlugins from "./usageQuotas/syncPlugins"
/**
* Synchronise quotas to the state of the db.
*/
export const run = async () => {
await runQuotaMigration(async () => {
await syncApps.run()
await syncRows.run()
await syncPlugins.run()
})
}

View File

@ -1,29 +0,0 @@
import { getTenantId } from "@budibase/backend-core/tenancy"
import { getAllApps } from "@budibase/backend-core/db"
import { getUniqueRows } from "../../../utilities/usageQuota/rows"
import { quotas } from "@budibase/pro"
import { StaticQuotaName, QuotaUsageType } from "@budibase/types"
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
)
}

View File

@ -5,7 +5,6 @@ import { QuotaUsageType, StaticQuotaName } from "@budibase/types"
export const run = async () => { export const run = async () => {
// get app count // get app count
// @ts-ignore
const devApps = await getAllApps({ dev: true }) const devApps = await getAllApps({ dev: true })
const appCount = devApps ? devApps.length : 0 const appCount = devApps ? devApps.length : 0

View File

@ -0,0 +1,10 @@
import { logging } from "@budibase/backend-core"
import { plugins } from "@budibase/pro"
export const run = async () => {
try {
await plugins.checkPluginQuotas()
} catch (err) {
logging.logAlert("Failed to update plugin quotas", err)
}
}

View File

@ -2,19 +2,28 @@ import { getTenantId } from "@budibase/backend-core/tenancy"
import { getAllApps } from "@budibase/backend-core/db" import { getAllApps } from "@budibase/backend-core/db"
import { getUniqueRows } from "../../../utilities/usageQuota/rows" import { getUniqueRows } from "../../../utilities/usageQuota/rows"
import { quotas } from "@budibase/pro" import { quotas } from "@budibase/pro"
import { QuotaUsageType, StaticQuotaName } from "@budibase/types" import { StaticQuotaName, QuotaUsageType } from "@budibase/types"
export const run = async () => { export const run = async () => {
// get all rows in all apps // get all rows in all apps
// @ts-ignore
const allApps = await getAllApps({ all: true }) const allApps = await getAllApps({ all: true })
// @ts-ignore
const appIds = allApps ? allApps.map((app: { appId: any }) => app.appId) : [] const appIds = allApps ? allApps.map((app: { appId: any }) => app.appId) : []
const { rows } = await getUniqueRows(appIds) const { appRows } = await getUniqueRows(appIds)
const rowCount = rows ? rows.length : 0
// get the counts per app
const counts: { [key: string]: number } = {}
let rowCount = 0
Object.entries(appRows).forEach(([appId, rows]) => {
counts[appId] = rows.length
rowCount += rows.length
})
// sync row count // sync row count
const tenantId = getTenantId() const tenantId = getTenantId()
console.log(`[Tenant: ${tenantId}] Syncing row count: ${rowCount}`) console.log(`[Tenant: ${tenantId}] Syncing row count: ${rowCount}`)
await quotas.setUsage(rowCount, StaticQuotaName.ROWS, QuotaUsageType.STATIC) await quotas.setUsagePerApp(
counts,
StaticQuotaName.ROWS,
QuotaUsageType.STATIC
)
} }

View File

@ -4,11 +4,9 @@ import env from "../environment"
// migration functions // migration functions
import * as userEmailViewCasing from "./functions/userEmailViewCasing" import * as userEmailViewCasing from "./functions/userEmailViewCasing"
import * as quota2 from "./functions/quotas2" import * as syncQuotas from "./functions/syncQuotas"
import * as appUrls from "./functions/appUrls" import * as appUrls from "./functions/appUrls"
import * as backfill from "./functions/backfill" import * as backfill from "./functions/backfill"
import * as pluginCount from "./functions/pluginCount"
/** /**
* Populate the migration function and additional configuration from * Populate the migration function and additional configuration from
* the static migration definitions. * the static migration definitions.
@ -26,10 +24,10 @@ export const buildMigrations = () => {
}) })
break break
} }
case MigrationName.QUOTAS_1: { case MigrationName.SYNC_QUOTAS: {
serverMigrations.push({ serverMigrations.push({
...definition, ...definition,
fn: quota2.run, fn: syncQuotas.run,
}) })
break break
} }
@ -69,16 +67,6 @@ export const buildMigrations = () => {
}) })
break break
} }
case MigrationName.PLUGIN_COUNT: {
if (env.SELF_HOSTED) {
serverMigrations.push({
...definition,
fn: pluginCount.run,
silent: !!env.SELF_HOSTED,
preventRetry: false,
})
}
}
} }
} }

View File

@ -4,7 +4,6 @@ import {
tenancy, tenancy,
DocumentType, DocumentType,
context, context,
db,
} from "@budibase/backend-core" } from "@budibase/backend-core"
import TestConfig from "../../tests/utilities/TestConfiguration" import TestConfig from "../../tests/utilities/TestConfiguration"
import structures from "../../tests/utilities/structures" import structures from "../../tests/utilities/structures"

View File

@ -39,14 +39,13 @@ export interface MigrationOptions {
export enum MigrationName { export enum MigrationName {
USER_EMAIL_VIEW_CASING = "user_email_view_casing", USER_EMAIL_VIEW_CASING = "user_email_view_casing",
QUOTAS_1 = "quotas_1",
APP_URLS = "app_urls", APP_URLS = "app_urls",
EVENT_APP_BACKFILL = "event_app_backfill", EVENT_APP_BACKFILL = "event_app_backfill",
EVENT_GLOBAL_BACKFILL = "event_global_backfill", EVENT_GLOBAL_BACKFILL = "event_global_backfill",
EVENT_INSTALLATION_BACKFILL = "event_installation_backfill", EVENT_INSTALLATION_BACKFILL = "event_installation_backfill",
GLOBAL_INFO_SYNC_USERS = "global_info_sync_users", GLOBAL_INFO_SYNC_USERS = "global_info_sync_users",
PLATFORM_USERS_EMAIL_CASING = "platform_users_email_casing", // increment this number to re-activate this migration
PLUGIN_COUNT = "plugin_count", SYNC_QUOTAS = "sync_quotas_1",
} }
export interface MigrationDefinition { export interface MigrationDefinition {