diff --git a/services/database/scripts/transformMeasureTable.ts b/services/database/scripts/transformMeasureTable.ts new file mode 100644 index 0000000000..542035ccad --- /dev/null +++ b/services/database/scripts/transformMeasureTable.ts @@ -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, + }, + }) + ); + } +} diff --git a/services/database/serverless.yml b/services/database/serverless.yml index 0d5f5dec8e..b8a09cafd1 100644 --- a/services/database/serverless.yml +++ b/services/database/serverless.yml @@ -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 @@ -34,7 +35,7 @@ provider: region: us-east-1 stackTags: PROJECT: ${self:custom.project} - SERVICE: ${self:service} + SERVICE: ${self:service} resources: Resources: @@ -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: