1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-25 00:07:47 +01:00

chore: split interfaces for import and export (#5004)

## About the changes
This splits the interfaces for import and export, especially because the
import functionality has to be replaced in enterprise repo.

This is a breaking change because of the service renames, but I'll have
the PR for the other repository ready so we reduce the time to fix. I
intentionally avoided doing it backward compatible because of time.
This commit is contained in:
Gastón Fournier 2023-10-12 11:34:09 +02:00 committed by GitHub
parent cfcf9de65a
commit 7343183f2d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 47 additions and 32 deletions

View File

@ -1,7 +1,10 @@
import { Response } from 'express'; import { Response } from 'express';
import Controller from '../../routes/controller'; import Controller from '../../routes/controller';
import { Logger } from '../../logger'; import { Logger } from '../../logger';
import ExportImportService from './export-import-service'; import ExportImportService, {
IExportService,
IImportService,
} from './export-import-service';
import { OpenApiService } from '../../services'; import { OpenApiService } from '../../services';
import { import {
TransactionCreator, TransactionCreator,
@ -32,32 +35,31 @@ import ApiUser from '../../types/api-user';
class ExportImportController extends Controller { class ExportImportController extends Controller {
private logger: Logger; private logger: Logger;
/** @deprecated gradually rolling out exportImportV2 */ private exportService: IExportService;
private exportImportService: ExportImportService;
/** @deprecated gradually rolling out exportImportV2 */ /** @deprecated gradually rolling out importService */
private transactionalExportImportService: ( private transactionalExportImportService: (
db: UnleashTransaction, db: UnleashTransaction,
) => Pick<ExportImportService, 'import' | 'validate'>; ) => IImportService;
private exportImportServiceV2: WithTransactional<ExportImportService>; private importService: WithTransactional<IImportService>;
private openApiService: OpenApiService; private openApiService: OpenApiService;
/** @deprecated gradually rolling out exportImportV2 */ /** @deprecated gradually rolling out importService */
private readonly startTransaction: TransactionCreator<UnleashTransaction>; private readonly startTransaction: TransactionCreator<UnleashTransaction>;
constructor( constructor(
config: IUnleashConfig, config: IUnleashConfig,
{ {
exportImportService, exportService,
transactionalExportImportService, transactionalExportImportService,
exportImportServiceV2, importService,
openApiService, openApiService,
}: Pick< }: Pick<
IUnleashServices, IUnleashServices,
| 'exportImportService' | 'exportService'
| 'exportImportServiceV2' | 'importService'
| 'openApiService' | 'openApiService'
| 'transactionalExportImportService' | 'transactionalExportImportService'
>, >,
@ -65,10 +67,10 @@ class ExportImportController extends Controller {
) { ) {
super(config); super(config);
this.logger = config.getLogger('/admin-api/export-import.ts'); this.logger = config.getLogger('/admin-api/export-import.ts');
this.exportImportService = exportImportService; this.exportService = exportService;
this.transactionalExportImportService = this.transactionalExportImportService =
transactionalExportImportService; transactionalExportImportService;
this.exportImportServiceV2 = exportImportServiceV2; this.importService = importService;
this.startTransaction = startTransaction; this.startTransaction = startTransaction;
this.openApiService = openApiService; this.openApiService = openApiService;
this.route({ this.route({
@ -141,12 +143,7 @@ class ExportImportController extends Controller {
const query = req.body; const query = req.body;
const userName = extractUsername(req); const userName = extractUsername(req);
const useTransactionalDecorator = this.config.flagResolver.isEnabled( const data = await this.exportService.export(query, userName);
'transactionalDecorator',
);
const data = useTransactionalDecorator
? await this.exportImportServiceV2.export(query, userName)
: await this.exportImportService.export(query, userName);
this.openApiService.respondWithValidation( this.openApiService.respondWithValidation(
200, 200,
@ -168,7 +165,7 @@ class ExportImportController extends Controller {
'transactionalDecorator', 'transactionalDecorator',
); );
const validation = useTransactionalDecorator const validation = useTransactionalDecorator
? await this.exportImportServiceV2.transactional((service) => ? await this.importService.transactional((service) =>
service.validate(dto, user), service.validate(dto, user),
) )
: await this.startTransaction(async (tx) => : await this.startTransaction(async (tx) =>
@ -203,7 +200,7 @@ class ExportImportController extends Controller {
); );
if (useTransactionalDecorator) { if (useTransactionalDecorator) {
await this.exportImportServiceV2.transactional((service) => await this.importService.transactional((service) =>
service.import(dto, user), service.import(dto, user),
); );
} else { } else {

View File

@ -50,7 +50,25 @@ import { ImportValidationMessages } from './import-validation-messages';
import { findDuplicates } from '../../util/findDuplicates'; import { findDuplicates } from '../../util/findDuplicates';
import { FeatureNameCheckResultWithFeaturePattern } from '../feature-toggle/feature-toggle-service'; import { FeatureNameCheckResultWithFeaturePattern } from '../feature-toggle/feature-toggle-service';
export default class ExportImportService { export type IImportService = {
validate(
dto: ImportTogglesSchema,
user: User,
): Promise<ImportTogglesValidateSchema>;
import(dto: ImportTogglesSchema, user: User): Promise<void>;
};
export type IExportService = {
export(
query: ExportQuerySchema,
userName: string,
): Promise<ExportResultSchema>;
};
export default class ExportImportService
implements IExportService, IImportService
{
private logger: Logger; private logger: Logger;
private toggleStore: IFeatureToggleStore; private toggleStore: IFeatureToggleStore;

View File

@ -299,7 +299,7 @@ export const createServices = (
const exportImportService = db const exportImportService = db
? createExportImportTogglesService(db, config) ? createExportImportTogglesService(db, config)
: createFakeExportImportTogglesService(config); : createFakeExportImportTogglesService(config);
const exportImportServiceV2 = db const importService = db
? withTransactional(deferredExportImportTogglesService(config), db) ? withTransactional(deferredExportImportTogglesService(config), db)
: withFakeTransactional(createFakeExportImportTogglesService(config)); : withFakeTransactional(createFakeExportImportTogglesService(config));
const transactionalExportImportService = (txDb: Knex.Transaction) => const transactionalExportImportService = (txDb: Knex.Transaction) =>
@ -403,9 +403,9 @@ export const createServices = (
instanceStatsService, instanceStatsService,
favoritesService, favoritesService,
maintenanceService, maintenanceService,
exportImportService, exportService: exportImportService,
transactionalExportImportService, transactionalExportImportService,
exportImportServiceV2, importService,
schedulerService, schedulerService,
configurationRevisionService, configurationRevisionService,
transactionalFeatureToggleService, transactionalFeatureToggleService,

View File

@ -39,7 +39,10 @@ import MaintenanceService from '../services/maintenance-service';
import { AccountService } from '../services/account-service'; import { AccountService } from '../services/account-service';
import { SchedulerService } from '../services/scheduler-service'; import { SchedulerService } from '../services/scheduler-service';
import { Knex } from 'knex'; import { Knex } from 'knex';
import ExportImportService from '../features/export-import-toggles/export-import-service'; import {
IExportService,
IImportService,
} from '../features/export-import-toggles/export-import-service';
import { ISegmentService } from '../segments/segment-service-interface'; import { ISegmentService } from '../segments/segment-service-interface';
import ConfigurationRevisionService from '../features/feature-toggle/configuration-revision-service'; import ConfigurationRevisionService from '../features/feature-toggle/configuration-revision-service';
import EventAnnouncerService from 'lib/services/event-announcer-service'; import EventAnnouncerService from 'lib/services/event-announcer-service';
@ -89,16 +92,13 @@ export interface IUnleashServices {
instanceStatsService: InstanceStatsService; instanceStatsService: InstanceStatsService;
favoritesService: FavoritesService; favoritesService: FavoritesService;
maintenanceService: MaintenanceService; maintenanceService: MaintenanceService;
/** @deprecated prefer exportImportServiceV2, we're doing a gradual rollout */ exportService: IExportService;
exportImportService: ExportImportService; importService: WithTransactional<IImportService>;
exportImportServiceV2: WithTransactional<ExportImportService>;
configurationRevisionService: ConfigurationRevisionService; configurationRevisionService: ConfigurationRevisionService;
schedulerService: SchedulerService; schedulerService: SchedulerService;
eventAnnouncerService: EventAnnouncerService; eventAnnouncerService: EventAnnouncerService;
/** @deprecated prefer exportImportServiceV2, we're doing a gradual rollout */ /** @deprecated prefer exportImportServiceV2, we're doing a gradual rollout */
transactionalExportImportService: ( transactionalExportImportService: (db: Knex.Transaction) => IImportService;
db: Knex.Transaction,
) => Pick<ExportImportService, 'import' | 'validate'>;
transactionalFeatureToggleService: ( transactionalFeatureToggleService: (
db: Knex.Transaction, db: Knex.Transaction,
) => FeatureToggleService; ) => FeatureToggleService;