Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(model): make diffIndexes() avoid trying to drop default timeseries collection index #15035

Merged
merged 2 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
46 changes: 46 additions & 0 deletions test/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8147,6 +8147,52 @@ 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 version = await start.mongodVersion();
if (version[0] < 5) {
this.skip();
return;
}

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