-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from guiyom-e/doc/add-examples
⚗️ add examples of migrations
- Loading branch information
Showing
3 changed files
with
98 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 60 additions & 8 deletions
68
demo/example/lib/runMigrationsLambda/migrations/migration2.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }); | ||
}, | ||
}; |