Skip to content

Commit

Permalink
Added feature flag for the v1 withTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
daneryl committed Feb 4, 2025
1 parent ebd8e58 commit 03f6dc0
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/api/entities/specs/routes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { legacyLogger } from 'api/log';
import templates from 'api/templates';
import thesauri from 'api/thesauri';
import { appContext } from 'api/utils/AppContext';
import { testingTenants } from 'api/utils/testingTenants';
import { UserInContextMockFactory } from 'api/utils/testingUserInContext';
import { ObjectId } from 'mongodb';
import path from 'path';
Expand Down Expand Up @@ -248,6 +249,7 @@ describe('entities routes', () => {
});

it('should run saveEntity process as a transaction', async () => {
testingTenants.changeCurrentTenant({ featureFlags: { v1_transactions: true } });
jest.restoreAllMocks();
jest.spyOn(entities, 'getUnrestrictedWithDocuments').mockImplementationOnce(() => {
throw new Error('error at the end of the saveEntity');
Expand Down
1 change: 1 addition & 0 deletions app/api/tenants/tenantContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Tenant = {
featureFlags?: {
s3Storage?: boolean;
sync?: boolean;
v1_transactions?: boolean;
};
globalMatomo?: { id: string; url: string };
ciMatomoActive?: boolean;
Expand Down
35 changes: 35 additions & 0 deletions app/api/utils/specs/withTransaction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { elasticTesting } from '../elastic_testing';
import { getFixturesFactory } from '../fixturesFactory';
import { testingEnvironment } from '../testingEnvironment';
import { withTransaction } from '../withTransaction';
import { testingTenants } from '../testingTenants';

const factory = getFixturesFactory();

Expand Down Expand Up @@ -48,6 +49,7 @@ describe('withTransaction utility', () => {

beforeEach(async () => {
await testingEnvironment.setUp({ transactiontests: [] });
testingTenants.changeCurrentTenant({ featureFlags: { v1_transactions: true } });
testingEnvironment.unsetFakeContext();
});

Expand Down Expand Up @@ -167,6 +169,26 @@ describe('withTransaction utility', () => {
});
});

it('should do nothing when the feature flag is off when there is an error', async () => {
testingTenants.changeCurrentTenant({ featureFlags: { v1_transactions: false } });

await appContext.run(async () => {
let error: Error | undefined;
try {
await withTransaction(async () => {
await model.save({ title: 'test-flag-off', value: 1 });
throw new Error('Testing error');
});
} catch (e) {
error = e;
}
expect(error?.message).toBe('Testing error');

const docs = await model.get({ title: 'test-flag-off' });
expect(docs).toHaveLength(1);
});
});

describe('manual abort', () => {
it('should allow manual abort without throwing error', async () => {
await appContext.run(async () => {
Expand Down Expand Up @@ -230,6 +252,19 @@ describe('withTransaction utility', () => {
expect(sessionToTest?.hasEnded).toBe(true);
});
});
it('should do nothing when the feature flag is off', async () => {
testingTenants.changeCurrentTenant({ featureFlags: { v1_transactions: false } });

await appContext.run(async () => {
await withTransaction(async ({ abort }) => {
await model.save({ title: 'test-flag-off-abort' });
await abort();
});

const docs = await model.get({ title: 'test-flag-off-abort' });
expect(docs).toHaveLength(1);
});
});
});
describe('Entities elasticsearch index', () => {
beforeEach(async () => {
Expand Down
4 changes: 4 additions & 0 deletions app/api/utils/withTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { storage } from 'api/files/storage';
import { dbSessionContext } from 'api/odm/sessionsContext';
import { search } from 'api/search';
import { appContext } from './AppContext';
import { tenants } from 'api/tenants';

interface TransactionOperation {
abort: () => Promise<void>;
Expand Down Expand Up @@ -39,6 +40,9 @@ const performDelayedReindexes = async () => {
const withTransaction = async <T>(
operation: (context: TransactionOperation) => Promise<T>
): Promise<T> => {
if (!tenants.current().featureFlags?.v1_transactions) {
return operation({ abort: async () => {} });
}
const session = await dbSessionContext.startSession();
session.startTransaction();
let wasManuallyAborted = false;
Expand Down

0 comments on commit 03f6dc0

Please sign in to comment.