-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'staging' into 10554-data-cleanup
- Loading branch information
Showing
64 changed files
with
1,333 additions
and
350 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
scripts/run-once-scripts/postgres-migration/batch-delete-dynamo-items.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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { | ||
BatchWriteCommand, | ||
DynamoDBDocumentClient, | ||
} from '@aws-sdk/lib-dynamodb'; | ||
|
||
export async function batchDeleteDynamoItems( | ||
itemsToDelete: { DeleteRequest: { Key: { pk: string; sk: string } } }[], | ||
client: DynamoDBDocumentClient, | ||
tableNameInput: string, | ||
): Promise<number> { | ||
const BATCH_SIZE = 25; | ||
const RETRY_DELAY_MS = 5000; // Set the delay between retries (in milliseconds) | ||
let totalItemsDeleted = 0; | ||
|
||
for (let i = 0; i < itemsToDelete.length; i += BATCH_SIZE) { | ||
const batch = itemsToDelete.slice(i, i + BATCH_SIZE); | ||
|
||
const batchWriteParams = { | ||
RequestItems: { | ||
[tableNameInput]: batch, | ||
}, | ||
}; | ||
|
||
try { | ||
let unprocessedItems: any[] = batch; | ||
let retryCount = 0; | ||
const MAX_RETRIES = 5; | ||
|
||
// Retry logic for unprocessed items | ||
while (unprocessedItems.length > 0 && retryCount < MAX_RETRIES) { | ||
const response = await client.send( | ||
new BatchWriteCommand(batchWriteParams), | ||
); | ||
|
||
totalItemsDeleted += | ||
unprocessedItems.length - | ||
(response.UnprocessedItems?.[tableNameInput]?.length || 0); | ||
|
||
unprocessedItems = response.UnprocessedItems?.[tableNameInput] ?? []; | ||
|
||
if (unprocessedItems.length > 0) { | ||
console.log( | ||
`Retrying unprocessed items: ${unprocessedItems.length}, attempt ${retryCount + 1}`, | ||
); | ||
batchWriteParams.RequestItems[tableNameInput] = unprocessedItems; | ||
retryCount++; | ||
|
||
// Add delay before the next retry | ||
await new Promise(resolve => setTimeout(resolve, RETRY_DELAY_MS)); | ||
} | ||
} | ||
|
||
if (unprocessedItems.length > 0) { | ||
console.error( | ||
`Failed to delete ${unprocessedItems.length} items after ${MAX_RETRIES} retries.`, | ||
); | ||
} | ||
} catch (error) { | ||
console.error('Error in batch delete:', error); | ||
} | ||
} | ||
return totalItemsDeleted; | ||
} |
60 changes: 60 additions & 0 deletions
60
scripts/run-once-scripts/postgres-migration/delete-case-notes.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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* HOW TO RUN | ||
* npx ts-node --transpileOnly scripts/run-once-scripts/postgres-migration/delete-case-notes.ts | ||
*/ | ||
|
||
import { DynamoDBDocumentClient } from '@aws-sdk/lib-dynamodb'; | ||
import { DynamoDBClient } from '@aws-sdk/client-dynamodb'; | ||
import { getDbReader } from '../../../web-api/src/database'; | ||
import { isEmpty } from 'lodash'; | ||
import { batchDeleteDynamoItems } from './batch-delete-dynamo-items'; | ||
import { environment } from '../../../web-api/src/environment'; | ||
|
||
const caseUserNotesPageSize = 10000; | ||
const dynamoDbClient = new DynamoDBClient({ region: 'us-east-1' }); | ||
const dynamoDbDocClient = DynamoDBDocumentClient.from(dynamoDbClient); | ||
|
||
// We set the environment as 'production' (= "a deployed environment") to get the RDS connection to work properly | ||
environment.nodeEnv = 'production'; | ||
|
||
const getCaseNotesToDelete = async (offset: number) => { | ||
const caseNotes = await getDbReader(reader => | ||
reader | ||
.selectFrom('dwUserCaseNote') | ||
.select(['docketNumber', 'userId']) | ||
.orderBy(['docketNumber', 'userId']) | ||
.limit(caseUserNotesPageSize) | ||
.offset(offset) | ||
.execute(), | ||
); | ||
return caseNotes; | ||
}; | ||
|
||
let totalItemsDeleted = 0; | ||
|
||
async function main() { | ||
let offset = 0; | ||
let caseNotesToDelete = await getCaseNotesToDelete(offset); | ||
|
||
while (!isEmpty(caseNotesToDelete)) { | ||
const dynamoItemsToDelete = caseNotesToDelete.map(c => ({ | ||
DeleteRequest: { | ||
Key: { | ||
pk: `user-case-note|${c.docketNumber}`, | ||
sk: `user|${c.userId}`, | ||
}, | ||
}, | ||
})); | ||
totalItemsDeleted += await batchDeleteDynamoItems( | ||
dynamoItemsToDelete, | ||
dynamoDbDocClient, | ||
environment.dynamoDbTableName, | ||
); | ||
console.log(`Total case notes deleted so far: ${totalItemsDeleted}`); | ||
offset += caseUserNotesPageSize; | ||
caseNotesToDelete = await getCaseNotesToDelete(offset); | ||
} | ||
console.log('Done deleting case notes from Dynamo'); | ||
} | ||
|
||
main().catch(console.error); |
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
36 changes: 36 additions & 0 deletions
36
shared/src/business/entities/factories/UserFactory.test.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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { Practitioner } from '@shared/business/entities/Practitioner'; | ||
import { ROLES } from '@shared/business/entities/EntityConstants'; | ||
import { User } from '@shared/business/entities/User'; | ||
import { UserFactory } from '@shared/business/entities/factories/UserFactory'; | ||
|
||
describe('UserFactory', () => { | ||
describe('getClass', () => { | ||
it('should return "Practitioner" class type if role is "privatePractitioner"', () => { | ||
const TEST_USER = { role: ROLES.privatePractitioner }; | ||
const userFactory = new UserFactory(TEST_USER); | ||
const classInstance = userFactory.getClass(); | ||
expect(classInstance).toEqual(Practitioner); | ||
}); | ||
|
||
it('should return "Practitioner" class type if role is "irsPractitioner"', () => { | ||
const TEST_USER = { role: ROLES.irsPractitioner }; | ||
const userFactory = new UserFactory(TEST_USER); | ||
const classInstance = userFactory.getClass(); | ||
expect(classInstance).toEqual(Practitioner); | ||
}); | ||
|
||
it('should return "Practitioner" class type if role is "inactivePractitioner"', () => { | ||
const TEST_USER = { role: ROLES.inactivePractitioner }; | ||
const userFactory = new UserFactory(TEST_USER); | ||
const classInstance = userFactory.getClass(); | ||
expect(classInstance).toEqual(Practitioner); | ||
}); | ||
|
||
it('should return "User" class type if role is "admin"', () => { | ||
const TEST_USER = { role: ROLES.admin }; | ||
const userFactory = new UserFactory(TEST_USER); | ||
const classInstance = userFactory.getClass(); | ||
expect(classInstance).toEqual(User); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Practitioner } from '@shared/business/entities/Practitioner'; | ||
import { ROLES, Role } from '@shared/business/entities/EntityConstants'; | ||
import { User } from '@shared/business/entities/User'; | ||
|
||
type MinimalFactoryInfo = { | ||
role: Role; | ||
}; | ||
|
||
export class UserFactory { | ||
private rawUser: MinimalFactoryInfo; | ||
|
||
constructor(rawUser: MinimalFactoryInfo) { | ||
this.rawUser = rawUser; | ||
} | ||
|
||
public getClass(): typeof User | typeof Practitioner { | ||
if ( | ||
this.rawUser.role === ROLES.privatePractitioner || | ||
this.rawUser.role === ROLES.irsPractitioner || | ||
this.rawUser.role === ROLES.inactivePractitioner | ||
) { | ||
return Practitioner; | ||
} | ||
|
||
return User; | ||
} | ||
} |
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
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
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
Oops, something went wrong.