Skip to content

Commit

Permalink
revert multiple branches
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume Égée committed Oct 6, 2023
1 parent e1bcd67 commit e4e6323
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 73 deletions.
21 changes: 6 additions & 15 deletions cdk-dynamodb-migrator/bin/cdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,12 @@ class UserStack extends Stack {
const userStack = new UserStack(app, 'UserStack');

new MigrationStack(app, 'MigrationStack', {
configurations: [
{
id: '0',
migrationHandling: {
type: 'lambda',
migrationLambdaFunction: userStack.runMigrationsFunction,
},
},
{
id: '1',
migrationHandling: {
type: 'lambda',
migrationLambdaFunction: userStack.runMigrationsFunction,
},
configuration: {
id: 'default',
migrationHandling: {
type: 'lambda',
migrationLambdaFunction: userStack.runMigrationsFunction,
},
],
},
settings: {},
});
11 changes: 4 additions & 7 deletions cdk-dynamodb-migrator/lib/constructs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { AttributeType, BillingMode, Table } from 'aws-cdk-lib/aws-dynamodb';
import { Construct } from 'constructs';

import { MigrationStateMachine } from './stepFunctions/stateMachine';
import { MigrationHandling, VersioningSettings } from './types';
import { MigrationConfiguration, VersioningSettings } from './types';
import {
DEFAULT_FIRST_VERSION,
DEFAULT_MIGRATION_PARTITION_KEY,
Expand All @@ -13,10 +13,7 @@ import {

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

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

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

// State machine
this.migrationStateMachine = new MigrationStateMachine(this, 'MSM', {
configurations,
configuration,
versioning: this.versioning,
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ export const getMigrationStateMachineBaseDefinition = (
} = props;

// STEPS
const getConfiguration = new Pass(scope, `GetConfiguration${definitionId}`, {
inputPath: JsonPath.stringAt(`$.configurations.[${props.index}]`),
});

// Check migration version
const setupFirstVersionIfNotDefined = new DynamoPutItem(
scope,
Expand Down Expand Up @@ -237,8 +233,6 @@ export const getMigrationStateMachineBaseDefinition = (
);

// STATE MACHINE ASSEMBLY
getConfiguration.next(setupFirstVersionIfNotDefined);

setupFirstVersionIfNotDefined.next(getCurrentVersion);

setupFirstVersionIfNotDefined.addCatch(getCurrentVersion, {
Expand Down Expand Up @@ -284,7 +278,7 @@ export const getMigrationStateMachineBaseDefinition = (
.otherwise(jobFailed),
);

const stateMachineDefinition = getConfiguration;
const stateMachineDefinition = setupFirstVersionIfNotDefined;

return stateMachineDefinition;
};
55 changes: 11 additions & 44 deletions cdk-dynamodb-migrator/lib/constructs/stepFunctions/stateMachine.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import {
ChainDefinitionBody,
JsonPath,
Parallel,
Pass,
StateMachine,
} from 'aws-cdk-lib/aws-stepfunctions';
import { Construct } from 'constructs';
Expand All @@ -11,55 +8,25 @@ import { getMigrationStateMachineBaseDefinition } from './getStateMachineBaseDef
import { MigrationConfiguration, VersioningSettings } from '../types';

interface MigrationStateMachineProps {
configurations: MigrationConfiguration[];
configuration: MigrationConfiguration;
versioning: VersioningSettings;
}

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

const { configurations, versioning } = props;
const {
configuration: { id: configId, migrationHandling },
versioning,
} = props;

const defineDefaults = new Pass(this, 'DefineDefaults', {
parameters: {
configurations: configurations.map((config, index) => ({
id: config.id,
index,
})),
},
resultPath: '$.inputDefaults',
}).next(
new Pass(this, 'ApplyDefaults', {
resultPath: '$.withDefaults',
outputPath: '$.withDefaults.args',
parameters: {
// Inspired from https://stackoverflow.com/a/74203476
args: JsonPath.jsonMerge(
JsonPath.objectAt('$.inputDefaults'),
JsonPath.executionInput,
),
},
}),
);

const migrationBranches = configurations.map(
({ migrationHandling }, configIndex) =>
getMigrationStateMachineBaseDefinition(this, {
id: configIndex.toString(),
index: configIndex,
migrationHandling,
versioning,
}),
);

const migrationsInParallel = new Parallel(
this,
'RunMigrationsParallel',
{},
).branch(...migrationBranches);

const definition = defineDefaults.next(migrationsInParallel);
const definition = getMigrationStateMachineBaseDefinition(this, {
id: configId,
index: 0,
migrationHandling,
versioning,
});

new StateMachine(this, 'RunMigrationsStateMachine', {
definitionBody: ChainDefinitionBody.fromChainable(definition),
Expand Down

0 comments on commit e4e6323

Please sign in to comment.