mirror of https://github.com/Budibase/budibase.git
10 changed files with 105 additions and 32 deletions
@ -0,0 +1,27 @@ |
|||
const { MIGRATIONS, MIGRATION_DBS, migrateIfRequired } = |
|||
require("@budibase/auth").migrations |
|||
const { getGlobalDB } = require("@budibase/auth/tenancy") |
|||
const { getUsageQuotaDoc } = require("../utilities/usageQuota") |
|||
const { getAllApps } = require("@budibase/auth/db") |
|||
const CouchDB = require("../db") |
|||
|
|||
exports.migrate = async () => { |
|||
await migrateIfRequired( |
|||
MIGRATION_DBS.GLOBAL_DB, |
|||
MIGRATIONS.SYNC_APP_AND_RESET_ROWS_QUOTAS, |
|||
async () => { |
|||
const globalDb = getGlobalDB() |
|||
const usageDoc = await getUsageQuotaDoc(globalDb) |
|||
|
|||
// reset the rows
|
|||
usageDoc.usageQuota.rows = 0 |
|||
|
|||
// sync the apps
|
|||
const apps = await getAllApps(CouchDB, { dev: true }) |
|||
const appCount = apps ? apps.length : 0 |
|||
usageDoc.usageQuota.apps = appCount |
|||
|
|||
await globalDb.put(usageDoc) |
|||
} |
|||
) |
|||
} |
|||
@ -0,0 +1,39 @@ |
|||
const { getGlobalDB } = require("@budibase/auth/tenancy") |
|||
const TestConfig = require("../../tests/utilities/TestConfiguration") |
|||
const { getUsageQuotaDoc, update, Properties } = require("../../utilities/usageQuota") |
|||
const { migrate } = require("../sync_app_and_reset_rows_quotas") |
|||
const env = require("../../environment") |
|||
|
|||
describe("Sync App And Reset Rows Quotas Migration", () => { |
|||
let config = new TestConfig(false) |
|||
|
|||
beforeEach(async () => { |
|||
await config.init() |
|||
env._set("USE_QUOTAS", 1) |
|||
}) |
|||
|
|||
afterAll(config.end) |
|||
|
|||
it("migrates successfully", async () => { |
|||
// create the usage quota doc and mock usages
|
|||
const db = getGlobalDB() |
|||
await getUsageQuotaDoc(db) |
|||
await update(Properties.APPS, 3) |
|||
await update(Properties.ROW, 300) |
|||
|
|||
let usageDoc = await getUsageQuotaDoc(db) |
|||
expect(usageDoc.usageQuota.apps).toEqual(3) |
|||
expect(usageDoc.usageQuota.rows).toEqual(300) |
|||
|
|||
// create an extra app to test the migration
|
|||
await config.createApp("quota-test") |
|||
|
|||
// migrate
|
|||
await migrate() |
|||
|
|||
// assert the migration worked
|
|||
usageDoc = await getUsageQuotaDoc(db) |
|||
expect(usageDoc.usageQuota.apps).toEqual(2) |
|||
expect(usageDoc.usageQuota.rows).toEqual(0) |
|||
}) |
|||
}) |
|||
@ -0,0 +1,18 @@ |
|||
// UNUSED CODE
|
|||
// Preserved for future use
|
|||
|
|||
/* eslint-disable no-unused-vars */ |
|||
|
|||
function getNewQuotaReset() { |
|||
return Date.now() + 2592000000 |
|||
} |
|||
|
|||
function resetQuotasIfRequired(quota) { |
|||
// Check if the quota needs reset
|
|||
if (Date.now() >= quota.quotaReset) { |
|||
quota.quotaReset = getNewQuotaReset() |
|||
for (let prop of Object.keys(quota.usageQuota)) { |
|||
quota.usageQuota[prop] = 0 |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue