feat(backend): Hinzufügen eines Skripts zur automatischen Korrektur von Index-Feldern
- Implementierung eines neuen Skripts, das camelCase-Feldnamen in snake_case für alle Modelldateien im models-Verzeichnis konvertiert. - Anpassung der Indexdefinitionen in mehreren Modelldateien zur Verbesserung der Konsistenz mit den Datenbankkonventionen. - Verbesserung der Konsolenausgaben zur Nachverfolgbarkeit der durchgeführten Änderungen.
This commit is contained in:
113
backend/fix-index-fields.js
Normal file
113
backend/fix-index-fields.js
Normal file
@@ -0,0 +1,113 @@
|
|||||||
|
#!/usr/bin/env node
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Script zur automatischen Korrektur aller Index-Felder
|
||||||
|
* Konvertiert camelCase zu snake_case für underscored: true Models
|
||||||
|
*/
|
||||||
|
|
||||||
|
import fs from 'fs';
|
||||||
|
import path from 'path';
|
||||||
|
import { fileURLToPath } from 'url';
|
||||||
|
|
||||||
|
const __filename = fileURLToPath(import.meta.url);
|
||||||
|
const __dirname = path.dirname(__filename);
|
||||||
|
|
||||||
|
// Funktion zur Konvertierung von camelCase zu snake_case
|
||||||
|
function camelToSnakeCase(str) {
|
||||||
|
return str.replace(/[A-Z]/g, letter => `_${letter.toLowerCase()}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Funktion zur Korrektur einer Model-Datei
|
||||||
|
function fixModelFile(filePath) {
|
||||||
|
try {
|
||||||
|
let content = fs.readFileSync(filePath, 'utf8');
|
||||||
|
let modified = false;
|
||||||
|
|
||||||
|
// Suche nach Index-Definitionen
|
||||||
|
const indexRegex = /indexes:\s*\[([\s\S]*?)\]/g;
|
||||||
|
let match;
|
||||||
|
|
||||||
|
while ((match = indexRegex.exec(content)) !== null) {
|
||||||
|
const indexesSection = match[1];
|
||||||
|
|
||||||
|
// Suche nach fields-Arrays
|
||||||
|
const fieldsRegex = /fields:\s*\[([^\]]+)\]/g;
|
||||||
|
let fieldsMatch;
|
||||||
|
|
||||||
|
while ((fieldsMatch = fieldsRegex.exec(indexesSection)) !== null) {
|
||||||
|
const fieldsString = fieldsMatch[1];
|
||||||
|
const fields = fieldsString.split(',').map(f => f.trim().replace(/['"]/g, ''));
|
||||||
|
|
||||||
|
// Konvertiere jedes Feld von camelCase zu snake_case
|
||||||
|
const correctedFields = fields.map(field => {
|
||||||
|
if (field.includes('_')) {
|
||||||
|
return field; // Bereits snake_case
|
||||||
|
}
|
||||||
|
const corrected = camelToSnakeCase(field);
|
||||||
|
if (corrected !== field) {
|
||||||
|
console.log(` 🔄 ${field} → ${corrected}`);
|
||||||
|
modified = true;
|
||||||
|
}
|
||||||
|
return corrected;
|
||||||
|
});
|
||||||
|
|
||||||
|
// Ersetze die fields-Array
|
||||||
|
const oldFieldsString = `fields: [${fields.map(f => `'${f}'`).join(', ')}]`;
|
||||||
|
const newFieldsString = `fields: [${correctedFields.map(f => `'${f}'`).join(', ')}]`;
|
||||||
|
|
||||||
|
content = content.replace(oldFieldsString, newFieldsString);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (modified) {
|
||||||
|
fs.writeFileSync(filePath, content, 'utf8');
|
||||||
|
console.log(`✅ ${filePath} korrigiert`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return modified;
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`❌ Fehler bei ${filePath}:`, error.message);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hauptfunktion
|
||||||
|
async function main() {
|
||||||
|
console.log('🔧 Starte automatische Korrektur aller Index-Felder...');
|
||||||
|
|
||||||
|
const modelsDir = path.join(__dirname, 'models');
|
||||||
|
let totalFiles = 0;
|
||||||
|
let modifiedFiles = 0;
|
||||||
|
|
||||||
|
// Rekursiv alle .js-Dateien im models-Verzeichnis durchsuchen
|
||||||
|
function processDirectory(dir) {
|
||||||
|
const items = fs.readdirSync(dir);
|
||||||
|
|
||||||
|
for (const item of items) {
|
||||||
|
const fullPath = path.join(dir, item);
|
||||||
|
const stat = fs.statSync(fullPath);
|
||||||
|
|
||||||
|
if (stat.isDirectory()) {
|
||||||
|
processDirectory(fullPath);
|
||||||
|
} else if (item.endsWith('.js')) {
|
||||||
|
totalFiles++;
|
||||||
|
if (fixModelFile(fullPath)) {
|
||||||
|
modifiedFiles++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
processDirectory(modelsDir);
|
||||||
|
|
||||||
|
console.log('\n🎉 Korrektur abgeschlossen!');
|
||||||
|
console.log(`📊 Gesamt: ${totalFiles} Dateien geprüft`);
|
||||||
|
console.log(`✅ Geändert: ${modifiedFiles} Dateien`);
|
||||||
|
|
||||||
|
if (modifiedFiles > 0) {
|
||||||
|
console.log('\n💡 Führen Sie jetzt das Deployment erneut aus:');
|
||||||
|
console.log(' sudo ./deploy-backend.sh');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
main().catch(console.error);
|
||||||
@@ -49,7 +49,7 @@ const Room = sequelize.define('Room', {
|
|||||||
indexes: [
|
indexes: [
|
||||||
{
|
{
|
||||||
name: 'idx_chat_room_owner',
|
name: 'idx_chat_room_owner',
|
||||||
fields: ['ownerId']},
|
fields: ['owner_id']},
|
||||||
]});
|
]});
|
||||||
|
|
||||||
export default Room;
|
export default Room;
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ Branch.init({
|
|||||||
indexes: [
|
indexes: [
|
||||||
{
|
{
|
||||||
unique: true,
|
unique: true,
|
||||||
fields: ['regionId', 'falukantUserId']
|
fields: ['region_id', 'falukant_user_id']
|
||||||
}
|
}
|
||||||
]});
|
]});
|
||||||
|
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ ProductType.init({
|
|||||||
indexes: [
|
indexes: [
|
||||||
{
|
{
|
||||||
unique: true,
|
unique: true,
|
||||||
fields: ['labelTr']
|
fields: ['label_tr']
|
||||||
}
|
}
|
||||||
]});
|
]});
|
||||||
|
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ const UserLevelProgress = sequelize.define('UserLevelProgress', {
|
|||||||
indexes: [
|
indexes: [
|
||||||
{
|
{
|
||||||
unique: true,
|
unique: true,
|
||||||
fields: ['userProgressId', 'levelId'] // WICHTIG: Bei underscored: true müssen snake_case Namen verwendet werden
|
fields: ['user_progress_id', 'level_id'] // WICHTIG: Bei underscored: true müssen snake_case Namen verwendet werden
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user