Skip to content

The open alternative to Salesforce, designed for AI.

Drop usage limit meter compatibility in 2.47 (#27780)

Félix Malfaita7124ba433fe
diff --git a/packages/twenty-server/src/database/commands/upgrade-version-command/2-47/2-47-instance-command-fast-1791720497488-drop-usage-limit-meter.ts b/packages/twenty-server/src/database/commands/upgrade-version-command/2-47/2-47-instance-command-fast-1791720497488-drop-usage-limit-meter.ts
new file mode 100644
index 00000000000..e3bb8f9b7a1
--- /dev/null
+++ b/packages/twenty-server/src/database/commands/upgrade-version-command/2-47/2-47-instance-command-fast-1791720497488-drop-usage-limit-meter.ts
@@ -0,0 +1,67 @@
+import { type QueryRunner } from 'typeorm';
+
+import { ensureUsageLimitMeterCompatibility } from 'src/database/commands/upgrade-version-command/2-46/utils/ensure-usage-limit-meter-compatibility.util';
+import { RegisteredInstanceCommand } from 'src/engine/core-modules/upgrade/decorators/registered-instance-command.decorator';
+import { type FastInstanceCommand } from 'src/engine/core-modules/upgrade/interfaces/fast-instance-command.interface';
+import { WorkspaceCacheService } from 'src/engine/workspace-cache/services/workspace-cache.service';
+
+// 2.46 servers never read or write meter, so only 2.45 servers needed the
+// compatibility trigger. Quotas 2.46 could not map to a unit are neither
+// deleted nor guessed here: they must be reconciled before upgrading.
+@RegisteredInstanceCommand('2.47.0', 1791720497488)
+export class DropUsageLimitMeterFastInstanceCommand implements FastInstanceCommand {
+ constructor(private readonly workspaceCacheService: WorkspaceCacheService) {}
+
+ public async up(queryRunner: QueryRunner): Promise<void> {
+ const pendingUsageLimits: {
+ resourceType: string;
+ operationType: string;
+ meter: string;
+ count: string;
+ }[] = await queryRunner.query(
+ `SELECT "resourceType", "operationType", "meter", count(*) AS "count"
+ FROM "core"."usageLimit"
+ WHERE "unit" IS NULL
+ GROUP BY 1, 2, 3`,
+ );
+
+ if (pendingUsageLimits.length > 0) {
+ const inventory = pendingUsageLimits
+ .map(
+ ({ resourceType, operationType, meter, count }) =>
+ `${resourceType}/${operationType}/${meter}: ${count}`,
+ )
+ .join(', ');
+
+ throw new Error(
+ `core."usageLimit" still holds quotas without a unit (${inventory}). Replace each with an equivalent unit-based limit, then rerun the upgrade.`,
+ );
+ }
+
+ await queryRunner.query(
+ `DROP TRIGGER "syncUsageLimitMeterAndUnit" ON "core"."usageLimit"`,
+ );
+ await queryRunner.query(
+ `DROP FUNCTION "core"."syncUsageLimitMeterAndUnit"()`,
+ );
+ await queryRunner.query(`ALTER TABLE "core"."usageLimit" DROP COLUMN "meter"`);
+ await queryRunner.query(
+ `ALTER TABLE "core"."usageLimit" ALTER COLUMN "unit" SET NOT NULL`,
+ );
+
+ // 2.45 and 2.46 servers shared the usageLimits cache key, so an entry a
+ // 2.45 server computed can still hold limits without a unit. Flushing here
+ // rather than in a workspace command also covers run-instance-commands.
+ const workspaces: { workspaceId: string }[] = await queryRunner.query(
+ `SELECT DISTINCT "workspaceId" FROM "core"."usageLimit"`,
+ );
+
+ for (const { workspaceId } of workspaces) {
+ await this.workspaceCacheService.flush(workspaceId, ['usageLimits']);
+ }
+ }
+
+ public async down(queryRunner: QueryRunner): Promise<void> {
+ await ensureUsageLimitMeterCompatibility(queryRunner);
+ }
+}
diff --git a/packages/twenty-server/src/database/commands/upgrade-version-command/instance-commands.constant.ts b/packages/twenty-server/src/database/commands/upgrade-version-command/instance-commands.constant.ts
index c53476f2f49..3ee3021a4a8 100644
--- a/packages/twenty-server/src/database/commands/upgrade-version-command/instance-commands.constant.ts
+++ b/packages/twenty-server/src/database/commands/upgrade-version-command/instance-commands.constant.ts
@@ -238,6 +238,7 @@ import { MoveAgentRunSuspensionsToPendingWakeUpsSlowInstanceCommand } from 'src/
import { RestoreAgentEvaluationInputsFastInstanceCommand } from 'src/database/commands/upgrade-version-command/2-46/2-46-instance-command-fast-1791464994704-restore-agent-evaluation-inputs';
import { AddPurchaseToBillingCreditGrantTypeFastInstanceCommand } from 'src/database/commands/upgrade-version-command/2-46/2-46-instance-command-fast-1791470452911-add-purchase-to-billing-credit-grant-type';
import { RestoreUsageLimitMeterCompatibilityFastInstanceCommand } from 'src/database/commands/upgrade-version-command/2-46/2-46-instance-command-fast-1791538680877-restore-usage-limit-meter-compatibility';
+import { DropUsageLimitMeterFastInstanceCommand } from 'src/database/commands/upgrade-version-command/2-47/2-47-instance-command-fast-1791720497488-drop-usage-limit-meter';
export const INSTANCE_COMMANDS = [
AddViewFieldGroupIdIndexOnViewFieldFastInstanceCommand,
@@ -478,4 +479,5 @@ export const INSTANCE_COMMANDS = [
RestoreAgentEvaluationInputsFastInstanceCommand,
AddPurchaseToBillingCreditGrantTypeFastInstanceCommand,
RestoreUsageLimitMeterCompatibilityFastInstanceCommand,
+ DropUsageLimitMeterFastInstanceCommand,
];
diff --git a/packages/twenty-server/src/engine/core-modules/usage-limit/services/usage-limit.service.ts b/packages/twenty-server/src/engine/core-modules/usage-limit/services/usage-limit.service.ts
index 7e9da72a442..0cbf35d8bf1 100644
--- a/packages/twenty-server/src/engine/core-modules/usage-limit/services/usage-limit.service.ts
+++ b/packages/twenty-server/src/engine/core-modules/usage-limit/services/usage-limit.service.ts
@@ -1,6 +1,4 @@
import { Injectable } from '@nestjs/common';
-import { IsNull, Not } from 'typeorm';
-
import { isNonEmptyString } from '@sniptt/guards';
import { isDefined } from 'twenty-shared/utils';
@@ -58,10 +56,7 @@ export class UsageLimitService {
) {}
async findAll(workspaceId: string): Promise<UsageLimitEntity[]> {
- // Legacy quantity scopes without an equivalent unit stay stored for reconciliation.
- return this.usageLimitRepository.find(workspaceId, {
- where: { unit: Not(IsNull()) },
- });
+ return this.usageLimitRepository.find(workspaceId);
}
async create({
@@ -113,7 +108,7 @@ export class UsageLimitService {
isOperator: boolean;
}): Promise<UsageLimitEntity> {
const usageLimit = await this.usageLimitRepository.findOne(workspaceId, {
- where: { id: input.id, unit: Not(IsNull()) },
+ where: { id: input.id },
});
if (!isDefined(usageLimit)) {
@@ -261,7 +256,7 @@ export class UsageLimitService {
isOperator: boolean;
}): Promise<boolean> {
const usageLimit = await this.usageLimitRepository.findOne(workspaceId, {
- where: { id: usageLimitId, unit: Not(IsNull()) },
+ where: { id: usageLimitId },
});
if (!isDefined(usageLimit)) {
diff --git a/packages/twenty-server/src/engine/core-modules/usage-limit/services/usage-limits-cache.service.ts b/packages/twenty-server/src/engine/core-modules/usage-limit/services/usage-limits-cache.service.ts
index 4b8a8180a35..bbf136ac2ad 100644
--- a/packages/twenty-server/src/engine/core-modules/usage-limit/services/usage-limits-cache.service.ts
+++ b/packages/twenty-server/src/engine/core-modules/usage-limit/services/usage-limits-cache.service.ts
@@ -1,5 +1,4 @@
import { Injectable } from '@nestjs/common';
-import { IsNull, Not } from 'typeorm';
import { WorkspaceCacheProvider } from 'src/engine/workspace-cache/interfaces/workspace-cache-provider.service';
@@ -10,8 +9,7 @@ import { type WorkspaceCacheProviderContext } from 'src/engine/workspace-cache/t
import { type WorkspaceCacheRowsRequirement } from 'src/engine/workspace-cache/types/workspace-cache-rows-requirement.type';
const USAGE_LIMITS_ROWS_REQUIREMENT = {
- // Unmapped legacy quantities must not enter unit-based counters.
- usageLimit: { columns: true, where: { unit: Not(IsNull()) } },
+ usageLimit: true,
} as const satisfies WorkspaceCacheRowsRequirement;
@Injectable()
diff --git a/packages/twenty-server/src/engine/core-modules/usage-limit/usage-limit.entity.ts b/packages/twenty-server/src/engine/core-modules/usage-limit/usage-limit.entity.ts
index faac36fb2fd..aefb53d5d51 100644
--- a/packages/twenty-server/src/engine/core-modules/usage-limit/usage-limit.entity.ts
+++ b/packages/twenty-server/src/engine/core-modules/usage-limit/usage-limit.entity.ts
@@ -53,12 +53,7 @@ export class UsageLimitEntity extends WorkspaceRelatedEntity {
@Column({ type: 'varchar' })
periodUnit: PeriodUnit;
- // Old pods still use meter; the compatibility trigger owns it until cleanup.
- @Column({ type: 'varchar', select: false, insert: false, update: false })
- meter: string;
-
- // Unmapped legacy quotas stay stored but are excluded from unit-based reads.
- @Column({ type: 'varchar', nullable: true })
+ @Column({ type: 'varchar' })
unit: UsageUnit;
@Column({ type: 'bigint', transformer: nullableBigintColumnTransformer })
diff --git a/packages/twenty-server/test/integration/upgrade/suites/2-46-instance-command-fast-1791186790123-rename-usage-limit-meter-to-unit.integration-spec.ts b/packages/twenty-server/test/integration/upgrade/suites/2-46-instance-command-fast-1791186790123-rename-usage-limit-meter-to-unit.integration-spec.ts
index 4db3e122434..fd8429ee11a 100644
--- a/packages/twenty-server/test/integration/upgrade/suites/2-46-instance-command-fast-1791186790123-rename-usage-limit-meter-to-unit.integration-spec.ts
+++ b/packages/twenty-server/test/integration/upgrade/suites/2-46-instance-command-fast-1791186790123-rename-usage-limit-meter-to-unit.integration-spec.ts
@@ -1,16 +1,11 @@
import { type QueryRunner } from 'typeorm';
import { v4 } from 'uuid';
-import { getCoreRepository } from 'test/integration/utils/get-core-repository.util';
-
import { RenameUsageLimitMeterToUnitFastInstanceCommand } from 'src/database/commands/upgrade-version-command/2-46/2-46-instance-command-fast-1791186790123-rename-usage-limit-meter-to-unit';
import { RestoreUsageLimitMeterCompatibilityFastInstanceCommand } from 'src/database/commands/upgrade-version-command/2-46/2-46-instance-command-fast-1791538680877-restore-usage-limit-meter-compatibility';
-import { UsageLimitsCacheService } from 'src/engine/core-modules/usage-limit/services/usage-limits-cache.service';
-import { UsageLimitEntity } from 'src/engine/core-modules/usage-limit/usage-limit.entity';
import { UsageOperationType } from 'src/engine/core-modules/usage/enums/usage-operation-type.enum';
import { UsageResourceType } from 'src/engine/core-modules/usage/enums/usage-resource-type.enum';
import { UsageUnit } from 'src/engine/core-modules/usage/enums/usage-unit.enum';
-import { WorkspaceCacheRowsBatchLoader } from 'src/engine/workspace-cache/services/workspace-cache-rows-batch-loader';
import { SEED_APPLE_WORKSPACE_ID } from 'src/engine/workspace-manager/dev-seeder/core/constants/seeder-workspaces.constant';
jest.useRealTimers();
@@ -382,49 +377,6 @@ describe('2-46 fast instance command 1791186790123 - RenameUsageLimitMeterToUnit
});
});
- it('retains unmapped quotas in storage without loading them into enforcement', async () => {
- await command.up(queryRunner);
-
- const pendingId = await insertUsageLimit({
- resourceType: UsageResourceType.AI,
- operationType: UsageOperationType.ALL,
- limitKind: 'quota',
- column: 'meter',
- value: 'quantity',
- });
- const activeId = await insertUsageLimit({
- resourceType: UsageResourceType.AI,
- operationType: UsageOperationType.AI_CHAT_TOKEN,
- limitKind: 'quota',
- column: 'unit',
- value: UsageUnit.TOKEN,
- });
- const provider = new UsageLimitsCacheService();
- const usageLimitRepository = queryRunner.manager.withRepository(
- getCoreRepository<UsageLimitEntity>(UsageLimitEntity),
- );
- const loader = new WorkspaceCacheRowsBatchLoader(
- { getRepository: () => usageLimitRepository },
- SEED_APPLE_WORKSPACE_ID,
- );
-
- await loader.loadRows([provider.rowsRequirement]);
-
- const limits = provider.computeForCache({
- workspaceId: SEED_APPLE_WORKSPACE_ID,
- rows: loader.readRows(provider.rowsRequirement),
- });
- const fixtureLimits = (
- limits.byResourceType[UsageResourceType.AI] ?? []
- ).filter((limit) => limit.spenderId === spenderId);
-
- expect(fixtureLimits.map(({ id }) => id)).toEqual([activeId]);
- expect(await findColumnValues('meter')).toEqual({
- [pendingId]: 'quantity',
- [activeId]: 'quantity',
- });
- });
-
it('drops the limits no meter can hold and maps the rest back on down', async () => {
const insertCodeExecutionUsageLimit = (unit: UsageUnit) =>
insertUsageLimit({
diff --git a/packages/twenty-server/test/integration/upgrade/suites/2-47-instance-command-fast-1791720497488-drop-usage-limit-meter.integration-spec.ts b/packages/twenty-server/test/integration/upgrade/suites/2-47-instance-command-fast-1791720497488-drop-usage-limit-meter.integration-spec.ts
new file mode 100644
index 00000000000..1e532594177
--- /dev/null
+++ b/packages/twenty-server/test/integration/upgrade/suites/2-47-instance-command-fast-1791720497488-drop-usage-limit-meter.integration-spec.ts
@@ -0,0 +1,196 @@
+import { type QueryRunner } from 'typeorm';
+import { v4 } from 'uuid';
+
+import { DropUsageLimitMeterFastInstanceCommand } from 'src/database/commands/upgrade-version-command/2-47/2-47-instance-command-fast-1791720497488-drop-usage-limit-meter';
+import { UsageOperationType } from 'src/engine/core-modules/usage/enums/usage-operation-type.enum';
+import { UsageResourceType } from 'src/engine/core-modules/usage/enums/usage-resource-type.enum';
+import { UsageUnit } from 'src/engine/core-modules/usage/enums/usage-unit.enum';
+import { type WorkspaceCacheService } from 'src/engine/workspace-cache/services/workspace-cache.service';
+import { SEED_APPLE_WORKSPACE_ID } from 'src/engine/workspace-manager/dev-seeder/core/constants/seeder-workspaces.constant';
+
+jest.useRealTimers();
+
+describe('2-47 fast instance command 1791720497488 - DropUsageLimitMeterFastInstanceCommand (integration)', () => {
+ const flush = jest.fn();
+ const command = new DropUsageLimitMeterFastInstanceCommand({
+ flush,
+ } as unknown as WorkspaceCacheService);
+ let queryRunner: QueryRunner;
+ let spenderId: string;
+
+ const insertUsageLimit = async ({
+ resourceType,
+ operationType,
+ column,
+ value,
+ }: {
+ resourceType: UsageResourceType;
+ operationType: UsageOperationType;
+ column: 'meter' | 'unit';
+ value: string;
+ }): Promise<string> => {
+ const id = v4();
+
+ await queryRunner.query(
+ `INSERT INTO "core"."usageLimit" (
+ "id",
+ "workspaceId",
+ "resourceType",
+ "operationType",
+ "spenderType",
+ "spenderId",
+ "limitKind",
+ "periodCount",
+ "periodUnit",
+ "${column}",
+ "limitValue"
+ ) VALUES ($1, $2, $3, $4, 'application', $5, 'quota', 1, 'month', $6, 1000)`,
+ [
+ id,
+ SEED_APPLE_WORKSPACE_ID,
+ resourceType,
+ operationType,
+ spenderId,
+ value,
+ ],
+ );
+
+ return id;
+ };
+
+ const findColumnValues = async (
+ column: 'meter' | 'unit',
+ ): Promise<Record<string, string | null>> => {
+ const rows: { id: string; value: string | null }[] =
+ await queryRunner.query(
+ `SELECT "id", "${column}" AS "value"
+ FROM "core"."usageLimit"
+ WHERE "spenderId" = $1`,
+ [spenderId],
+ );
+
+ return Object.fromEntries(rows.map(({ id, value }) => [id, value]));
+ };
+
+ const findUsageLimitColumns = async (): Promise<
+ { columnName: string; isNullable: string }[]
+ > =>
+ queryRunner.query(
+ `SELECT column_name AS "columnName", is_nullable AS "isNullable"
+ FROM information_schema.columns
+ WHERE table_schema = 'core'
+ AND table_name = 'usageLimit'
+ AND column_name IN ('meter', 'unit') ORDER BY column_name`,
+ );
+
+ const findCompatibilityTriggerCount = async (): Promise<number> => {
+ const rows: unknown[] = await queryRunner.query(
+ `SELECT 1 FROM pg_trigger
+ WHERE tgrelid = '"core"."usageLimit"'::regclass
+ AND tgname = 'syncUsageLimitMeterAndUnit' AND NOT tgisinternal`,
+ );
+
+ return rows.length;
+ };
+
+ beforeEach(async () => {
+ flush.mockReset();
+ spenderId = v4();
+ queryRunner =
+ global.workflowTestServices.coreDataSource.createQueryRunner();
+ await queryRunner.connect();
+ await queryRunner.startTransaction();
+ await command.down(queryRunner);
+ });
+
+ afterEach(async () => {
+ await queryRunner.rollbackTransaction();
+ await queryRunner.release();
+ });
+
+ it('refuses to drop meter while a quota has no unit and keeps it stored', async () => {
+ const pendingId = await insertUsageLimit({
+ resourceType: UsageResourceType.AI,
+ operationType: UsageOperationType.ALL,
+ column: 'meter',
+ value: 'quantity',
+ });
+
+ await expect(command.up(queryRunner)).rejects.toThrow(
+ /quotas without a unit \(.*AI\/ALL\/quantity: \d+/,
+ );
+
+ expect(await findColumnValues('meter')).toEqual({
+ [pendingId]: 'quantity',
+ });
+ expect(await findCompatibilityTriggerCount()).toBe(1);
+ expect(flush).not.toHaveBeenCalled();
+ });
+
+ it('drops meter and its trigger, requires a unit and flushes usage limits caches once every quota has one', async () => {
+ const tokenId = await insertUsageLimit({
+ resourceType: UsageResourceType.AI,
+ operationType: UsageOperationType.AI_CHAT_TOKEN,
+ column: 'meter',
+ value: 'quantity',
+ });
+ const creditId = await insertUsageLimit({
+ resourceType: UsageResourceType.AI,
+ operationType: UsageOperationType.ALL,
+ column: 'unit',
+ value: UsageUnit.CREDIT,
+ });
+
+ await command.up(queryRunner);
+
+ expect(await findUsageLimitColumns()).toEqual([
+ { columnName: 'unit', isNullable: 'NO' },
+ ]);
+ expect(await findCompatibilityTriggerCount()).toBe(0);
+ expect(await findColumnValues('unit')).toEqual({
+ [tokenId]: UsageUnit.TOKEN,
+ [creditId]: UsageUnit.CREDIT,
+ });
+ expect(flush).toHaveBeenCalledWith(SEED_APPLE_WORKSPACE_ID, [
+ 'usageLimits',
+ ]);
+ await expect(
+ insertUsageLimit({
+ resourceType: UsageResourceType.WORKFLOW,
+ operationType: UsageOperationType.WORKFLOW_EXECUTION,
+ column: 'meter',
+ value: 'quantity',
+ }),
+ ).rejects.toThrow(/column "meter" .* does not exist/);
+ });
+
+ it('restores meter from unit on down and keeps both in sync again', async () => {
+ const byteId = await insertUsageLimit({
+ resourceType: UsageResourceType.STORAGE,
+ operationType: UsageOperationType.STORAGE_FILE,
+ column: 'unit',
+ value: UsageUnit.BYTE,
+ });
+
+ await command.up(queryRunner);
+ await command.down(queryRunner);
+
+ expect(await findUsageLimitColumns()).toEqual([
+ { columnName: 'meter', isNullable: 'NO' },
+ { columnName: 'unit', isNullable: 'YES' },
+ ]);
+ expect(await findCompatibilityTriggerCount()).toBe(1);
+ expect(await findColumnValues('meter')).toEqual({ [byteId]: 'bytes' });
+
+ const oldWriterId = await insertUsageLimit({
+ resourceType: UsageResourceType.WORKFLOW,
+ operationType: UsageOperationType.WORKFLOW_EXECUTION,
+ column: 'meter',
+ value: 'quantity',
+ });
+
+ expect((await findColumnValues('unit'))[oldWriterId]).toBe(
+ UsageUnit.INVOCATION,
+ );
+ });
+});