mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-22 03:10:15 +01:00
🔧 refactor: Centralize Collection Checks for Permissions Migration (#9565)
* 🔧 refactor: Centralize Collection Existence Checks for Permissions Migration * Replace individual collection existence checks with a unified function `ensureRequiredCollectionsExist` in the database utility module. * Update migration scripts for agents and prompts to utilize the new function, ensuring all required collections are verified for existence in a single call. * Remove redundant collection existence logic from migration files, improving code maintainability and clarity. * chore: import order in migration scripts * 🔧 test: Update Token Test Cases for Realistic Scenarios * Changed email in test data to 'user1-alt@example.com' for a more realistic scenario. * Clarified expectation comment for token retrieval to indicate it finds the only matching token based on criteria.
This commit is contained in:
parent
a2ff6613c5
commit
85aa3e7d9c
7 changed files with 67 additions and 102 deletions
|
|
@ -1,7 +1,8 @@
|
|||
import { logger } from '@librechat/data-schemas';
|
||||
import { AccessRoleIds, ResourceType, PrincipalType, Constants } from 'librechat-data-provider';
|
||||
import { ensureRequiredCollectionsExist } from '../db/utils';
|
||||
import type { AccessRoleMethods, IPromptGroupDocument } from '@librechat/data-schemas';
|
||||
import type { Model, Mongoose, mongo } from 'mongoose';
|
||||
import type { Model, Mongoose } from 'mongoose';
|
||||
|
||||
const { GLOBAL_PROJECT_NAME } = Constants;
|
||||
|
||||
|
|
@ -52,32 +53,10 @@ export async function checkPromptPermissionsMigration({
|
|||
logger.debug('Checking if prompt permissions migration is needed');
|
||||
|
||||
try {
|
||||
/** Ensurse `aclentries` collection exists for DocumentDB compatibility */
|
||||
async function ensureCollectionExists(db: mongo.Db, collectionName: string) {
|
||||
try {
|
||||
const collections = await db.listCollections({ name: collectionName }).toArray();
|
||||
if (collections.length === 0) {
|
||||
await db.createCollection(collectionName);
|
||||
logger.info(`Created collection: ${collectionName}`);
|
||||
} else {
|
||||
logger.debug(`Collection already exists: ${collectionName}`);
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error(`'Failed to check/create "${collectionName}" collection:`, error);
|
||||
// If listCollections fails, try alternative approach
|
||||
try {
|
||||
// Try to access the collection directly - this will create it in MongoDB if it doesn't exist
|
||||
await db.collection(collectionName).findOne({}, { projection: { _id: 1 } });
|
||||
} catch (createError) {
|
||||
logger.error(`Could not ensure collection ${collectionName} exists:`, createError);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Native MongoDB database instance */
|
||||
const db = mongoose.connection.db;
|
||||
if (db) {
|
||||
await ensureCollectionExists(db, 'aclentries');
|
||||
await ensureRequiredCollectionsExist(db);
|
||||
}
|
||||
|
||||
// Verify required roles exist
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue