1
0
Fork 0
mirror of synced 2024-06-14 08:24:48 +12:00

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,
name: MigrationName.QUOTAS_1,
name: MigrationName.SYNC_QUOTAS,
},
{
type: MigrationType.APP,
@ -33,8 +33,4 @@ export const DEFINITIONS: MigrationDefinition[] = [
type: MigrationType.GLOBAL,
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 () => {
// get app count
// @ts-ignore
const devApps = await getAllApps({ dev: true })
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 { getUniqueRows } from "../../../utilities/usageQuota/rows"
import { quotas } from "@budibase/pro"
import { QuotaUsageType, StaticQuotaName } from "@budibase/types"
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 { rows } = await getUniqueRows(appIds)
const rowCount = rows ? rows.length : 0
const { appRows } = await getUniqueRows(appIds)
// 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
const tenantId = getTenantId()
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
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 backfill from "./functions/backfill"
import * as pluginCount from "./functions/pluginCount"
/**
* Populate the migration function and additional configuration from
* the static migration definitions.
@ -26,10 +24,10 @@ export const buildMigrations = () => {
})
break
}
case MigrationName.QUOTAS_1: {
case MigrationName.SYNC_QUOTAS: {
serverMigrations.push({
...definition,
fn: quota2.run,
fn: syncQuotas.run,
})
break
}
@ -69,16 +67,6 @@ export const buildMigrations = () => {
})
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,
DocumentType,
context,
db,
} from "@budibase/backend-core"
import TestConfig from "../../tests/utilities/TestConfiguration"
import structures from "../../tests/utilities/structures"

View file

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