Erster commit wie von Claude erstellt (unverändert)
This commit is contained in:
118
migrate.js
Normal file
118
migrate.js
Normal file
@@ -0,0 +1,118 @@
|
||||
import mysql from 'mysql2/promise';
|
||||
import { MongoClient } from 'mongodb';
|
||||
|
||||
// Konfiguration - ANPASSEN!
|
||||
const mysqlConfig = {
|
||||
host: 'DEIN_MYSQL_HOST',
|
||||
user: 'DEIN_MYSQL_USER',
|
||||
password: 'DEIN_MYSQL_PASSWORD',
|
||||
database: 'DEINE_MYSQL_DB'
|
||||
};
|
||||
|
||||
const mongoUri = 'mongodb://localhost:27017/recipes';
|
||||
|
||||
async function migrate() {
|
||||
let mysqlConn, mongoClient, db;
|
||||
|
||||
try {
|
||||
console.log('📦 Verbinde mit MySQL...');
|
||||
mysqlConn = await mysql.createConnection(mysqlConfig);
|
||||
|
||||
console.log('📦 Verbinde mit MongoDB...');
|
||||
mongoClient = new MongoClient(mongoUri);
|
||||
await mongoClient.connect();
|
||||
db = mongoClient.db();
|
||||
|
||||
console.log('🗑️ Lösche bestehende MongoDB-Daten...');
|
||||
await db.collection('recipes').deleteMany({});
|
||||
await db.collection('images').deleteMany({});
|
||||
|
||||
console.log('📋 Migriere Rezepte...');
|
||||
const [rezepte] = await mysqlConn.query('SELECT * FROM Rezepte ORDER BY Rezeptnummer');
|
||||
|
||||
let recipeCount = 0;
|
||||
let imageCount = 0;
|
||||
|
||||
for (const rezept of rezepte) {
|
||||
const rezeptnr = 'R' + String(rezept.Rezeptnummer).padStart(3, '0');
|
||||
|
||||
// Hole Zutaten aus ingredients
|
||||
const [ingredients] = await mysqlConn.query(
|
||||
'SELECT ingr FROM ingredients WHERE rezeptnr = ?',
|
||||
[rezeptnr]
|
||||
);
|
||||
const zutaten = ingredients.length > 0 ? ingredients[0].ingr : rezept.Zutaten || '';
|
||||
|
||||
// Hole Zubereitungsschritte
|
||||
const [zubereitungRows] = await mysqlConn.query(
|
||||
'SELECT schritt, text FROM Zubereitung WHERE rezeptnummer = ? ORDER BY schritt',
|
||||
[rezeptnr]
|
||||
);
|
||||
const zubereitung = zubereitungRows.map(row => ({
|
||||
schritt: row.schritt,
|
||||
text: row.text
|
||||
}));
|
||||
|
||||
// Erstelle MongoDB-Dokument
|
||||
const recipeDoc = {
|
||||
rezeptnummer: parseInt(rezept.Rezeptnummer),
|
||||
bezeichnung: rezept.Bezeichnung || '',
|
||||
beschreibung: rezept.Beschreibung || '',
|
||||
kategorie: rezept.Kategorie || '',
|
||||
vorbereitung: rezept.Vorbereitung || '',
|
||||
anzahl: rezept.Anzahl || 2,
|
||||
zutaten,
|
||||
zubereitung,
|
||||
kommentar: rezept.Kommentar || '',
|
||||
erstelltAm: new Date(),
|
||||
aktualisiertAm: new Date()
|
||||
};
|
||||
|
||||
const result = await db.collection('recipes').insertOne(recipeDoc);
|
||||
recipeCount++;
|
||||
|
||||
console.log(` ✓ Rezept ${rezeptnr}: ${rezept.Bezeichnung}`);
|
||||
|
||||
// Bilder migrieren
|
||||
const [bilder] = await mysqlConn.query(
|
||||
'SELECT * FROM rezepte_bilder WHERE rezepte_id = ?',
|
||||
[rezept.id]
|
||||
);
|
||||
|
||||
for (const bild of bilder) {
|
||||
const imageDoc = {
|
||||
rezeptId: result.insertedId,
|
||||
dateiPfad: bild.datei_pfad,
|
||||
dateiName: bild.datei_pfad.split('/').pop(),
|
||||
hochgeladenAm: new Date()
|
||||
};
|
||||
|
||||
await db.collection('images').insertOne(imageDoc);
|
||||
imageCount++;
|
||||
}
|
||||
}
|
||||
|
||||
// Erstelle Indizes
|
||||
console.log('📇 Erstelle Indizes...');
|
||||
await db.collection('recipes').createIndex({ rezeptnummer: 1 }, { unique: true });
|
||||
await db.collection('recipes').createIndex({
|
||||
bezeichnung: 'text',
|
||||
beschreibung: 'text',
|
||||
kategorie: 'text'
|
||||
});
|
||||
|
||||
console.log('\n✅ Migration abgeschlossen!');
|
||||
console.log(` ${recipeCount} Rezepte migriert`);
|
||||
console.log(` ${imageCount} Bilder migriert`);
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ Fehler bei der Migration:', error);
|
||||
process.exit(1);
|
||||
} finally {
|
||||
if (mysqlConn) await mysqlConn.end();
|
||||
if (mongoClient) await mongoClient.close();
|
||||
}
|
||||
}
|
||||
|
||||
// Script ausführen
|
||||
migrate();
|
||||
Reference in New Issue
Block a user