Skip to content

Commit 5055dbe

Browse files
authored
fix: remove uniqueness requirement for area's L-R indices (#376)
1 parent 9980f26 commit 5055dbe

File tree

4 files changed

+9
-40
lines changed

4 files changed

+9
-40
lines changed

db-migrations/0005-area-sorting.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/**
2+
* Issue: 375
3+
*/
4+
5+
db.areas.dropIndexes('metadata.leftRightIndex_1')

src/db/AreaSchema.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,9 @@ export const AreaSchema = new Schema<AreaType>({
118118
}, { timestamps: true })
119119

120120
AreaSchema.index({ _deleting: 1 }, { expireAfterSeconds: 0 })
121-
AreaSchema.index({ 'metadata.leftRightIndex': 1 }, {
122-
unique: true,
121+
AreaSchema.index({
122+
'metadata.leftRightIndex': 1
123+
}, {
123124
partialFilterExpression: {
124125
'metadata.leftRightIndex': {
125126
$gt: -1

src/model/MutableAreaDataSource.ts

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -427,21 +427,6 @@ export default class MutableAreaDataSource extends AreaDataSource {
427427
const opType = OperationType.orderAreas
428428
const change = await changelogDataSource.create(session, user, opType)
429429
const updates: any[] = []
430-
let expectedOpCount = input.length
431-
432-
// Clear existing indices so we can re-order without running into duplicate key errors.
433-
if (input.some(i => i.leftRightIndex >= 0)) {
434-
updates.push({
435-
updateMany: {
436-
filter: { 'metadata.area_id': { $in: input.map(i => muuid.from(i.areaId)) } },
437-
update: {
438-
$set: { 'metadata.leftRightIndex': -1 }
439-
// Don't record change since this is an intermediate step.
440-
}
441-
}
442-
})
443-
expectedOpCount = expectedOpCount * 2
444-
}
445430

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

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

468-
if (rs.ok === 1 && rs.nMatched === rs.nModified && rs.nMatched === expectedOpCount) {
453+
if (rs.ok === 1 && rs.nMatched === rs.nModified) {
469454
return input.map(item => item.areaId)
470455
} else {
471456
throw new Error(`Expect to update ${input.length} areas but found ${rs.nMatched}.`)

src/model/__tests__/updateAreas.ts

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -286,28 +286,6 @@ describe('Areas', () => {
286286
leftRightIndex: change2.leftRightIndex
287287
})
288288
}))
289-
290-
// Able to overwrite existing leftRightIndices without duplicate key errors
291-
const change3: UpdateSortingOrderType = {
292-
areaId: a1.metadata.area_id.toUUID().toString(),
293-
leftRightIndex: 9
294-
}
295-
const change4: UpdateSortingOrderType = {
296-
areaId: a2.metadata.area_id.toUUID().toString(),
297-
leftRightIndex: 10
298-
}
299-
300-
await expect(areas.updateSortingOrder(testUser, [change3, change4])).resolves.toStrictEqual(
301-
[a1.metadata.area_id.toUUID().toString(), a2.metadata.area_id.toUUID().toString()])
302-
303-
// Make sure we can't have duplicate leftToRight indices >= 0
304-
await expect(
305-
areas.updateSortingOrder(testUser, [{ ...change3, leftRightIndex: change4.leftRightIndex }]))
306-
.rejects.toThrowError(/E11000/)
307-
308-
// But we can have duplicate indices < 0 to indicate unsorted
309-
await areas.updateSortingOrder(testUser,
310-
[{ ...change3, leftRightIndex: -1 }, { ...change4, leftRightIndex: -1 }])
311289
})
312290

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

0 commit comments

Comments
 (0)