Skip to content

Commit

Permalink
Batch activity updates while merging organizations (CrowdDotDev#1714)
Browse files Browse the repository at this point in the history
  • Loading branch information
epipav authored Oct 17, 2023
1 parent 6845ad5 commit 859a6be
Showing 1 changed file with 32 additions and 19 deletions.
51 changes: 32 additions & 19 deletions backend/src/database/repositories/organizationRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1130,31 +1130,44 @@ class OrganizationRepository {
fromOrganizationId: string,
toOrganizationId: string,
options: IRepositoryOptions,
batchSize = 10000,
): Promise<void> {
const transaction = SequelizeRepository.getTransaction(options)

const seq = SequelizeRepository.getSequelize(options)

const tenant = SequelizeRepository.getCurrentTenant(options)

const query = `
update "activities"
set
"organizationId" = :newOrganizationId
where
"tenantId" = :tenantId and
"organizationId" = :oldOrganizationId
`
let updatedRowsCount = 0

await seq.query(query, {
replacements: {
tenantId: tenant.id,
oldOrganizationId: fromOrganizationId,
newOrganizationId: toOrganizationId,
},
type: QueryTypes.UPDATE,
transaction,
})
do {
options.log.info(
`[Move Activities] - Moving maximum of ${batchSize} activities from ${fromOrganizationId} to ${toOrganizationId}.`,
)

const query = `
UPDATE "activities"
SET "organizationId" = :newOrganizationId
WHERE id IN (
SELECT id
FROM "activities"
WHERE "tenantId" = :tenantId
AND "organizationId" = :oldOrganizationId
LIMIT :limit
)
`

const [, rowCount] = await seq.query(query, {
replacements: {
tenantId: tenant.id,
oldOrganizationId: fromOrganizationId,
newOrganizationId: toOrganizationId,
limit: batchSize,
},
type: QueryTypes.UPDATE,
transaction,
})

updatedRowsCount = rowCount ?? 0
} while (updatedRowsCount === batchSize)
}

static async *getMergeSuggestions(
Expand Down

0 comments on commit 859a6be

Please sign in to comment.