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

syncIndexes and diffIndexes does not correctly find compound indexes involving text and non-text indexe #13136

Closed
2 tasks done
rdeavila94 opened this issue Mar 6, 2023 · 1 comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@rdeavila94
Copy link
Contributor

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

5.x

Node.js version

14.x

MongoDB server version

4.x

Typescript version (if applicable)

No response

Description

Given a schema with the following:

      const Test = new Schema({
        title: {
          type: String
        },
        description: {
          type: String
        },
        age: {
          type: Number
        }
      }, {
        autoIndex: false
      })

Calling syncIndexes on that schema ALWAYS attempts to delete and create it.
Calling diffIndexes on that schema, even after it's been synced, always returns the compound index as both a toDrop entry and a toCreate entry

Steps to Reproduce

Run the following tests in test/model.indexes.test.js

    it('should not re-create a compound text index that involves non-text indexes, using syncIndexes', function(done) {
      const Test = new Schema({
        title: {
          type: String
        },
        description: {
          type: String
        },
        age: {
          type: Number
        }
      }, {
        autoIndex: false
      })
  
      Test.index({
        title:'text',
        description:'text',
        age: 1
      });

      const TestModel = db.model('Test', Test);
      TestModel.syncIndexes().then((results1) => {
        assert.strictEqual(results1, []);
        // second call to syncIndexes should return an empty array, representing 0 deleted indexes
        TestModel.syncIndexes().then((results2) => {
          assert.strictEqual(results2, []);
          done();
        })
      })
    })

    it('should not find a diff when calling diffIndexes after syncIndexes involving a text and non-text compound index', function(done) {
      const Test = new Schema({
        title: {
          type: String
        },
        description: {
          type: String
        },
        age: {
          type: Number
        }
      }, {
        autoIndex: false
      })
  
      Test.index({
        title:'text',
        description:'text',
        age: 1
      });

      const TestModel = db.model('Test', Test);
      
      TestModel.diffIndexes().then((diff) => {
        assert.deepEqual(diff, {toCreate: [{age: 1, title: 'text', description:'text'}], toDrop: []});
        TestModel.syncIndexes().then(() => {
          TestModel.diffIndexes().then((diff2) => {
            assert.deepEqual(diff2, {toCreate: [], toDrop: []});
            done();
          })
        })
      })
    })

Expected Behavior

The above tests to pass.

Calling diffIndexes on a schema that's been synced, where that schema contains a compound index involving both text and non-text indexes, should not return any diff at all.

@rdeavila94
Copy link
Contributor Author

Opened up a PR to fix this. Created this issue to have a github issue reference number

@vkarpov15 vkarpov15 added this to the 5.13.17 milestone Mar 7, 2023
@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Mar 10, 2023
vkarpov15 added a commit that referenced this issue Mar 13, 2023
Updated the isIndexEqual function to take into account non-text index…
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

3 participants