Skip to content

Commit

Permalink
feat: transactional bulk update (#3806)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwasniew committed May 19, 2023
1 parent 4adc977 commit f9409fc
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/lib/routes/admin-api/index.ts
Expand Up @@ -107,7 +107,7 @@ class AdminApi extends Controller {
'/feedback',
new UserFeedbackController(config, services).router,
);
this.app.use('/projects', new ProjectApi(config, services).router);
this.app.use('/projects', new ProjectApi(config, services, db).router);
this.app.use(
'/environments',
new EnvironmentsController(config, services).router,
Expand Down
13 changes: 11 additions & 2 deletions src/lib/routes/admin-api/project/index.ts
Expand Up @@ -24,6 +24,8 @@ import { OpenApiService, SettingService } from '../../../services';
import { IAuthRequest } from '../../unleash-types';
import { ProjectApiTokenController } from './api-token';
import ProjectArchiveController from './project-archive';
import { createKnexTransactionStarter } from '../../../db/transaction';
import { Db } from '../../../db/db';

export default class ProjectApi extends Controller {
private projectService: ProjectService;
Expand All @@ -32,7 +34,7 @@ export default class ProjectApi extends Controller {

private openApiService: OpenApiService;

constructor(config: IUnleashConfig, services: IUnleashServices) {
constructor(config: IUnleashConfig, services: IUnleashServices, db: Db) {
super(config);
this.projectService = services.projectService;
this.openApiService = services.openApiService;
Expand Down Expand Up @@ -70,7 +72,14 @@ export default class ProjectApi extends Controller {
],
});

this.use('/', new ProjectFeaturesController(config, services).router);
this.use(
'/',
new ProjectFeaturesController(
config,
services,
createKnexTransactionStarter(db),
).router,
);
this.use('/', new EnvironmentsController(config, services).router);
this.use('/', new ProjectHealthReport(config, services).router);
this.use('/', new VariantsController(config, services).router);
Expand Down
61 changes: 43 additions & 18 deletions src/lib/routes/admin-api/project/project-features.ts
Expand Up @@ -42,6 +42,10 @@ import {
import { OpenApiService, FeatureToggleService } from '../../../services';
import { querySchema } from '../../../schema/feature-schema';
import { BatchStaleSchema } from '../../../openapi/spec/batch-stale-schema';
import {
TransactionCreator,
UnleashTransaction,
} from '../../../db/transaction';

interface FeatureStrategyParams {
projectId: string;
Expand Down Expand Up @@ -90,24 +94,41 @@ const PATH_STRATEGY = `${PATH_STRATEGIES}/:strategyId`;

type ProjectFeaturesServices = Pick<
IUnleashServices,
'featureToggleServiceV2' | 'projectHealthService' | 'openApiService'
| 'featureToggleServiceV2'
| 'projectHealthService'
| 'openApiService'
| 'transactionalFeatureToggleService'
>;

export default class ProjectFeaturesController extends Controller {
private featureService: FeatureToggleService;

private transactionalFeatureToggleService: (
db: UnleashTransaction,
) => FeatureToggleService;

private openApiService: OpenApiService;

private flagResolver: IFlagResolver;

private readonly logger: Logger;

private readonly startTransaction: TransactionCreator<UnleashTransaction>;

constructor(
config: IUnleashConfig,
{ featureToggleServiceV2, openApiService }: ProjectFeaturesServices,
{
featureToggleServiceV2,
openApiService,
transactionalFeatureToggleService,
}: ProjectFeaturesServices,
startTransaction: TransactionCreator<UnleashTransaction>,
) {
super(config);
this.featureService = featureToggleServiceV2;
this.transactionalFeatureToggleService =
transactionalFeatureToggleService;
this.startTransaction = startTransaction;
this.openApiService = openApiService;
this.flagResolver = config.flagResolver;
this.logger = config.getLogger('/admin-api/project/features.ts');
Expand Down Expand Up @@ -727,14 +748,16 @@ export default class ProjectFeaturesController extends Controller {
const { shouldActivateDisabledStrategies } = req.query;
const { features } = req.body;

await this.featureService.bulkUpdateEnabled(
projectId,
features,
environment,
true,
extractUsername(req),
req.user,
shouldActivateDisabledStrategies === 'true',
await this.startTransaction(async (tx) =>
this.transactionalFeatureToggleService(tx).bulkUpdateEnabled(
projectId,
features,
environment,
true,
extractUsername(req),
req.user,
shouldActivateDisabledStrategies === 'true',
),
);
res.status(200).end();
}
Expand All @@ -752,14 +775,16 @@ export default class ProjectFeaturesController extends Controller {
const { shouldActivateDisabledStrategies } = req.query;
const { features } = req.body;

await this.featureService.bulkUpdateEnabled(
projectId,
features,
environment,
false,
extractUsername(req),
req.user,
shouldActivateDisabledStrategies === 'true',
await this.startTransaction(async (tx) =>
this.transactionalFeatureToggleService(tx).bulkUpdateEnabled(
projectId,
features,
environment,
false,
extractUsername(req),
req.user,
shouldActivateDisabledStrategies === 'true',
),
);
res.status(200).end();
}
Expand Down
4 changes: 4 additions & 0 deletions src/lib/services/index.ts
Expand Up @@ -57,6 +57,7 @@ import {
createFakeChangeRequestAccessService,
} from '../features/change-request-access-service/createChangeRequestAccessReadModel';
import ConfigurationRevisionService from '../features/feature-toggle/configuration-revision-service';
import { createFeatureToggleService } from '../features';

// TODO: will be moved to scheduler feature directory
export const scheduleServices = (services: IUnleashServices): void => {
Expand Down Expand Up @@ -184,6 +185,8 @@ export const createServices = (
: createFakeExportImportTogglesService(config);
const transactionalExportImportService = (txDb: Knex.Transaction) =>
createExportImportTogglesService(txDb, config);
const transactionalFeatureToggleService = (txDb: Knex.Transaction) =>
createFeatureToggleService(txDb, config);
const userSplashService = new UserSplashService(stores, config);
const openApiService = new OpenApiService(config);
const clientSpecService = new ClientSpecService(config);
Expand Down Expand Up @@ -275,6 +278,7 @@ export const createServices = (
transactionalExportImportService,
schedulerService,
configurationRevisionService,
transactionalFeatureToggleService,
};
};

Expand Down
3 changes: 3 additions & 0 deletions src/lib/types/services.ts
Expand Up @@ -91,4 +91,7 @@ export interface IUnleashServices {
transactionalExportImportService: (
db: Knex.Transaction,
) => ExportImportService;
transactionalFeatureToggleService: (
db: Knex.Transaction,
) => FeatureToggleService;
}

0 comments on commit f9409fc

Please sign in to comment.