mirror of
https://github.com/strapi/strapi.git
synced 2025-09-23 15:29:27 +00:00
move strategy check to destination provider
This commit is contained in:
parent
40e76c2fa8
commit
2e7cc82064
@ -294,50 +294,6 @@ describe('Transfer engine', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('transfer', () => {
|
describe('transfer', () => {
|
||||||
test('requires strategy to be either restore or merge', async () => {
|
|
||||||
const engineOptions = {
|
|
||||||
versionMatching: 'exact',
|
|
||||||
exclude: [],
|
|
||||||
} as unknown as ITransferEngineOptions;
|
|
||||||
|
|
||||||
const restoreEngine = createTransferEngine(minimalSource, minimalDestination, {
|
|
||||||
...engineOptions,
|
|
||||||
strategy: 'restore',
|
|
||||||
});
|
|
||||||
await restoreEngine.transfer();
|
|
||||||
expect(restoreEngine).toBeValidTransferEngine();
|
|
||||||
|
|
||||||
const mergeEngine = createTransferEngine(minimalSource, minimalDestination, {
|
|
||||||
...engineOptions,
|
|
||||||
strategy: 'merge',
|
|
||||||
});
|
|
||||||
await mergeEngine.transfer();
|
|
||||||
expect(mergeEngine).toBeValidTransferEngine();
|
|
||||||
|
|
||||||
// undefined strategy
|
|
||||||
await expect(
|
|
||||||
(async () => {
|
|
||||||
const invalidEngine = createTransferEngine(
|
|
||||||
minimalSource,
|
|
||||||
minimalDestination,
|
|
||||||
engineOptions
|
|
||||||
);
|
|
||||||
await invalidEngine.transfer();
|
|
||||||
})()
|
|
||||||
).rejects.toThrow();
|
|
||||||
|
|
||||||
// invalid strategy
|
|
||||||
await expect(
|
|
||||||
(async () => {
|
|
||||||
const invalidEngine = createTransferEngine(minimalSource, minimalDestination, {
|
|
||||||
...engineOptions,
|
|
||||||
strategy: 'foo',
|
|
||||||
} as unknown as ITransferEngineOptions);
|
|
||||||
await invalidEngine.transfer();
|
|
||||||
})()
|
|
||||||
).rejects.toThrow();
|
|
||||||
});
|
|
||||||
|
|
||||||
test('calls all provider stages', async () => {
|
test('calls all provider stages', async () => {
|
||||||
const engine = createTransferEngine(completeSource, completeDestination, defaultOptions);
|
const engine = createTransferEngine(completeSource, completeDestination, defaultOptions);
|
||||||
expect(completeSource).toHaveSourceStagesCalledTimes(0);
|
expect(completeSource).toHaveSourceStagesCalledTimes(0);
|
||||||
|
@ -32,8 +32,6 @@ type TransferEngineProgress = {
|
|||||||
stream: PassThrough;
|
stream: PassThrough;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const VALID_STRATEGIES = ['restore', 'merge'];
|
|
||||||
|
|
||||||
class TransferEngine<
|
class TransferEngine<
|
||||||
S extends ISourceProvider = ISourceProvider,
|
S extends ISourceProvider = ISourceProvider,
|
||||||
D extends IDestinationProvider = IDestinationProvider
|
D extends IDestinationProvider = IDestinationProvider
|
||||||
@ -236,16 +234,8 @@ class TransferEngine<
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
validateTransferOptions() {
|
|
||||||
if (!VALID_STRATEGIES.includes(this.options.strategy)) {
|
|
||||||
throw new Error('Invalid stategy ' + this.options.strategy);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async transfer(): Promise<ITransferResults<S, D>> {
|
async transfer(): Promise<ITransferResults<S, D>> {
|
||||||
try {
|
try {
|
||||||
this.validateTransferOptions();
|
|
||||||
|
|
||||||
await this.bootstrap();
|
await this.bootstrap();
|
||||||
await this.init();
|
await this.init();
|
||||||
|
|
||||||
|
@ -24,6 +24,33 @@ describe('Local Strapi Source Destination', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('Strategy', () => {
|
describe('Strategy', () => {
|
||||||
|
test('requires strategy to be either restore or merge', async () => {
|
||||||
|
const restoreProvider = createLocalStrapiDestinationProvider({
|
||||||
|
getStrapi: getStrapiFactory(),
|
||||||
|
strategy: 'restore',
|
||||||
|
});
|
||||||
|
await restoreProvider.bootstrap();
|
||||||
|
expect(restoreProvider.strapi).toBeDefined();
|
||||||
|
|
||||||
|
const mergeProvider = createLocalStrapiDestinationProvider({
|
||||||
|
getStrapi: getStrapiFactory(),
|
||||||
|
strategy: 'merge',
|
||||||
|
});
|
||||||
|
await mergeProvider.bootstrap();
|
||||||
|
expect(mergeProvider.strapi).toBeDefined();
|
||||||
|
|
||||||
|
await expect(
|
||||||
|
(async () => {
|
||||||
|
const invalidProvider = createLocalStrapiDestinationProvider({
|
||||||
|
getStrapi: getStrapiFactory(),
|
||||||
|
/* @ts-ignore: disable-next-line */
|
||||||
|
strategy: 'foo',
|
||||||
|
});
|
||||||
|
await invalidProvider.bootstrap();
|
||||||
|
})()
|
||||||
|
).rejects.toThrow();
|
||||||
|
});
|
||||||
|
|
||||||
test('Should delete all entities if it is a restore', async () => {
|
test('Should delete all entities if it is a restore', async () => {
|
||||||
const entities = [
|
const entities = [
|
||||||
{
|
{
|
||||||
|
@ -7,6 +7,8 @@ import { Duplex } from 'stream';
|
|||||||
|
|
||||||
import { mapSchemasValues } from '../../utils';
|
import { mapSchemasValues } from '../../utils';
|
||||||
|
|
||||||
|
export const VALID_STRATEGIES = ['restore', 'merge'];
|
||||||
|
|
||||||
interface ILocalStrapiDestinationProviderOptions {
|
interface ILocalStrapiDestinationProviderOptions {
|
||||||
getStrapi(): Strapi.Strapi | Promise<Strapi.Strapi>;
|
getStrapi(): Strapi.Strapi | Promise<Strapi.Strapi>;
|
||||||
restore?: DeleteOptions;
|
restore?: DeleteOptions;
|
||||||
@ -35,6 +37,7 @@ class LocalStrapiDestinationProvider implements IDestinationProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async bootstrap(): Promise<void> {
|
async bootstrap(): Promise<void> {
|
||||||
|
this.validateOptions();
|
||||||
this.strapi = await this.options.getStrapi();
|
this.strapi = await this.options.getStrapi();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -42,6 +45,12 @@ class LocalStrapiDestinationProvider implements IDestinationProvider {
|
|||||||
await this.strapi?.destroy?.();
|
await this.strapi?.destroy?.();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
validateOptions() {
|
||||||
|
if (!VALID_STRATEGIES.includes(this.options.strategy)) {
|
||||||
|
throw new Error('Invalid stategy ' + this.options.strategy);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async deleteAll() {
|
async deleteAll() {
|
||||||
if (!this.strapi) {
|
if (!this.strapi) {
|
||||||
throw new Error('Strapi instance not found');
|
throw new Error('Strapi instance not found');
|
||||||
|
@ -24,7 +24,6 @@ export const deleteAllRecords = async (strapi: Strapi.Strapi, deleteOptions?: De
|
|||||||
);
|
);
|
||||||
const contentTypes: ContentTypeSchema[] = deleteOptions?.contentTypes ?? defaultContentTypes;
|
const contentTypes: ContentTypeSchema[] = deleteOptions?.contentTypes ?? defaultContentTypes;
|
||||||
let count = 0;
|
let count = 0;
|
||||||
console.log(deleteOptions?.contentTypes);
|
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
contentTypes.map(async (contentType) => {
|
contentTypes.map(async (contentType) => {
|
||||||
const filters = conditions[contentType.uid] ?? {};
|
const filters = conditions[contentType.uid] ?? {};
|
||||||
|
@ -19,6 +19,7 @@ interface IProvider {
|
|||||||
close?(): Promise<void> | void;
|
close?(): Promise<void> | void;
|
||||||
getMetadata(): IMetadata | null | Promise<IMetadata | null>;
|
getMetadata(): IMetadata | null | Promise<IMetadata | null>;
|
||||||
beforeStreaming?(): Promise<void>;
|
beforeStreaming?(): Promise<void>;
|
||||||
|
validateOptions?(): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ISourceProvider extends IProvider {
|
export interface ISourceProvider extends IProvider {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user