Skip to content

Commit

Permalink
fix: remove uniqueness requirement for area's L-R indices
Browse files Browse the repository at this point in the history
  • Loading branch information
viet nguyen committed Jan 2, 2024
1 parent 9980f26 commit 8607c29
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 40 deletions.
5 changes: 5 additions & 0 deletions db-migrations/0005-area-sorting.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* Issue: 375
*/

db.areas.dropIndexes('metadata.leftRightIndex_1')
5 changes: 3 additions & 2 deletions src/db/AreaSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,9 @@ export const AreaSchema = new Schema<AreaType>({
}, { timestamps: true })

AreaSchema.index({ _deleting: 1 }, { expireAfterSeconds: 0 })
AreaSchema.index({ 'metadata.leftRightIndex': 1 }, {
unique: true,
AreaSchema.index({
'metadata.leftRightIndex': 1
}, {
partialFilterExpression: {
'metadata.leftRightIndex': {
$gt: -1
Expand Down
17 changes: 1 addition & 16 deletions src/model/MutableAreaDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,21 +427,6 @@ export default class MutableAreaDataSource extends AreaDataSource {
const opType = OperationType.orderAreas
const change = await changelogDataSource.create(session, user, opType)
const updates: any[] = []
let expectedOpCount = input.length

// Clear existing indices so we can re-order without running into duplicate key errors.
if (input.some(i => i.leftRightIndex >= 0)) {
updates.push({
updateMany: {
filter: { 'metadata.area_id': { $in: input.map(i => muuid.from(i.areaId)) } },
update: {
$set: { 'metadata.leftRightIndex': -1 }
// Don't record change since this is an intermediate step.
}
}
})
expectedOpCount = expectedOpCount * 2
}

input.forEach(({ areaId, leftRightIndex }, index) => {
updates.push({
Expand All @@ -465,7 +450,7 @@ export default class MutableAreaDataSource extends AreaDataSource {

const rs = (await this.areaModel.bulkWrite(updates, { session })).toJSON()

if (rs.ok === 1 && rs.nMatched === rs.nModified && rs.nMatched === expectedOpCount) {
if (rs.ok === 1 && rs.nMatched === rs.nModified) {
return input.map(item => item.areaId)
} else {
throw new Error(`Expect to update ${input.length} areas but found ${rs.nMatched}.`)
Expand Down
22 changes: 0 additions & 22 deletions src/model/__tests__/updateAreas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,28 +286,6 @@ describe('Areas', () => {
leftRightIndex: change2.leftRightIndex
})
}))

// Able to overwrite existing leftRightIndices without duplicate key errors
const change3: UpdateSortingOrderType = {
areaId: a1.metadata.area_id.toUUID().toString(),
leftRightIndex: 9
}
const change4: UpdateSortingOrderType = {
areaId: a2.metadata.area_id.toUUID().toString(),
leftRightIndex: 10
}

await expect(areas.updateSortingOrder(testUser, [change3, change4])).resolves.toStrictEqual(
[a1.metadata.area_id.toUUID().toString(), a2.metadata.area_id.toUUID().toString()])

// Make sure we can't have duplicate leftToRight indices >= 0
await expect(
areas.updateSortingOrder(testUser, [{ ...change3, leftRightIndex: change4.leftRightIndex }]))
.rejects.toThrowError(/E11000/)

// But we can have duplicate indices < 0 to indicate unsorted
await areas.updateSortingOrder(testUser,
[{ ...change3, leftRightIndex: -1 }, { ...change4, leftRightIndex: -1 }])
})

it('should update self and childrens pathTokens', async () => {
Expand Down

0 comments on commit 8607c29

Please sign in to comment.