Skip to content

Commit

Permalink
refactor: fix modularization
Browse files Browse the repository at this point in the history
  • Loading branch information
RafalWilinski committed Aug 8, 2022
1 parent 19efb4b commit b2f5e6e
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 53 deletions.
3 changes: 2 additions & 1 deletion package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 45 additions & 15 deletions src/custom-resource-migrations-runner.ts
@@ -1,41 +1,71 @@
import { CloudFormationCustomResourceEvent } from "aws-lambda";
import { Table as cdkTable } from "aws-cdk-lib/aws-dynamodb";
import {
CloudFormationCustomResourceEvent,
CloudFormationCustomResourceResponse,
CloudFormationCustomResourceSuccessResponse,
CloudFormationCustomResourceFailedResponse,
} from "aws-lambda";
import { Construct } from "constructs";
import { $AWS, Function } from "functionless";
import { $AWS, Function, Table } from "functionless";
import { MigrationHistoryItem } from "./migrations-manager";

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

export default class CustomResourceMigrationsRunner extends Construct {
public readonly function: Function<
CloudFormationCustomResourceEvent,
CloudFormationCustomResourceResponse
>;
constructor(
scope: Construct,
id: string,
props: CustomResourceMigrationsRunnerProps
) {
super(scope, id);

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

this.function = new Function(
scope,
`${id}-MigrationsRunner`,
async (event: CloudFormationCustomResourceEvent) => {
console.log(event);

const migrations = await $AWS.DynamoDB.Scan({
Table: migrationsHistoryTable,
});
try {
const migrations = await $AWS.DynamoDB.Scan({
Table: migrationsHistoryTable,
});

console.log({ migrations });

console.log({ migrations });
const migrationsToRun = props.migrationFiles.filter(
(migrationFile) =>
!(migrations.Items ?? []).find(
(migration) => migration.id.S === migrationFile
)
);

const migrationsToRun = props.migrationFiles.filter(
(migrationFile) =>
!(migrations.Items ?? []).find(
(migration) => migration.id.S === migrationFile
)
);
console.log({ migrationsToRun });

console.log({ migrationsToRun });
// todo: Start the migrations

// todo: Start the migrations
return {
Status: "SUCCESS",
} as CloudFormationCustomResourceSuccessResponse;
} catch (error) {
return {
Status: "FAILED",
} as CloudFormationCustomResourceFailedResponse;
}
}
);
}
Expand Down
14 changes: 4 additions & 10 deletions src/examples/migrations/20220801-add-attribute.ts
@@ -1,14 +1,7 @@
import { Table as cdkTable } from "aws-cdk-lib/aws-dynamodb";
import { Construct } from "constructs";
import { $AWS, Table } from "functionless";
import { unmarshall, marshall } from "typesafe-dynamodb/lib/marshall";
import { MigrationProps, Migration } from "../../migration";

export type MigrationFunction = (
scope: Construct,
id: string,
props: MigrationProps
) => Migration<any>;
import { Migration, MigrationFunction } from "../..";

const tableArn =
"arn:aws:dynamodb:us-east-1:085108115628:table/TestStack-TableCD117FA1-ZVV3ZWUOWPO";
Expand All @@ -20,16 +13,17 @@ export const migration: MigrationFunction = (scope, migrationName) => {
});

const table = Table.fromTable(
cdkTable.fromTableArn(scope, "SubjectTable", tableArn)
cdkTable.fromTableArn(scope, "TargetTable", tableArn)
);

// Actual migration code goes here.
// For each item in the table
migrationDefinition.scan(async ({ result }) => {
for (const i of result.Items as any[]) {
// Do the following:
// Do the following
await $AWS.DynamoDB.PutItem({
Table: table,
// Add migratedAt attribute to the item
Item: marshall({ ...unmarshall(i), migratedAt: Date.now() }),
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/examples/test-stack.ts
@@ -1,6 +1,6 @@
import { App, CfnOutput, Stack } from "aws-cdk-lib";
import { AttributeType, BillingMode, Table } from "aws-cdk-lib/aws-dynamodb";
import { MigrationsManager } from "../app";
import { MigrationsManager } from "../";

const app = new App();

Expand Down
6 changes: 6 additions & 0 deletions src/migration.ts
Expand Up @@ -94,3 +94,9 @@ export class Migration<T extends object> extends NestedStack {
);
}
}

export type MigrationFunction = (
scope: Construct,
id: string,
props: MigrationProps
) => Migration<any>;
34 changes: 8 additions & 26 deletions src/migrations-manager.ts
Expand Up @@ -2,9 +2,9 @@ import * as fs from "fs";
import * as path from "path";
import { aws_dynamodb, CustomResource } from "aws-cdk-lib";
import { Provider } from "aws-cdk-lib/custom-resources";
import { CloudFormationCustomResourceEvent } from "aws-lambda";
import { Construct } from "constructs";
import { $AWS, Table, Function } from "functionless";
import { Table } from "functionless";
import CustomResourceMigrationsRunner from "./custom-resource-migrations-runner";

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

const migrationsDir = path.resolve(props.migrationsDir);
const migrationFiles = fs.readdirSync(migrationsDir);

let migrationStacks = [];

for (const migrationFile of migrationFiles) {
// Cannot use dynamic imports here due to synchronous nature of CDKs synthesis process
// eslint-disable-next-line @typescript-eslint/no-require-imports
const migrationStack = require(path.resolve(
migrationsDir,
Expand All @@ -58,35 +58,17 @@ export class MigrationsManager extends Construct {
migrationStacks.push(migrationStack.migration(this, migrationFile));
}

const onEventHandler = new Function(
const onEventHandler = new CustomResourceMigrationsRunner(
this,
"OnEventHandler",
async (event: CloudFormationCustomResourceEvent) => {
console.log(event);

const migrations = await $AWS.DynamoDB.Scan({
Table: migrationsHistoryTable,
});

console.log({ migrations });

const migrationsToRun = migrationFiles.filter(
(migrationFile) =>
!(migrations.Items ?? []).find(
(migration) => migration.id.S === migrationFile
)
);

console.log({ migrationsToRun });

// todo: Start the migrations
}
"MigrationsRunner",
{ migrationsHistoryTable, migrationFiles }
);

const migrationsProvider = new Provider(this, "MigrationsProvider", {
onEventHandler: onEventHandler.resource,
onEventHandler: onEventHandler.function.resource,
});

// Ensure migrations provider is ran after all nested stacks are created
migrationStacks.map((stack) =>
migrationsProvider.node.addDependency(stack)
);
Expand Down

0 comments on commit b2f5e6e

Please sign in to comment.