Skip to content

Commit 92c3279

Browse files
committed
Refactor migration for project_user roleType to ensure idempotency by filtering documents without roleType. Updated AGENT_ROLES_FILTER and USER_ROLES_FILTER to include checks for existing roleType, enhancing data integrity during updates.
1 parent f14dddc commit 92c3279

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

migrations/1757601159298-project_user_role_type.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,27 @@ const BATCH_SIZE = 100;
55
/** Log a progress line every this many bulkWrite rounds (plus always after the 1st). */
66
const PROGRESS_LOG_EVERY_BATCHES = 10;
77

8+
/** Only documents that still need roleType (idempotent re-runs). */
9+
const WITHOUT_ROLE_TYPE = {
10+
$or: [{ roleType: { $exists: false } }, { roleType: null }]
11+
};
12+
813
const AGENT_ROLES_FILTER = {
9-
$or: [
10-
{ role: 'agent' },
11-
{ role: 'supervisor' },
12-
{ role: 'admin' },
13-
{ role: 'owner' }
14+
$and: [
15+
{
16+
$or: [
17+
{ role: 'agent' },
18+
{ role: 'supervisor' },
19+
{ role: 'admin' },
20+
{ role: 'owner' }
21+
]
22+
},
23+
WITHOUT_ROLE_TYPE
1424
]
1525
};
1626

1727
const USER_ROLES_FILTER = {
18-
$or: [{ role: 'user' }, { role: 'guest' }]
28+
$and: [{ $or: [{ role: 'user' }, { role: 'guest' }] }, WITHOUT_ROLE_TYPE]
1929
};
2030

2131
async function batchSetRoleType(filter, roleType, phaseLabel) {
@@ -42,7 +52,7 @@ async function batchSetRoleType(filter, roleType, phaseLabel) {
4252
scanned++;
4353
batch.push({
4454
updateOne: {
45-
filter: { _id: doc._id },
55+
filter: { _id: doc._id, ...WITHOUT_ROLE_TYPE },
4656
update: { $set: { roleType } }
4757
}
4858
});

0 commit comments

Comments
 (0)