Skip to content

Commit

Permalink
feat: Use a factory method to create migrations.
Browse files Browse the repository at this point in the history
  • Loading branch information
kinyoklion committed Sep 25, 2023
1 parent d1f7197 commit 28d4424
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 24 deletions.
18 changes: 9 additions & 9 deletions packages/shared/sdk-server/__tests__/Migration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from '../src';
import { TestData } from '../src/integrations';
import { LDClientCallbacks } from '../src/LDClientImpl';
import Migration, { LDMigrationError, LDMigrationSuccess } from '../src/Migration';
import { createMigration, LDMigrationError, LDMigrationSuccess } from '../src/Migration';
import basicPlatform from './evaluation/mocks/platform';
import makeCallbacks from './makeCallbacks';

Expand Down Expand Up @@ -172,7 +172,7 @@ describe('given an LDClient with test data', () => {
],
])('given each migration step: %p, read: %p, write: %j.', (stage, readValue, writeMatch) => {
it('uses the correct authoritative source', async () => {
const migration = new Migration<string, boolean>(client, {
const migration = createMigration<string, boolean>(client, {
execution,
latencyTracking: false,
errorTracking: false,
Expand Down Expand Up @@ -204,7 +204,7 @@ describe('given an LDClient with test data', () => {
it('correctly forwards the payload for read and write operations', async () => {
let receivedReadPayload: string | undefined;
let receivedWritePayload: string | undefined;
const migration = new Migration<string, boolean, string, string>(client, {
const migration = createMigration<string, boolean, string, string>(client, {
execution,
latencyTracking: false,
errorTracking: false,
Expand Down Expand Up @@ -249,7 +249,7 @@ describe('given an LDClient with test data', () => {
[LDMigrationStage.RampDown, 'new'],
[LDMigrationStage.Complete, 'new'],
])('handles read errors for stage: %p', async (stage, authority) => {
const migration = new Migration<string, boolean>(client, {
const migration = createMigration<string, boolean>(client, {
execution: new LDSerialExecution(LDExecutionOrdering.Fixed),
latencyTracking: false,
errorTracking: false,
Expand Down Expand Up @@ -282,7 +282,7 @@ describe('given an LDClient with test data', () => {
[LDMigrationStage.RampDown, 'new'],
[LDMigrationStage.Complete, 'new'],
])('handles exceptions for stage: %p', async (stage, authority) => {
const migration = new Migration<string, boolean>(client, {
const migration = createMigration<string, boolean>(client, {
execution: new LDSerialExecution(LDExecutionOrdering.Fixed),
latencyTracking: false,
errorTracking: false,
Expand Down Expand Up @@ -378,7 +378,7 @@ describe('given an LDClient with test data', () => {
let oldWriteCalled = false;
let newWriteCalled = false;

const migration = new Migration<string, boolean>(client, {
const migration = createMigration<string, boolean>(client, {
execution: new LDSerialExecution(LDExecutionOrdering.Fixed),
latencyTracking: false,
errorTracking: false,
Expand Down Expand Up @@ -420,7 +420,7 @@ describe('given an LDClient with test data', () => {
let oldWriteCalled = false;
let newWriteCalled = false;

const migration = new Migration<string, boolean>(client, {
const migration = createMigration<string, boolean>(client, {
execution: new LDSerialExecution(LDExecutionOrdering.Fixed),
latencyTracking: false,
errorTracking: false,
Expand Down Expand Up @@ -453,7 +453,7 @@ describe('given an LDClient with test data', () => {
});

it('handles the case where the authoritative write succeeds, but the non-authoritative fails', async () => {
const migrationA = new Migration<string, boolean>(client, {
const migrationA = createMigration<string, boolean>(client, {
execution: new LDSerialExecution(LDExecutionOrdering.Fixed),
latencyTracking: false,
errorTracking: false,
Expand Down Expand Up @@ -500,7 +500,7 @@ describe('given an LDClient with test data', () => {
},
});

const migrationB = new Migration<string, boolean>(client, {
const migrationB = createMigration<string, boolean>(client, {
execution: new LDSerialExecution(LDExecutionOrdering.Fixed),
latencyTracking: false,
errorTracking: false,
Expand Down
19 changes: 10 additions & 9 deletions packages/shared/sdk-server/__tests__/MigrationOpEvent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import {
} from '../src';
import { TestData } from '../src/integrations';

Check failure on line 13 in packages/shared/sdk-server/__tests__/MigrationOpEvent.test.ts

View workflow job for this annotation

GitHub Actions / build-test-sdk-server

Insert `LDMigration·}·from·'../src/api/LDMigration';⏎import·{·`
import { LDClientCallbacks } from '../src/LDClientImpl';
import Migration, { LDMigrationError, LDMigrationSuccess } from '../src/Migration';
import { createMigration, LDMigrationError, LDMigrationSuccess } from '../src/Migration';
import MigrationOpEventConversion from '../src/MigrationOpEventConversion';
import basicPlatform from './evaluation/mocks/platform';
import makeCallbacks from './makeCallbacks';

Check failure on line 18 in packages/shared/sdk-server/__tests__/MigrationOpEvent.test.ts

View workflow job for this annotation

GitHub Actions / build-test-sdk-server

Delete `';⏎import·{·LDMigration·}·from·'../src/api/LDMigration`
import { LDMigration } from '../src/api/LDMigration';

jest.mock('@launchdarkly/js-sdk-common', () => ({
__esModule: true,
Expand Down Expand Up @@ -64,9 +65,9 @@ describe('given an LDClient with test data', () => {
[new LDConcurrentExecution(), 'concurrent'],
])('given different execution methods: %p %p', (execution) => {
describe('given a migration which checks consistency and produces consistent results', () => {
let migration: Migration<string, string>;
let migration: LDMigration<string, string>;
beforeEach(() => {
migration = new Migration(client, {
migration = createMigration(client, {
execution,
latencyTracking: false,
errorTracking: false,
Expand Down Expand Up @@ -137,9 +138,9 @@ describe('given an LDClient with test data', () => {
});

describe('given a migration which checks consistency and produces inconsistent results', () => {
let migration: Migration<string, string>;
let migration: LDMigration<string, string>;
beforeEach(() => {
migration = new Migration(client, {
migration = createMigration(client, {
execution,
latencyTracking: false,
errorTracking: false,
Expand Down Expand Up @@ -171,7 +172,7 @@ describe('given an LDClient with test data', () => {
});

describe('given a migration which takes time to execute and tracks latency', () => {
let migration: Migration<string, string>;
let migration: LDMigration<string, string>;

function timeoutPromise<TReturn>(val: TReturn): Promise<TReturn> {
return new Promise((a) => {
Expand All @@ -180,7 +181,7 @@ describe('given an LDClient with test data', () => {
}

beforeEach(() => {
migration = new Migration(client, {
migration = createMigration(client, {
execution,
latencyTracking: true,
errorTracking: false,
Expand Down Expand Up @@ -351,9 +352,9 @@ describe('given an LDClient with test data', () => {
});

describe('given a migration which produces errors for every step', () => {
let migration: Migration<string, boolean>;
let migration: LDMigration<string, boolean>;
beforeEach(() => {
migration = new Migration<string, boolean>(client, {
migration = createMigration<string, boolean>(client, {
execution,
latencyTracking: false,
errorTracking: true,
Expand Down
19 changes: 18 additions & 1 deletion packages/shared/sdk-server/src/Migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ interface MigrationContext<TPayload> {
/**
* Class which allows performing technology migrations.
*/
export default class Migration<
class Migration<
TMigrationRead,
TMigrationWrite,
TMigrationReadInput = any,
Expand Down Expand Up @@ -372,3 +372,20 @@ export default class Migration<
return result;
}
}

export function createMigration<
TMigrationRead,
TMigrationWrite,
TMigrationReadInput = any,
TMigrationWriteInput = any,
>(
client: LDClient,
config: LDMigrationOptions<
TMigrationRead,
TMigrationWrite,
TMigrationReadInput,
TMigrationWriteInput
>,
): LDMigration<TMigrationRead, TMigrationWrite, TMigrationReadInput, TMigrationWriteInput> {
return new Migration(client, config);
}
4 changes: 2 additions & 2 deletions packages/shared/sdk-server/src/api/LDMigration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ export type LDMigrationWriteResult<TResult> = {
export interface LDMigration<
TMigrationRead,
TMigrationWrite,
TMigrationReadInput,
TMigrationWriteInput,
TMigrationReadInput = any,
TMigrationWriteInput = any,
> {
/**
* Perform a read using the migration.
Expand Down
5 changes: 2 additions & 3 deletions packages/shared/sdk-server/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import BigSegmentStoreStatusProviderImpl from './BigSegmentStatusProviderImpl';
import LDClientImpl from './LDClientImpl';
// TODO: Maybe we have a factory?
import Migration, { LDMigrationError, LDMigrationSuccess } from './Migration';
import { createMigration, LDMigrationError, LDMigrationSuccess } from './Migration';

export * as integrations from './integrations';
export * as platform from '@launchdarkly/js-sdk-common';
Expand All @@ -16,5 +15,5 @@ export {
BigSegmentStoreStatusProviderImpl,
LDMigrationError,
LDMigrationSuccess,
Migration,
createMigration,
};

0 comments on commit 28d4424

Please sign in to comment.