Skip to content

Commit

Permalink
feat(construct): BREAKING CHANGE: add multiple migration configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume Égée committed Oct 6, 2023
1 parent c10df04 commit e1bcd67
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 25 deletions.
20 changes: 16 additions & 4 deletions cdk-dynamodb-migrator/bin/cdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,21 @@ class UserStack extends Stack {
const userStack = new UserStack(app, 'UserStack');

new MigrationStack(app, 'MigrationStack', {
migrationHandling: {
type: 'lambda',
migrationLambdaFunction: userStack.runMigrationsFunction,
},
configurations: [
{
id: '0',
migrationHandling: {
type: 'lambda',
migrationLambdaFunction: userStack.runMigrationsFunction,
},
},
{
id: '1',
migrationHandling: {
type: 'lambda',
migrationLambdaFunction: userStack.runMigrationsFunction,
},
},
],
settings: {},
});
12 changes: 9 additions & 3 deletions cdk-dynamodb-migrator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import {
FAILURE_STATUS,
SUCCESS_STATUS,
} from './lib/constants';
import { MigrationConstruct, MigrationConstructProps } from './lib/constructs';
import {
MigrationConstruct,
MigrationConstructProps,
MigrationConfiguration,
MigrationHandling,
VersioningSettings,
} from './lib/constructs';
} from './lib/constructs/types';

export {
DEFAULT_PARTITION_KEY_NAME,
Expand All @@ -20,9 +21,14 @@ export {
CURRENT_STATUS_SORT_KEY,
SUCCESS_STATUS,
FAILURE_STATUS,
// Export Migration construct
MigrationConstruct,
MigrationConstructProps,
// Export Migration stack
MigrationStack,
MigrationStackProps,
// Export types
MigrationConfiguration,
MigrationHandling,
VersioningSettings,
};
15 changes: 7 additions & 8 deletions cdk-dynamodb-migrator/lib/constructs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import {

/** MigrationConstruct props */
export type MigrationConstructProps = StackProps & {
migrationHandling: MigrationHandling;
configurations: {
id: string;
migrationHandling: MigrationHandling;
}[];
settings?: {
versioning?: Partial<VersioningSettings>;
};
Expand All @@ -26,7 +29,7 @@ export class MigrationConstruct extends Construct {

constructor(scope: Construct, id: string, props: MigrationConstructProps) {
super(scope, id);
const { migrationHandling, settings } = props;
const { configurations, settings } = props;

// Versioning
const versioningPartitionKey =
Expand Down Expand Up @@ -58,12 +61,8 @@ export class MigrationConstruct extends Construct {

// State machine
this.migrationStateMachine = new MigrationStateMachine(this, 'MSM', {
configurations: [
{
migrationHandling,
versioning: this.versioning,
},
],
configurations,
versioning: this.versioning,
});
}
}
19 changes: 9 additions & 10 deletions cdk-dynamodb-migrator/lib/constructs/stepFunctions/stateMachine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,25 @@ import {
import { Construct } from 'constructs';

import { getMigrationStateMachineBaseDefinition } from './getStateMachineBaseDefinition';
import { MigrationHandling, VersioningSettings } from '../types';
import { MigrationConfiguration, VersioningSettings } from '../types';

interface MigrationStateMachineProps {
configurations: [
{
migrationHandling: MigrationHandling;
versioning: VersioningSettings;
},
];
configurations: MigrationConfiguration[];
versioning: VersioningSettings;
}

export class MigrationStateMachine extends Construct {
constructor(scope: Construct, id: string, props: MigrationStateMachineProps) {
super(scope, id);

const { configurations } = props;
const { configurations, versioning } = props;

const defineDefaults = new Pass(this, 'DefineDefaults', {
parameters: {
configurations: configurations.map((_, index) => ({ id: index })),
configurations: configurations.map((config, index) => ({
id: config.id,
index,
})),
},
resultPath: '$.inputDefaults',
}).next(
Expand All @@ -45,7 +44,7 @@ export class MigrationStateMachine extends Construct {
);

const migrationBranches = configurations.map(
({ migrationHandling, versioning }, configIndex) =>
({ migrationHandling }, configIndex) =>
getMigrationStateMachineBaseDefinition(this, {
id: configIndex.toString(),
index: configIndex,
Expand Down
5 changes: 5 additions & 0 deletions cdk-dynamodb-migrator/lib/constructs/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ interface GenericMigration {

export type MigrationHandling = GenericMigration;

export type MigrationConfiguration = {
id: string;
migrationHandling: MigrationHandling;
};

/** Versioning settings of MigrationConstruct */
export type VersioningSettings = {
/** DynamoDB table to store migration state and versions */
Expand Down

0 comments on commit e1bcd67

Please sign in to comment.