Skip to content

Commit

Permalink
CMDCT-3960: pushing new table to deploy to dev
Browse files Browse the repository at this point in the history
  • Loading branch information
angelaco11 committed Sep 12, 2024
1 parent 093d889 commit 8740adb
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
62 changes: 62 additions & 0 deletions services/database/scripts/transformMeasureTable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { DynamoDBClient, paginateScan } from "@aws-sdk/client-dynamodb";
import { DynamoDBDocumentClient, PutCommand } from "@aws-sdk/lib-dynamodb";

const transformMeasureTable = async () => {
const dbClient = buildClient(!!process.env.DYNAMODB_URL);
const tableName = "local-measures";
const newTableName = "local-cs-measures";
console.log(`Processing table ${tableName}`);
for await (let entry of scan(dbClient, tableName)) {
add(dbClient, newTableName, entry);
}
};

async function* scan(client: DynamoDBDocumentClient, table: string) {
const query = {
TableName: table,
};
for await (const result of paginateScan({ client }, query)) {
yield* result.Items ?? [];
}
}

async function add(client: DynamoDBDocumentClient, table: string, entry: any) {
const newCompoundKey = `${entry.state}${entry.year}${entry.coreSet}`;
const params = {
TableName: table,
Item: {
compoundKey: newCompoundKey,
coreSet: entry.coreSet,
},
};
await client.send(new PutCommand(params));
}

function buildClient(isLocal: boolean) {
if (isLocal) {
return DynamoDBDocumentClient.from(
new DynamoDBClient({
region: "localhost",
endpoint: "http://localhost:8000",
credentials: {
accessKeyId: "LOCALFAKEKEY", // pragma: allowlist secret
secretAccessKey: "LOCALFAKESECRET", // pragma: allowlist secret
},
})
);
} else {
return DynamoDBDocumentClient.from(
new DynamoDBClient({
region: "us-east-1",
logger: {
debug: () => {
/* Dynamo's debug logs are extremely noisy */
},
info: console.info,
warn: console.warn,
error: console.error,
},
})
);
}
}
22 changes: 21 additions & 1 deletion services/database/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ custom:
measureTableName: ${self:custom.stage}-measures
bannerTableName: ${self:custom.stage}-banners
rateTableName: ${self:custom.stage}-rates
measureTableName2: ${self:custom.stage}-cs-measures
dynamodb:
stages:
- local
Expand All @@ -34,7 +35,7 @@ provider:
region: us-east-1
stackTags:
PROJECT: ${self:custom.project}
SERVICE: ${self:service}
SERVICE: ${self:service}

resources:
Resources:
Expand Down Expand Up @@ -95,6 +96,25 @@ resources:
- AttributeName: coreSet
KeyType: RANGE
BillingMode: PAY_PER_REQUEST # Set the capacity to auto-scale
MeasureTable2:
Type: AWS::DynamoDB::Table
Properties:
TableName: ${self:custom.measureTableName2}
PointInTimeRecoverySpecification:
PointInTimeRecoveryEnabled: true
StreamSpecification:
StreamViewType: NEW_AND_OLD_IMAGES
AttributeDefinitions:
- AttributeName: compoundKey
AttributeType: S
- AttributeName: coreSet
AttributeType: S
KeySchema:
- AttributeName: compoundKey
KeyType: HASH
- AttributeName: coreSet
KeyType: RANGE
BillingMode: PAY_PER_REQUEST # Set the capacity to auto-scale
BannerTable:
Type: AWS::DynamoDB::Table
Properties:
Expand Down

0 comments on commit 8740adb

Please sign in to comment.