Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes 2-456: Preserve all data from strategy import #2720

Merged
merged 2 commits into from
Dec 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/lib/db/strategy-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
IEditableStrategy,
IMinimalStrategyRow,
IStrategy,
IStrategyImport,
IStrategyStore,
} from '../types/stores/strategy-store';

Expand Down Expand Up @@ -156,9 +157,16 @@ export default class StrategyStore implements IStrategyStore {
await this.db(TABLE).where({ name }).del();
}

// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
async importStrategy(data): Promise<void> {
const rowData = this.eventDataToRow(data);
async importStrategy(data: IStrategyImport): Promise<void> {
chriswk marked this conversation as resolved.
Show resolved Hide resolved
const rowData = {
name: data.name,
description: data.description,
deprecated: data.deprecated || false,
parameters: JSON.stringify(data.parameters || []),
built_in: data.builtIn ? 1 : 0,
sort_order: data.sortOrder || 9999,
display_name: data.displayName,
};
await this.db(TABLE).insert(rowData).onConflict(['name']).merge();
}

Expand Down
54 changes: 53 additions & 1 deletion src/lib/services/state-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
PROJECT_IMPORT,
} from '../types/events';
import { GLOBAL_ENV } from '../types/environment';

import variantsExportV3 from '../../test/examples/variantsexport_v3.json';
const oldExportExample = require('./state-service-export-v1.json');

function getSetup() {
Expand Down Expand Up @@ -813,3 +813,55 @@ test('Import v1 and exporting v2 should work', async () => {
).toBeTruthy();
expect(exported.featureStrategies).toHaveLength(strategiesCount);
});

test('Importing states with deprecated strategies should keep their deprecated state', async () => {
const { stateService, stores } = getSetup();
const deprecatedStrategyExample = {
version: 4,
features: [],
strategies: [
{
name: 'deprecatedstrat',
description: 'This should be deprecated when imported',
deprecated: true,
parameters: [],
builtIn: false,
sortOrder: 9999,
displayName: 'Deprecated strategy',
},
],
featureStrategies: [],
};
await stateService.import({
data: deprecatedStrategyExample,
userName: 'strategy-importer',
dropBeforeImport: true,
keepExisting: false,
});
const deprecatedStrategy = await stores.strategyStore.get(
'deprecatedstrat',
);
expect(deprecatedStrategy.deprecated).toBe(true);
});

test('Exporting a deprecated strategy and then importing it should keep correct state', async () => {
const { stateService, stores } = getSetup();
await stateService.import({
data: variantsExportV3,
keepExisting: false,
dropBeforeImport: true,
userName: 'strategy importer',
});
const rolloutRandom = await stores.strategyStore.get(
'gradualRolloutRandom',
);
expect(rolloutRandom.deprecated).toBe(true);
const rolloutSessionId = await stores.strategyStore.get(
'gradualRolloutSessionId',
);
expect(rolloutSessionId.deprecated).toBe(true);
const rolloutUserId = await stores.strategyStore.get(
'gradualRolloutUserId',
);
expect(rolloutUserId.deprecated).toBe(true);
});
12 changes: 11 additions & 1 deletion src/lib/types/stores/strategy-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ export interface IMinimalStrategy {
parameters?: any[];
}

export interface IStrategyImport {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at IMinimalStrategy there's a missing property in IStrategyImport:

  • editable?: boolean

Just checking if this is intentional

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, we call it builtIn in the export as well as built_in in the table. where editable is built_in === 0. Unfortunately built_in is still an integer in our table for some reason :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I just checked the previous code using

eventDataToRow(data): IMinimalStrategyRow {
return {
name: data.name,
description: data.description,
parameters: JSON.stringify(data.parameters),
};
}

and I see that editable although present in the type was completely ignored 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup :(

name: string;
description?: string;
deprecated?: boolean;
parameters?: object[];
builtIn?: boolean;
sortOrder?: number;
displayName?: string;
}

export interface IMinimalStrategyRow {
name: string;
description?: string;
Expand All @@ -36,7 +46,7 @@ export interface IStrategyStore extends Store<IStrategy, string> {
updateStrategy(update: IMinimalStrategy): Promise<void>;
deprecateStrategy({ name }: Pick<IStrategy, 'name'>): Promise<void>;
reactivateStrategy({ name }: Pick<IStrategy, 'name'>): Promise<void>;
importStrategy(data: IMinimalStrategy): Promise<void>;
importStrategy(data: IStrategyImport): Promise<void>;
dropCustomStrategies(): Promise<void>;
count(): Promise<number>;
}
50 changes: 50 additions & 0 deletions src/test/e2e/services/state-service.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,53 @@ test('Should import variants in new format (per environment)', async () => {
let featureEnvironments = await stores.featureEnvironmentStore.getAll();
expect(featureEnvironments).toHaveLength(6); // 3 environments, 2 features === 6 rows
});

test('Importing states with deprecated strategies should keep their deprecated state', async () => {
const deprecatedStrategyExample = {
version: 4,
features: [],
strategies: [
{
name: 'deprecatedstrat',
description: 'This should be deprecated when imported',
deprecated: true,
parameters: [],
builtIn: false,
sortOrder: 9999,
displayName: 'Deprecated strategy',
},
],
featureStrategies: [],
};
await stateService.import({
data: deprecatedStrategyExample,
userName: 'strategy-importer',
dropBeforeImport: true,
keepExisting: false,
});
const deprecatedStrategy = await stores.strategyStore.get(
'deprecatedstrat',
);
expect(deprecatedStrategy.deprecated).toBe(true);
});

test('Exporting a deprecated strategy and then importing it should keep correct state', async () => {
await stateService.import({
data: oldFormat,
keepExisting: false,
dropBeforeImport: true,
userName: 'strategy importer',
});
const rolloutRandom = await stores.strategyStore.get(
'gradualRolloutRandom',
);
expect(rolloutRandom.deprecated).toBe(true);
const rolloutSessionId = await stores.strategyStore.get(
'gradualRolloutSessionId',
);
expect(rolloutSessionId.deprecated).toBe(true);
const rolloutUserId = await stores.strategyStore.get(
'gradualRolloutUserId',
);
expect(rolloutUserId.deprecated).toBe(true);
});
3 changes: 2 additions & 1 deletion src/test/fixtures/fake-strategies-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
IEditableStrategy,
IMinimalStrategy,
IStrategy,
IStrategyImport,
IStrategyStore,
} from '../../lib/types/stores/strategy-store';
import NotFoundError from '../../lib/error/notfound-error';
Expand Down Expand Up @@ -104,7 +105,7 @@ export default class FakeStrategiesStore implements IStrategyStore {
throw new NotFoundError(`Could not find strategy with name: ${name}`);
}

async importStrategy(data: IMinimalStrategy): Promise<void> {
async importStrategy(data: IStrategyImport): Promise<void> {
return this.createStrategy(data);
}

Expand Down