Skip to content

Commit

Permalink
fix(model): make diffIndexes() avoid trying to drop default timeserie…
Browse files Browse the repository at this point in the history
…s collection index

Fix #14984
  • Loading branch information
vkarpov15 committed Nov 12, 2024
1 parent a0fdb9e commit 6a5ee71
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
16 changes: 16 additions & 0 deletions lib/helpers/indexes/isTimeseriesIndex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

/**
* Returns `true` if the given index matches the schema's `timestamps` options
*/

module.exports = function isTimeseriesIndex(dbIndex, schemaOptions) {
if (schemaOptions.timeseries == null) {
return false;
}
const { timeField, metaField } = schemaOptions.timeseries;
if (typeof timeField !== 'string' || typeof metaField !== 'string') {
return false;
}
return Object.keys(dbIndex.key).length === 2 && dbIndex.key[timeField] === 1 && dbIndex.key[metaField] === 1;
};
11 changes: 9 additions & 2 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const immediate = require('./helpers/immediate');
const internalToObjectOptions = require('./options').internalToObjectOptions;
const isDefaultIdIndex = require('./helpers/indexes/isDefaultIdIndex');
const isIndexEqual = require('./helpers/indexes/isIndexEqual');
const isTimeseriesIndex = require('./helpers/indexes/isTimeseriesIndex');
const {
getRelatedDBIndexes,
getRelatedSchemaIndexes
Expand Down Expand Up @@ -1418,6 +1419,10 @@ function getIndexesToDrop(schema, schemaIndexes, dbIndexes) {
if (isDefaultIdIndex(dbIndex)) {
continue;
}
// Timeseries collections have a default index on { timeField: 1, metaField: 1 }.
if (isTimeseriesIndex(dbIndex, schema.options)) {
continue;
}

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

if (!found) {
toDrop.push(dbIndex.name);
if (found) {
continue;
}

toDrop.push(dbIndex.name);
}

return toDrop;
Expand Down
40 changes: 40 additions & 0 deletions test/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8147,6 +8147,46 @@ describe('Model', function() {
assert.ok(obj.post.updatedAt.valueOf(), new Date('2023-06-01T18:00:00.000Z').valueOf());
});
});

describe('diffIndexes()', function() {
it('avoids trying to drop timeseries collections (gh-14984)', async function() {
const schema = new mongoose.Schema(
{
time: {
type: Date
},
deviceId: {
type: String
}
},
{
timeseries: {
timeField: 'time',
metaField: 'deviceId',
granularity: 'seconds'
},
autoCreate: false
}
);

const TestModel = db.model(
'TimeSeriesTest',
schema,
'gh14984'
);

await db.dropCollection('gh14984').catch(err => {
if (err.codeName === 'NamespaceNotFound') {
return;
}
throw err;
});
await TestModel.createCollection();

const { toDrop } = await TestModel.diffIndexes();
assert.deepStrictEqual(toDrop, []);
});
});
});


Expand Down

0 comments on commit 6a5ee71

Please sign in to comment.