require("dotenv").config(); const fs = require("fs"); const path = require("path"); const { MongoClient } = require("mongodb"); const DB_FILE = path.join(__dirname, "db.json"); async function migrate() { const uri = process.env.MONGODB_URI; if (!uri) { console.error("❌ MONGODB_URI not set in .env file!"); process.exit(1); } if (!fs.existsSync(DB_FILE)) { console.log("ℹ️ No local db.json file found. Nothing to migrate."); process.exit(0); } let localData; try { localData = JSON.parse(fs.readFileSync(DB_FILE, "utf8")); console.log("✅ Loaded local db.json successfully."); } catch (err) { console.error("❌ Failed to read or parse local db.json:", err.message); process.exit(1); } let client; try { console.log("Connecting to MongoDB..."); client = new MongoClient(uri); await client.connect(); const db = client.db(); const collection = db.collection("bot_state"); console.log("Migrating state to MongoDB..."); await collection.updateOne( { _id: "state" }, { $set: { data: localData } }, { upsert: true } ); console.log("🎉 Migration completed successfully!"); } catch (err) { console.error("❌ Migration failed:", err.message || err); } finally { if (client) { await client.close(); } } } migrate();