Skip to content
Open
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
115 changes: 115 additions & 0 deletions migrations/mongo/aiUpdatesMigration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { MongoClient } from "mongodb";
import dotenv from "dotenv";
dotenv.config();
/**
* Migration script for ai_updates field reorganization
* Moves prompt_enhancer_percentage and criteria_check into ai_updates object for configurations only
*/

const MONGODB_CONNECTION_URI = process.env.MONGODB_CONNECTION_URI;

async function migrateAiUpdatesField() {
const client = new MongoClient(MONGODB_CONNECTION_URI);

try {
await client.connect();
console.log("Connected to MongoDB");

const db = client.db();
const collectionsToMigrate = ["configurations", "configuration_versions"];

for (const collectionName of collectionsToMigrate) {
const collection = db.collection(collectionName);
console.log(`\nStarting migration for ${collectionName}...`);

const cursor = collection.find({});
const batch = [];
let processed = 0;

for await (const doc of cursor) {
processed++;

// Skip if ai_updates already exists
if (doc.ai_updates) {
console.log(`Skipping document ${doc._id} in ${collectionName} - ai_updates already exists`);
continue;
}

// Create ai_updates object from existing fields
const ai_updates = {
prompt_enhancer_percentage: doc.prompt_enhancer_percentage || 0,
criteria_check: doc.criteria_check || {}
};

const updateOp = {
$set: { ai_updates: ai_updates },
$unset: {
prompt_enhancer_percentage: 1,
criteria_check: 1
}
};

batch.push({
updateOne: {
filter: { _id: doc._id },
update: updateOp
}
});

// Process batch every 100 documents
if (batch.length >= 100) {
await collection.bulkWrite(batch);
console.log(`Processed batch of ${batch.length} documents in ${collectionName} (total: ${processed})`);
batch.length = 0; // Clear batch
}
}

// Process remaining documents
if (batch.length > 0) {
await collection.bulkWrite(batch);
console.log(`Processed final batch of ${batch.length} documents in ${collectionName} (total: ${processed})`);
}

console.log(`Migration for ${collectionName} completed. Processed ${processed} documents.`);

// Verification - count documents with ai_updates
const count = await collection.countDocuments({ ai_updates: { $exists: true } });

console.log(`\n=== Migration Summary for ${collectionName} ===`);
console.log(`Documents with ai_updates: ${count}`);

// Verify old fields are removed
const oldCount = await collection.countDocuments({
$or: [{ prompt_enhancer_percentage: { $exists: true } }, { criteria_check: { $exists: true } }]
});

console.log(`Documents with old fields remaining: ${oldCount}`);

if (oldCount === 0) {
console.log(`✅ Migration for ${collectionName} completed successfully!`);
} else {
console.log(`⚠️ Some old fields remain in ${collectionName} - manual cleanup may be required`);
}
}
} catch (error) {
console.error("Migration failed:", error);
throw error;
} finally {
await client.close();
}
}

// Run migration if this file is executed directly
if (require.main === module) {
migrateAiUpdatesField()
.then(() => {
console.log("Migration completed successfully");
process.exit(0);
})
.catch((error) => {
console.error("Migration failed:", error);
process.exit(1);
});
}

module.exports = { migrateAiUpdatesField };
4 changes: 2 additions & 2 deletions src/controllers/template.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const FILTER_BRIDGE_EXCLUDE_KEYS = new Set([
"user_id",
"total_tokens",
"prompt_total_tokens",
"prompt_enhancer_percentage",
"ai_updates",
"bridge_usage",
"bridge_limit",
"bridge_limit_reset_period",
Expand Down Expand Up @@ -229,7 +229,7 @@ const createAgentFromTemplateController = async (req, res, next) => {
"IsstarterQuestionEnable",
"defaultQuestions",
"page_config",
"criteria_check",
"ai_updates",
"auto_model_select",
"connected_agent_details",
"meta",
Expand Down
7 changes: 6 additions & 1 deletion src/db_services/agentVersion.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,12 @@ async function getPromptEnhancerPercentage(parentId, prompt) {
// Update the document in the configurationModel
await configurationModel.updateOne(
{ _id: parentId },
{ $set: { prompt_enhancer_percentage: prompt_enhancer_percentage, criteria_check: criteria_check } }
{
$set: {
"ai_updates.prompt_enhancer_percentage": prompt_enhancer_percentage,
"ai_updates.criteria_check": criteria_check
Comment thread
Anushtha-Rathore marked this conversation as resolved.
}
}
Comment thread
Anushtha-Rathore marked this conversation as resolved.
);

return { prompt_enhancer_percentage, criteria_check };
Expand Down
4 changes: 2 additions & 2 deletions src/db_services/configuration.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -1156,8 +1156,8 @@ const getAllAgentsInOrg = async (org_id, folder_id, user_id, isEmbedUser) => {
createdAt: 1,
updatedAt: 1,
prompt_total_tokens: 1,
prompt_enhancer_percentage: 1,
criteria_check: 1
"ai_updates.prompt_enhancer_percentage": 1,
"ai_updates.criteria_check": 1
Comment thread
Anushtha-Rathore marked this conversation as resolved.
})
.sort({ createdAt: -1 })
.lean();
Expand Down
7 changes: 7 additions & 0 deletions src/mongoModel/BridgeVersion.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ const version = new mongoose.Schema({
type: Number,
default: 0
},
ai_updates: {
type: Object,
default: {
prompt_enhancer_percentage: 0,
criteria_check: {}
}
},
version_description: {
type: String,
default: ""
Expand Down
11 changes: 5 additions & 6 deletions src/mongoModel/Configuration.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,12 @@ const configuration = new mongoose.Schema({
type: Number,
default: 0
},
prompt_enhancer_percentage: {
type: Number,
default: 0
},
criteria_check: {
ai_updates: {
type: Object,
default: {}
default: {
prompt_enhancer_percentage: 0,
criteria_check: {}
}
},
created_at: {
type: Date,
Expand Down