Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚗️ add examples of migrations #44

Merged
merged 1 commit into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions demo/example/lib/exampleStack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,7 @@ export class ExampleStack extends Stack {
id: 'default',
migrationHandling: {
type: 'lambda',
migrationLambdaFunction: new NodejsFunction(
this,
'MigrationLambdaFunction',
{
runtime: Runtime.NODEJS_18_X,
handler: 'index.handler',
entry: path.join(__dirname, 'runMigrationsLambda/index.ts'),
},
),
migrationLambdaFunction,
},
},
});
Expand Down
44 changes: 37 additions & 7 deletions demo/example/lib/runMigrationsLambda/migrations/migration1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,57 @@ const client = new DynamoDB({ region: 'eu-west-1' });
export const migration1: Migration = {
id: 1,
up: async (): Promise<{ status: string }> => {
console.log('MIGRATION_1_UP');

try {
const elements = await client.scan({ TableName: 'Dinosaurs' });
console.log('ELEMENTS', elements);
console.log('ELEMENTS_1_UP', elements);

const modifiedElements = elements.Items?.map((element) => ({
...element,
eyeColor: { S: 'green' },
}));

if (modifiedElements === undefined) {
return { status: 'SUCCEEDED' };
}
await Promise.all(
modifiedElements.map((element) =>
client.putItem({ TableName: 'Dinosaurs', Item: element }),
),
);
} catch (err) {
console.log('ERROR', err);

return Promise.resolve({ status: 'FAILED' });
}
console.log('Some up migration happening here', 1);

// Modify the element and put it back in db
return Promise.resolve({
status: Math.random() < 0.01 ? 'FAILED' : 'SUCCEEDED',
});
return { status: 'SUCCEEDED' };
},
down: async (): Promise<{ status: string }> => {
console.log('Some down migration happening here', 1);
console.log('MIGRATION_1_DOWN');
try {
const elements = await client.scan({ TableName: 'Dinosaurs' });
console.log('ELEMENTS down', elements);

const modifiedElements = elements.Items?.map(
({ eyeColor, ...rest }) => rest,
);

if (modifiedElements === undefined) {
return { status: 'SUCCEEDED' };
}
await Promise.all(
modifiedElements.map((element) =>
client.putItem({ TableName: 'Dinosaurs', Item: element }),
),
);
} catch (err) {
console.log('ERROR down', err);

return Promise.resolve({ status: 'FAILED' });
}
console.log('Some up migration happening here', 1);

return Promise.resolve({ status: 'SUCCEEDED' });
},
Expand Down
68 changes: 60 additions & 8 deletions demo/example/lib/runMigrationsLambda/migrations/migration2.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,71 @@
import { DynamoDB } from '@aws-sdk/client-dynamodb';
import { Migration } from 'migration-version-helpers';

const client = new DynamoDB({ region: 'eu-west-1' });

export const migration2: Migration = {
id: 2,
up: async (): Promise<{ status: string }> => {
console.log('Some up migration happening here', 2);
console.log('MIGRATION_2_UP');
try {
const elements = await client.scan({ TableName: 'Dinosaurs' });

const modifiedElements = elements.Items?.map(({ eyeColor, ...rest }) => ({
...rest,
eyeColor: { L: [eyeColor ?? { S: 'yellow' }] },
}));

if (modifiedElements === undefined) {
return { status: 'SUCCEEDED' };
}
await Promise.all(
modifiedElements.map((element) =>
client.putItem({ TableName: 'Dinosaurs', Item: element }),
),
);
} catch (err) {
console.log('ERROR', err);

return Promise.resolve({ status: 'FAILED' });
}
console.log('Some up migration happening here', 1);

return Promise.resolve({
status: Math.random() > 0.001 ? 'SUCCEEDED' : 'FAILED',
});
// Modify the element and put it back in db
return { status: 'SUCCEEDED' };
},
down: async (): Promise<{ status: string }> => {
console.log('Some down migration happening here', 2);
console.log('MIGRATION_2_DOWN');

try {
const elements = await client.scan({ TableName: 'Dinosaurs' });
console.log('ELEMENTS down', elements.Items);

const modifiedElements = elements.Items?.map((eyeColor, ...rest) => ({
...rest,
// @ts-expect-error AttributeValue type is not correct
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
...(eyeColor.L?.[0]
? {
eyeColor: { S: eyeColor.L[0].S },
}
: {}),
}));

if (modifiedElements === undefined) {
return { status: 'SUCCEEDED' };
}
await Promise.all(
modifiedElements.map((element) =>
client.putItem({ TableName: 'Dinosaurs', Item: element }),
),
);
} catch (err) {
console.log('ERROR down', err);

return Promise.resolve({ status: 'FAILED' });
}
console.log('Some up migration happening here', 1);

return Promise.resolve({
status: Math.random() > 0.5 ? 'SUCCEEDED' : 'FAILED',
});
return Promise.resolve({ status: 'SUCCEEDED' });
},
};
Loading