Skip to content

Commit 8bf4c4f

Browse files
authored
fix: Allow identical ticks on one day (#340)
The schema enforces a unique index on {climbId, dateClimbed, style, userID, source}. This is meant to prevent duplicating ticks imported from mountain project. However, it also prevents logging such ticks when they are legitimate. The import procedure also deletes imported ticks before reimporting, so this was overly cautious. Change the schema so it does not enforce uniqueness in this way. Instead, add non-unique indexes on { userId } and { userId, climbId }. Add a db migration script. Remove climb uniqueness test. Fixes OpenBeta/open-tacos#631
1 parent 110b0c2 commit 8bf4c4f

File tree

3 files changed

+13
-21
lines changed

3 files changed

+13
-21
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* https://github.com/OpenBeta/open-tacos/issues/631
3+
**/
4+
5+
rs1 = db.ticks.createIndex({ userId: -1 })
6+
rs2 = db.ticks.createIndex({ userId: -1, climbId: -1 })
7+
rs3 = db.ticks.dropIndex({ climbId: 1, dateClimbed: 1, style: 1, userId: 1, source: 1 })
8+
9+
printjson(rs1)
10+
printjson(rs2)
11+
printjson(rs3)

src/db/TickSchema.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ export const TickSchema = new Schema<TickType>({
2424
source: { type: Schema.Types.String, enum: ['MP', 'OB'] as TickSource[], required: true, index: true }
2525
})
2626

27-
TickSchema.index({ climbId: 1, dateClimbed: 1, style: 1, userId: 1, source: 1 }, { unique: true })
27+
TickSchema.index({ userId: 1 }) // for ticksByUser()
28+
TickSchema.index({ userId: 1, climbId: 1 }) // for ticksByUserIdAndClimb()
2829

2930
export const getTickModel = (name: string = 'ticks'): mongoose.Model<TickType> => {
3031
return mongoose.model(name, TickSchema)

src/model/__tests__/ticks.ts

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -185,24 +185,4 @@ describe('Ticks', () => {
185185
expect(newTick?._id).toEqual(OBTick._id)
186186
expect(newTick?.notes).toEqual('Not sandbagged')
187187
})
188-
189-
it('should reject duplicate ticks', async () => {
190-
const tick1: TickInput = {
191-
name: 'Small Dog',
192-
notes: 'Not sandbagged',
193-
climbId: 'c76d2083-6b8f-524a-8fb8-76e1dc79833f',
194-
userId: 'user123',
195-
style: 'Lead',
196-
attemptType: 'Fell/Hung',
197-
dateClimbed: new Date('2012-12-12'),
198-
grade: '5.7',
199-
source: 'OB'
200-
}
201-
202-
await ticks.addTick(tick1)
203-
204-
await expect(
205-
ticks.addTick(tick1)
206-
).rejects.toThrow()
207-
})
208188
})

0 commit comments

Comments
 (0)