Skip to content

Commit

Permalink
ISSUE #5412 drivers test passing
Browse files Browse the repository at this point in the history
  • Loading branch information
carmenfan committed Mar 7, 2025
1 parent 9365537 commit 6ce6200
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion backend/src/v5/models/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ User.removeUsers = (users) => db.deleteMany(USERS_DB_NAME, USERS_COL, { user: {

User.ensureIndicesExist = async () => {
try {
await db.createIndex(USERS_DB_NAME, USERS_COL, { 'customData.userId': 1 }, { runInBackground: true, unique: true });
await db.createIndex(USERS_DB_NAME, USERS_COL, { 'customData.userId': 1 }, { runInBackground: true });
} catch (err) {
// Note this will fail pre 5.16 migration.
logger.logWarning('Failed to create index on user ID. Please ensure 5.16 migration script has been executed.');
Expand Down
16 changes: 16 additions & 0 deletions backend/tests/v5/drivers/handler/db.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,22 @@ const testIndices = () => {
const array = res.map(({ key }) => key);
expect(array).toEqual(expect.arrayContaining([{ _id: 1 }, ...newIndex.map(({ key }) => key)]));
});

test('Should be able to create unique index', async () => {
const database = generateRandomString();
const col = generateRandomString();

await DB.insertMany(database, col, [{ n: 2 }, { a: 1 }]);

const newIndex = { n: 1 };
await expect(DB.createIndex(database, col, newIndex, { unique: true })).resolves.toBeUndefined();

await DB.insertOne(database, col, newIndex);
// Unique index means we shouldn't be able to insert another one with the same value.
await expect(DB.insertOne(database, col, newIndex)).rejects.not.toBeUndefined();
// Unique index means we shouldn't be able to insert another one that is empty
await expect(DB.insertOne(database, col, { b: 1 })).rejects.not.toBeUndefined();
});
});
};

Expand Down

0 comments on commit 6ce6200

Please sign in to comment.