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

Backfill eventID in Circulars #2647

Open
jracusin opened this issue Oct 28, 2024 · 0 comments
Open

Backfill eventID in Circulars #2647

jracusin opened this issue Oct 28, 2024 · 0 comments
Assignees

Comments

@jracusin
Copy link
Contributor

          And in addition to this change, I am aware that there needs to be a backfill to get things to work. I need a backfill to populate the eventId in circulars that existed before I added in the eventId on circular creation. I then need a backfill to populate the synonyms with the existing eventIds. I have combined both backfills into one for efficiency sake. This is the backfill script I have currently:
import { tables } from '@architect/functions'
import { ReturnValue } from '@aws-sdk/client-dynamodb'
import type { DynamoDBDocument } from '@aws-sdk/lib-dynamodb'
import { paginateScan, UpdateCommand } from '@aws-sdk/lib-dynamodb'

import {
  type Circular,
  parseEventFromSubject,
} from '~/routes/circulars/circulars.lib'
import { createSynonyms, synonymExists } from '~/routes/synonyms/synonyms.server'

export async function backfill() {
  console.log('Starting backfill...')
  const db = await tables()
  const client = db._doc as unknown as DynamoDBDocument
  const TableName = db.name('circulars')
  const pages = paginateScan(
    { client },
    {
      TableName,
    }
  )
  for await (const page of pages) {
    for (const record of page.Items || []) {
      const circular = record as unknown as Circular
      const validEvent =
        circular.eventId || parseEventFromSubject(circular.subject)
      const promises: Promise<any>[] = []
      if (!circular.eventId && validEvent) {
        const updateParams = {
          TableName,
          Key: {
            circularId: circular.circularId,
          },
          UpdateExpression: 'SET eventId = :eventId',
          ExpressionAttributeValues: {
            ':eventId': validEvent,
          },
          ReturnValues: ReturnValue.UPDATED_NEW,
        }

        promises.push(client.send(new UpdateCommand(updateParams)))
      }
      if (validEvent && !(await synonymExists({ eventId: validEvent }))) promises.push(createSynonyms([validEvent]))
      if(promises.length >= 1){
        try {
          const results = await Promise.all(promises)
          const data = await Promise.all(results)
          data[0].Attributes ? console.log(data[0].Attributes) : console.log(data[0])
          if(data[1]) console.log(data[1])
          console.log("--------")
        } catch (error) {
          console.error(
            `ERROR updating Circular ${circular.circularId}: `,
            error
          )
          throw error
        }
      }
    }
    console.log('... End backfill')
  }
}
await backfill()

Originally posted by @Courey in #2642 (comment)

@jracusin jracusin added this to GCN Oct 28, 2024
@Courey Courey moved this to In Progress in GCN Oct 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: In Progress
Development

No branches or pull requests

2 participants