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 42b60a7
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 21 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: {},
});
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,
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,18 @@ 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: {
Expand All @@ -45,7 +41,7 @@ export class MigrationStateMachine extends Construct {
);

const migrationBranches = configurations.map(
({ migrationHandling, versioning }, configIndex) =>
({ migrationHandling }, configIndex) =>
getMigrationStateMachineBaseDefinition(this, {
id: configIndex.toString(),
index: configIndex,
Expand Down
2 changes: 2 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,8 @@ interface GenericMigration {

export type MigrationHandling = GenericMigration;

export type MigrationConfiguration = { migrationHandling: MigrationHandling };

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

0 comments on commit 42b60a7

Please sign in to comment.