mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 08:50:15 +01:00
fix: migrate old schema to new
This commit is contained in:
parent
0fa19bb6ad
commit
4fd05e15b4
6 changed files with 108 additions and 42 deletions
|
|
@ -1,44 +1,48 @@
|
||||||
const mongoose = require('mongoose');
|
const mongoose = require('mongoose');
|
||||||
|
const crypto = require('crypto');
|
||||||
const { getMessages, deleteMessages } = require('./Message');
|
const { getMessages, deleteMessages } = require('./Message');
|
||||||
|
|
||||||
const convoSchema = mongoose.Schema({
|
const convoSchema = mongoose.Schema(
|
||||||
conversationId: {
|
{
|
||||||
type: String,
|
conversationId: {
|
||||||
unique: true,
|
type: String,
|
||||||
required: true
|
unique: true,
|
||||||
|
required: true
|
||||||
|
},
|
||||||
|
parentMessageId: {
|
||||||
|
type: String,
|
||||||
|
required: true
|
||||||
|
},
|
||||||
|
title: {
|
||||||
|
type: String,
|
||||||
|
default: 'New Chat'
|
||||||
|
},
|
||||||
|
jailbreakConversationId: {
|
||||||
|
type: String
|
||||||
|
},
|
||||||
|
conversationSignature: {
|
||||||
|
type: String
|
||||||
|
},
|
||||||
|
clientId: {
|
||||||
|
type: String
|
||||||
|
},
|
||||||
|
invocationId: {
|
||||||
|
type: String
|
||||||
|
},
|
||||||
|
chatGptLabel: {
|
||||||
|
type: String
|
||||||
|
},
|
||||||
|
promptPrefix: {
|
||||||
|
type: String
|
||||||
|
},
|
||||||
|
model: {
|
||||||
|
type: String
|
||||||
|
},
|
||||||
|
suggestions: [{ type: String }],
|
||||||
|
messages: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Message' }]
|
||||||
},
|
},
|
||||||
parentMessageId: {
|
{ timestamps: true }
|
||||||
type: String,
|
);
|
||||||
required: true
|
|
||||||
},
|
|
||||||
title: {
|
|
||||||
type: String,
|
|
||||||
default: 'New Chat'
|
|
||||||
},
|
|
||||||
jailbreakConversationId: {
|
|
||||||
type: String
|
|
||||||
},
|
|
||||||
conversationSignature: {
|
|
||||||
type: String
|
|
||||||
},
|
|
||||||
clientId: {
|
|
||||||
type: String
|
|
||||||
},
|
|
||||||
invocationId: {
|
|
||||||
type: String
|
|
||||||
},
|
|
||||||
chatGptLabel: {
|
|
||||||
type: String
|
|
||||||
},
|
|
||||||
promptPrefix: {
|
|
||||||
type: String
|
|
||||||
},
|
|
||||||
model: {
|
|
||||||
type: String
|
|
||||||
},
|
|
||||||
suggestions: [{ type: String }],
|
|
||||||
messages: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Message' }],
|
|
||||||
}, { timestamps: true });
|
|
||||||
|
|
||||||
const Conversation =
|
const Conversation =
|
||||||
mongoose.models.Conversation || mongoose.model('Conversation', convoSchema);
|
mongoose.models.Conversation || mongoose.model('Conversation', convoSchema);
|
||||||
|
|
@ -114,5 +118,62 @@ module.exports = {
|
||||||
let deleteCount = await Conversation.deleteMany(filter).exec();
|
let deleteCount = await Conversation.deleteMany(filter).exec();
|
||||||
deleteCount.messages = await deleteMessages(filter);
|
deleteCount.messages = await deleteMessages(filter);
|
||||||
return deleteCount;
|
return deleteCount;
|
||||||
|
},
|
||||||
|
migrateDb: async () => {
|
||||||
|
try {
|
||||||
|
const conversations = await Conversation.find({ model: null }).exec();
|
||||||
|
|
||||||
|
if (!conversations || conversations.length === 0)
|
||||||
|
return { message: 'No conversations to migrate' };
|
||||||
|
|
||||||
|
for (let convo of conversations) {
|
||||||
|
const messages = await getMessages({
|
||||||
|
conversationId: convo.conversationId,
|
||||||
|
messageId: { $exists: false }
|
||||||
|
});
|
||||||
|
|
||||||
|
const promises = [];
|
||||||
|
let model;
|
||||||
|
let oldId;
|
||||||
|
messages.forEach((message, i) => {
|
||||||
|
const msgObj = message.toObject();
|
||||||
|
const newId = msgObj.id;
|
||||||
|
if (i === 0) {
|
||||||
|
message.parentMessageId = '00000000-0000-0000-0000-000000000000';
|
||||||
|
oldId = newId;
|
||||||
|
} else {
|
||||||
|
message.parentMessageId = oldId;
|
||||||
|
oldId = newId;
|
||||||
|
}
|
||||||
|
|
||||||
|
message.messageId = newId;
|
||||||
|
message.createdAt = message.created;
|
||||||
|
if (message.sender.toLowerCase() !== 'user' && !model) {
|
||||||
|
model = message.sender.toLowerCase();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (message.sender.toLowerCase() === 'user') {
|
||||||
|
message.isCreatedByUser = true;
|
||||||
|
}
|
||||||
|
promises.push(message.save());
|
||||||
|
});
|
||||||
|
await Promise.all(promises);
|
||||||
|
|
||||||
|
await Conversation.findOneAndUpdate(
|
||||||
|
{ conversationId: convo.conversationId },
|
||||||
|
{ model, createdAt: convo.created },
|
||||||
|
{ new: true }
|
||||||
|
).exec();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
await mongoose.connection.db.collection('messages').dropIndex('id_1');
|
||||||
|
} catch (error) {
|
||||||
|
console.log("Index doesn't exist or already dropped");
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error);
|
||||||
|
return { message: 'Error migrating conversations' };
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -64,7 +64,7 @@ module.exports = {
|
||||||
},
|
},
|
||||||
deleteMessagesSince: async ({ messageId, conversationId }) => {
|
deleteMessagesSince: async ({ messageId, conversationId }) => {
|
||||||
try {
|
try {
|
||||||
message = await Message.findOne({ messageId }).exec()
|
const message = await Message.findOne({ messageId }).exec()
|
||||||
|
|
||||||
if (message)
|
if (message)
|
||||||
return await Message.find({ conversationId }).deleteMany({ createdAt: { $gt: message.createdAt } }).exec();
|
return await Message.find({ conversationId }).deleteMany({ createdAt: { $gt: message.createdAt } }).exec();
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
const { saveMessage, deleteMessagesSince, deleteMessages } = require('./Message');
|
const { saveMessage, deleteMessagesSince, deleteMessages } = require('./Message');
|
||||||
const { getCustomGpts, updateCustomGpt, updateByLabel, deleteCustomGpts } = require('./CustomGpt');
|
const { getCustomGpts, updateCustomGpt, updateByLabel, deleteCustomGpts } = require('./CustomGpt');
|
||||||
const { getConvoTitle, getConvo, saveConvo } = require('./Conversation');
|
const { getConvoTitle, getConvo, saveConvo, migrateDb } = require('./Conversation');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
saveMessage,
|
saveMessage,
|
||||||
|
|
@ -9,6 +9,7 @@ module.exports = {
|
||||||
getConvoTitle,
|
getConvoTitle,
|
||||||
getConvo,
|
getConvo,
|
||||||
saveConvo,
|
saveConvo,
|
||||||
|
migrateDb,
|
||||||
getCustomGpts,
|
getCustomGpts,
|
||||||
updateCustomGpt,
|
updateCustomGpt,
|
||||||
updateByLabel,
|
updateByLabel,
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const dbConnect = require('../models/dbConnect');
|
const dbConnect = require('../models/dbConnect');
|
||||||
|
const { migrateDb } = require('../models');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const cors = require('cors');
|
const cors = require('cors');
|
||||||
const routes = require('./routes');
|
const routes = require('./routes');
|
||||||
|
|
@ -7,7 +8,10 @@ const app = express();
|
||||||
const port = process.env.PORT || 3080;
|
const port = process.env.PORT || 3080;
|
||||||
const host = process.env.HOST || 'localhost'
|
const host = process.env.HOST || 'localhost'
|
||||||
const projectPath = path.join(__dirname, '..', '..', 'client');
|
const projectPath = path.join(__dirname, '..', '..', 'client');
|
||||||
dbConnect().then(() => console.log('Connected to MongoDB'));
|
dbConnect().then(() => {
|
||||||
|
console.log('Connected to MongoDB');
|
||||||
|
migrateDb();
|
||||||
|
});
|
||||||
|
|
||||||
app.use(cors());
|
app.use(cors());
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ router.post('/', async (req, res) => {
|
||||||
|
|
||||||
const conversationId = oldConversationId || crypto.randomUUID();
|
const conversationId = oldConversationId || crypto.randomUUID();
|
||||||
|
|
||||||
const userMessageId = messageId;
|
const userMessageId = crypto.randomUUID();
|
||||||
const userParentMessageId = parentMessageId || '00000000-0000-0000-0000-000000000000'
|
const userParentMessageId = parentMessageId || '00000000-0000-0000-0000-000000000000'
|
||||||
let userMessage = {
|
let userMessage = {
|
||||||
messageId: userMessageId,
|
messageId: userMessageId,
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ router.post('/', async (req, res) => {
|
||||||
|
|
||||||
const conversationId = oldConversationId || crypto.randomUUID();
|
const conversationId = oldConversationId || crypto.randomUUID();
|
||||||
|
|
||||||
const userMessageId = messageId;
|
const userMessageId = crypto.randomUUID();
|
||||||
const userParentMessageId = parentMessageId || '00000000-0000-0000-0000-000000000000'
|
const userParentMessageId = parentMessageId || '00000000-0000-0000-0000-000000000000'
|
||||||
let userMessage = {
|
let userMessage = {
|
||||||
messageId: userMessageId,
|
messageId: userMessageId,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue