Skip to content

Commit

Permalink
Merge pull request #15035 from Automattic/vkarpov15/gh-14984
Browse files Browse the repository at this point in the history
fix(model): make diffIndexes() avoid trying to drop default timeseries collection index
  • Loading branch information
vkarpov15 authored Nov 13, 2024
2 parents 71a0de3 + 61df30c commit 8799da2
Show file tree
Hide file tree
Showing 3 changed files with 71 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
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

0 comments on commit 8799da2

Please sign in to comment.