Skip to content

Commit 8799da2

Browse files
authored
Merge pull request #15035 from Automattic/vkarpov15/gh-14984
fix(model): make diffIndexes() avoid trying to drop default timeseries collection index
2 parents 71a0de3 + 61df30c commit 8799da2

File tree

3 files changed

+71
-2
lines changed

3 files changed

+71
-2
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
/**
4+
* Returns `true` if the given index matches the schema's `timestamps` options
5+
*/
6+
7+
module.exports = function isTimeseriesIndex(dbIndex, schemaOptions) {
8+
if (schemaOptions.timeseries == null) {
9+
return false;
10+
}
11+
const { timeField, metaField } = schemaOptions.timeseries;
12+
if (typeof timeField !== 'string' || typeof metaField !== 'string') {
13+
return false;
14+
}
15+
return Object.keys(dbIndex.key).length === 2 && dbIndex.key[timeField] === 1 && dbIndex.key[metaField] === 1;
16+
};

lib/model.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ const immediate = require('./helpers/immediate');
5151
const internalToObjectOptions = require('./options').internalToObjectOptions;
5252
const isDefaultIdIndex = require('./helpers/indexes/isDefaultIdIndex');
5353
const isIndexEqual = require('./helpers/indexes/isIndexEqual');
54+
const isTimeseriesIndex = require('./helpers/indexes/isTimeseriesIndex');
5455
const {
5556
getRelatedDBIndexes,
5657
getRelatedSchemaIndexes
@@ -1418,6 +1419,10 @@ function getIndexesToDrop(schema, schemaIndexes, dbIndexes) {
14181419
if (isDefaultIdIndex(dbIndex)) {
14191420
continue;
14201421
}
1422+
// Timeseries collections have a default index on { timeField: 1, metaField: 1 }.
1423+
if (isTimeseriesIndex(dbIndex, schema.options)) {
1424+
continue;
1425+
}
14211426

14221427
for (const [schemaIndexKeysObject, schemaIndexOptions] of schemaIndexes) {
14231428
const options = decorateDiscriminatorIndexOptions(schema, clone(schemaIndexOptions));
@@ -1429,9 +1434,11 @@ function getIndexesToDrop(schema, schemaIndexes, dbIndexes) {
14291434
}
14301435
}
14311436

1432-
if (!found) {
1433-
toDrop.push(dbIndex.name);
1437+
if (found) {
1438+
continue;
14341439
}
1440+
1441+
toDrop.push(dbIndex.name);
14351442
}
14361443

14371444
return toDrop;

test/model.test.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8147,6 +8147,52 @@ describe('Model', function() {
81478147
assert.ok(obj.post.updatedAt.valueOf(), new Date('2023-06-01T18:00:00.000Z').valueOf());
81488148
});
81498149
});
8150+
8151+
describe('diffIndexes()', function() {
8152+
it('avoids trying to drop timeseries collections (gh-14984)', async function() {
8153+
const version = await start.mongodVersion();
8154+
if (version[0] < 5) {
8155+
this.skip();
8156+
return;
8157+
}
8158+
8159+
const schema = new mongoose.Schema(
8160+
{
8161+
time: {
8162+
type: Date
8163+
},
8164+
deviceId: {
8165+
type: String
8166+
}
8167+
},
8168+
{
8169+
timeseries: {
8170+
timeField: 'time',
8171+
metaField: 'deviceId',
8172+
granularity: 'seconds'
8173+
},
8174+
autoCreate: false
8175+
}
8176+
);
8177+
8178+
const TestModel = db.model(
8179+
'TimeSeriesTest',
8180+
schema,
8181+
'gh14984'
8182+
);
8183+
8184+
await db.dropCollection('gh14984').catch(err => {
8185+
if (err.codeName === 'NamespaceNotFound') {
8186+
return;
8187+
}
8188+
throw err;
8189+
});
8190+
await TestModel.createCollection();
8191+
8192+
const { toDrop } = await TestModel.diffIndexes();
8193+
assert.deepStrictEqual(toDrop, []);
8194+
});
8195+
});
81508196
});
81518197

81528198

0 commit comments

Comments
 (0)