Skip to content

Commit

Permalink
feat: actually run state machines
Browse files Browse the repository at this point in the history
  • Loading branch information
RafalWilinski committed Aug 8, 2022
1 parent b2f5e6e commit 92418af
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
34 changes: 20 additions & 14 deletions src/custom-resource-migrations-runner.ts
@@ -1,4 +1,4 @@
import { Table as cdkTable } from "aws-cdk-lib/aws-dynamodb";
// import { Table as cdkTable } from "aws-cdk-lib/aws-dynamodb";
import {
CloudFormationCustomResourceEvent,
CloudFormationCustomResourceResponse,
Expand All @@ -7,10 +7,12 @@ import {
} from "aws-lambda";
import { Construct } from "constructs";
import { $AWS, Function, Table } from "functionless";
import { Migration } from "./migration";
import { MigrationHistoryItem } from "./migrations-manager";

export type CustomResourceMigrationsRunnerProps = {
migrationFiles: string[];
migrationStacks: Migration<any>[];
migrationsHistoryTable: Table<MigrationHistoryItem, "id">;
};

Expand All @@ -22,18 +24,15 @@ export default class CustomResourceMigrationsRunner extends Construct {
constructor(
scope: Construct,
id: string,
props: CustomResourceMigrationsRunnerProps
{
migrationsHistoryTable,
migrationStacks,
}: CustomResourceMigrationsRunnerProps
) {
super(scope, id);

const migrationsHistoryTable = Table.fromTable<MigrationHistoryItem, "id">(
cdkTable.fromTableArn(
scope,
"MigrationsHistoryTable",
props.migrationsHistoryTable.tableArn
)
);

// todo/ to think: maybe this can be a step function too?
// I think it cannot because of custom resources provider - it requires a function
this.function = new Function(
scope,
`${id}-MigrationsRunner`,
Expand All @@ -47,16 +46,23 @@ export default class CustomResourceMigrationsRunner extends Construct {

console.log({ migrations });

const migrationsToRun = props.migrationFiles.filter(
(migrationFile) =>
// todo: Ensure chronological order of migrations.
const migrationsToRun = migrationStacks.filter(
(migrationStack) =>
!(migrations.Items ?? []).find(
(migration) => migration.id.S === migrationFile
(migration) => migration.id.S === migrationStack.stackId
)
);

console.log({ migrationsToRun });

// todo: Start the migrations
// todo: run in sequence actually
if (migrationsToRun[0].stateMachine) {
await migrationsToRun[0].stateMachine({});
// todo: store migration state
// todo: after finish, mark it as complete
// todo: maybe isCompleteHandler should take care of it?
}

return {
Status: "SUCCESS",
Expand Down
7 changes: 6 additions & 1 deletion src/migration.ts
Expand Up @@ -31,6 +31,8 @@ export class Migration<T extends object> extends NestedStack {

public readonly migrationName: string;

public stateMachine?: StepFunction<any, any>;

constructor(scope: Construct, id: string, props: MigrationProps) {
super(scope, id);

Expand All @@ -57,7 +59,7 @@ export class Migration<T extends object> extends NestedStack {
_transformFn
);

return new StepFunction(
const stateMachine = new StepFunction(
this,
"MigrationStateMachine",
{
Expand Down Expand Up @@ -92,6 +94,9 @@ export class Migration<T extends object> extends NestedStack {
});
}
);

this.stateMachine = stateMachine;
return stateMachine;
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/migrations-manager.ts
Expand Up @@ -5,6 +5,7 @@ import { Provider } from "aws-cdk-lib/custom-resources";
import { Construct } from "constructs";
import { Table } from "functionless";
import CustomResourceMigrationsRunner from "./custom-resource-migrations-runner";
import { Migration } from "./migration";

export type MigrationManagerProps = {
/**
Expand Down Expand Up @@ -45,7 +46,7 @@ export class MigrationsManager extends Construct {

const migrationsDir = path.resolve(props.migrationsDir);
const migrationFiles = fs.readdirSync(migrationsDir);
let migrationStacks = [];
let migrationStacks: Migration<any>[] = [];

for (const migrationFile of migrationFiles) {
// Cannot use dynamic imports here due to synchronous nature of CDKs synthesis process
Expand All @@ -61,7 +62,7 @@ export class MigrationsManager extends Construct {
const onEventHandler = new CustomResourceMigrationsRunner(
this,
"MigrationsRunner",
{ migrationsHistoryTable, migrationFiles }
{ migrationsHistoryTable, migrationFiles, migrationStacks }
);

const migrationsProvider = new Provider(this, "MigrationsProvider", {
Expand Down

0 comments on commit 92418af

Please sign in to comment.