fix(backend): Anpassung der Indexfelder in mehreren Modelldateien auf snake_case
- Umbenennung der Indexfelder von camelCase auf snake_case in verschiedenen Modelldateien zur Verbesserung der Konsistenz mit den Datenbankkonventionen. - Verbesserung der Regex-Logik zur Erkennung und Ersetzung von Index-Definitionen und Feld-Arrays im Skript zur automatischen Korrektur.
This commit is contained in:
149
backend/fix-index-fields-v2.js
Normal file
149
backend/fix-index-fields-v2.js
Normal file
@@ -0,0 +1,149 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
/**
|
||||
* Script zur automatischen Korrektur aller Index-Felder - Version 2
|
||||
* 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;
|
||||
|
||||
// Prüfe ob die Datei Indexe enthält
|
||||
const hasIndexes = content.includes('indexes:') || content.includes('fields:');
|
||||
|
||||
if (hasIndexes) {
|
||||
console.log(`\n🔍 Prüfe: ${path.basename(filePath)} (enthält Indexe)`);
|
||||
|
||||
// Suche nach verschiedenen Index-Formaten
|
||||
const patterns = [
|
||||
// Standard-Format: fields: ['field1', 'field2']
|
||||
{
|
||||
regex: /fields:\s*\[([^\]]+)\]/g,
|
||||
name: 'Standard fields'
|
||||
},
|
||||
// Mit Leerzeichen: fields: [ 'field1', 'field2' ]
|
||||
{
|
||||
regex: /fields:\s*\[\s*([^\]]+)\s*\]/g,
|
||||
name: 'Fields mit Leerzeichen'
|
||||
}
|
||||
];
|
||||
|
||||
let foundIndexes = false;
|
||||
|
||||
for (const pattern of patterns) {
|
||||
let match;
|
||||
while ((match = pattern.regex.exec(content)) !== null) {
|
||||
foundIndexes = true;
|
||||
const fieldsString = match[1];
|
||||
console.log(` 📍 ${pattern.name}: ${fieldsString}`);
|
||||
|
||||
// Extrahiere Felder
|
||||
const fields = fieldsString.split(',').map(f => {
|
||||
const cleaned = f.trim().replace(/['"]/g, '');
|
||||
return cleaned;
|
||||
}).filter(f => f.length > 0);
|
||||
|
||||
console.log(` 📋 Gefundene Felder: [${fields.join(', ')}]`);
|
||||
|
||||
// Konvertiere Felder
|
||||
const correctedFields = fields.map(field => {
|
||||
if (field.includes('_')) {
|
||||
console.log(` ✅ ${field} (bereits snake_case)`);
|
||||
return field;
|
||||
}
|
||||
const corrected = camelToSnakeCase(field);
|
||||
if (corrected !== field) {
|
||||
console.log(` 🔄 ${field} → ${corrected}`);
|
||||
modified = true;
|
||||
}
|
||||
return corrected;
|
||||
});
|
||||
|
||||
// Ersetze in der Datei
|
||||
const oldFieldsString = `fields: [${fields.map(f => `'${f}'`).join(', ')}]`;
|
||||
const newFieldsString = `fields: [${correctedFields.map(f => `'${f}'`).join(', ')}]`;
|
||||
|
||||
if (oldFieldsString !== newFieldsString) {
|
||||
console.log(` 📝 Ersetze: ${oldFieldsString}`);
|
||||
console.log(` 📝 Durch: ${newFieldsString}`);
|
||||
content = content.replace(oldFieldsString, newFieldsString);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!foundIndexes) {
|
||||
console.log(` ⚠️ Datei enthält 'indexes:' aber keine 'fields:' Definitionen`);
|
||||
}
|
||||
|
||||
if (modified) {
|
||||
fs.writeFileSync(filePath, content, 'utf8');
|
||||
console.log(` ✅ Datei korrigiert und gespeichert`);
|
||||
} else {
|
||||
console.log(` ℹ️ Keine Änderungen nötig`);
|
||||
}
|
||||
} else {
|
||||
console.log(`\n🔍 Prüfe: ${path.basename(filePath)} (keine Indexe)`);
|
||||
}
|
||||
|
||||
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 (Version 2)...');
|
||||
|
||||
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);
|
||||
@@ -23,20 +23,24 @@ function fixModelFile(filePath) {
|
||||
let content = fs.readFileSync(filePath, 'utf8');
|
||||
let modified = false;
|
||||
|
||||
// Suche nach Index-Definitionen
|
||||
// Suche nach Index-Definitionen - verbesserte Regex
|
||||
const indexRegex = /indexes:\s*\[([\s\S]*?)\]/g;
|
||||
let match;
|
||||
|
||||
while ((match = indexRegex.exec(content)) !== null) {
|
||||
const indexesSection = match[1];
|
||||
|
||||
// Suche nach fields-Arrays
|
||||
// Suche nach fields-Arrays - verbesserte Regex
|
||||
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, ''));
|
||||
// Verbesserte Feld-Extraktion
|
||||
const fields = fieldsString.split(',').map(f => {
|
||||
const cleaned = f.trim().replace(/['"]/g, '');
|
||||
return cleaned;
|
||||
}).filter(f => f.length > 0);
|
||||
|
||||
// Konvertiere jedes Feld von camelCase zu snake_case
|
||||
const correctedFields = fields.map(field => {
|
||||
@@ -51,10 +55,15 @@ function fixModelFile(filePath) {
|
||||
return corrected;
|
||||
});
|
||||
|
||||
// Ersetze die fields-Array
|
||||
// Ersetze die fields-Array - verbesserte Ersetzung
|
||||
const oldFieldsString = `fields: [${fields.map(f => `'${f}'`).join(', ')}]`;
|
||||
const newFieldsString = `fields: [${correctedFields.map(f => `'${f}'`).join(', ')}]`;
|
||||
|
||||
// Debug-Ausgabe
|
||||
console.log(` 📍 Datei: ${path.basename(filePath)}`);
|
||||
console.log(` 🔍 Gefunden: ${oldFieldsString}`);
|
||||
console.log(` ✅ Ersetzt: ${newFieldsString}`);
|
||||
|
||||
content = content.replace(oldFieldsString, newFieldsString);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ Vote.init(
|
||||
indexes: [
|
||||
{
|
||||
unique: true,
|
||||
fields: ['electionId', 'candidateId']},
|
||||
fields: ['election_id', 'candidate_id']},
|
||||
]}
|
||||
);
|
||||
|
||||
|
||||
@@ -34,7 +34,7 @@ DayProduction.init({
|
||||
indexes: [
|
||||
{
|
||||
unique: true,
|
||||
fields: ['producerId', 'productId', 'regionId', 'productionDate']
|
||||
fields: ['producer_id', 'product_id', 'region_id', 'production_date']
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
@@ -30,7 +30,7 @@ DaySell.init({
|
||||
indexes: [
|
||||
{
|
||||
unique: true,
|
||||
fields: ['sellerId', 'productId', 'regionId']
|
||||
fields: ['seller_id', 'product_id', 'region_id']
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
@@ -20,7 +20,7 @@ BranchType.init({
|
||||
indexes: [
|
||||
{
|
||||
unique: true,
|
||||
fields: ['labelTr']
|
||||
fields: ['label_tr']
|
||||
}
|
||||
]});
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ HouseType.init({
|
||||
indexes: [
|
||||
{
|
||||
unique: true,
|
||||
fields: ['labelTr']
|
||||
fields: ['label_tr']
|
||||
}
|
||||
]});
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@ TitleRequirement.init({
|
||||
indexes: [
|
||||
{
|
||||
unique: true,
|
||||
fields: ['titleId', 'requirementType'],
|
||||
fields: ['title_id', 'requirement_type'],
|
||||
name: 'title_requirement_titleid_reqtype_unique'
|
||||
}
|
||||
]
|
||||
|
||||
@@ -45,7 +45,7 @@ const Match3LevelTileType = sequelize.define('Match3LevelTileType', {
|
||||
indexes: [
|
||||
{
|
||||
unique: true,
|
||||
fields: ['levelId', 'tileTypeId'] // WICHTIG: Bei underscored: true müssen snake_case Namen verwendet werden
|
||||
fields: ['level_id', 'tile_type_id'] // WICHTIG: Bei underscored: true müssen snake_case Namen verwendet werden
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
@@ -55,7 +55,7 @@ const UserProgress = sequelize.define('UserProgress', {
|
||||
indexes: [
|
||||
{
|
||||
unique: true,
|
||||
fields: ['userId', 'campaignId'] // WICHTIG: Bei underscored: true müssen snake_case Namen verwendet werden
|
||||
fields: ['user_id', 'campaign_id'] // WICHTIG: Bei underscored: true müssen snake_case Namen verwendet werden
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user