feat(backend): Verbesserung der Datenbank-Synchronisation und Deployment-Logik
- Einführung eines optionalen Parameters STAGE im Deployment-Skript zur flexiblen Handhabung von Umgebungen. - Anpassung der Datenbank-Synchronisation, um immer Schema-Updates im Deployment-Modus durchzuführen. - Umbenennung der Synchronisationsfunktion für Klarheit und bessere Lesbarkeit. - Erweiterung der Konsolenausgaben zur besseren Nachverfolgbarkeit während des Deployments.
This commit is contained in:
@@ -6,16 +6,16 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import './config/loadEnv.js';
|
import './config/loadEnv.js';
|
||||||
import { syncDatabase } from './utils/syncDatabase.js';
|
import { syncDatabaseForDeployment } from './utils/syncDatabase.js';
|
||||||
import cleanupDatabaseConstraints from './utils/cleanupDatabaseConstraints.js';
|
import cleanupDatabaseConstraints from './utils/cleanupDatabaseConstraints.js';
|
||||||
|
|
||||||
console.log('🗄️ Starte Datenbank-Synchronisation...');
|
console.log('🗄️ Starte Datenbank-Synchronisation (Deployment-Modus)...');
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
try {
|
try {
|
||||||
// 1. Datenbank-Synchronisation
|
// 1. Datenbank-Synchronisation mit Schema-Updates
|
||||||
console.log('📊 Führe Datenbank-Synchronisation durch...');
|
console.log('📊 Führe Datenbank-Synchronisation mit Schema-Updates durch...');
|
||||||
await syncDatabase();
|
await syncDatabaseForDeployment();
|
||||||
console.log('✅ Datenbank-Synchronisation erfolgreich abgeschlossen');
|
console.log('✅ Datenbank-Synchronisation erfolgreich abgeschlossen');
|
||||||
|
|
||||||
// 2. Constraint-Bereinigung
|
// 2. Constraint-Bereinigung
|
||||||
|
|||||||
@@ -384,4 +384,19 @@ async function updateFalukantUserMoney(falukantUserId, moneyChange, activity, ch
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export { sequelize, initializeDatabase, syncModels, syncModelsWithUpdates, updateSchema, updateFalukantUserMoney };
|
// Immer Schema-Updates (für Deployment)
|
||||||
|
const syncModelsAlways = async (models) => {
|
||||||
|
console.log('🔍 Deployment-Modus: Führe immer Schema-Updates durch...');
|
||||||
|
|
||||||
|
try {
|
||||||
|
for (const model of Object.values(models)) {
|
||||||
|
await model.sync({ alter: true, force: false });
|
||||||
|
}
|
||||||
|
console.log('✅ Schema-Updates für alle Models abgeschlossen');
|
||||||
|
} catch (error) {
|
||||||
|
console.error('❌ Fehler bei Schema-Updates:', error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export { sequelize, initializeDatabase, syncModels, syncModelsWithUpdates, syncModelsAlways, updateSchema, updateFalukantUserMoney };
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
// syncDatabase.js
|
// syncDatabase.js
|
||||||
|
|
||||||
import { initializeDatabase, syncModelsWithUpdates } from './sequelize.js';
|
import { initializeDatabase, syncModelsWithUpdates, syncModelsAlways } from './sequelize.js';
|
||||||
import initializeTypes from './initializeTypes.js';
|
import initializeTypes from './initializeTypes.js';
|
||||||
import initializeSettings from './initializeSettings.js';
|
import initializeSettings from './initializeSettings.js';
|
||||||
import initializeUserRights from './initializeUserRights.js';
|
import initializeUserRights from './initializeUserRights.js';
|
||||||
@@ -14,6 +14,7 @@ import initializeChat from './initializeChat.js';
|
|||||||
import initializeMatch3Data from './initializeMatch3.js';
|
import initializeMatch3Data from './initializeMatch3.js';
|
||||||
import updateExistingMatch3Levels from './updateExistingMatch3Levels.js';
|
import updateExistingMatch3Levels from './updateExistingMatch3Levels.js';
|
||||||
|
|
||||||
|
// Normale Synchronisation (nur bei STAGE=dev Schema-Updates)
|
||||||
const syncDatabase = async () => {
|
const syncDatabase = async () => {
|
||||||
try {
|
try {
|
||||||
// Zeige den aktuellen Stage an
|
// Zeige den aktuellen Stage an
|
||||||
@@ -74,4 +75,59 @@ const syncDatabase = async () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export { syncDatabase };
|
// Deployment-Synchronisation (immer Schema-Updates)
|
||||||
|
const syncDatabaseForDeployment = async () => {
|
||||||
|
try {
|
||||||
|
// Zeige den aktuellen Stage an
|
||||||
|
const currentStage = process.env.STAGE || 'nicht gesetzt';
|
||||||
|
console.log(`🚀 Starte Datenbank-Synchronisation für Deployment (Stage: ${currentStage})`);
|
||||||
|
console.log('✅ Deployment-Modus: Schema-Updates sind immer aktiviert');
|
||||||
|
|
||||||
|
console.log("Initializing database schemas...");
|
||||||
|
await initializeDatabase();
|
||||||
|
|
||||||
|
console.log("Synchronizing models with schema updates...");
|
||||||
|
await syncModelsAlways(models);
|
||||||
|
|
||||||
|
console.log("Setting up associations...");
|
||||||
|
setupAssociations();
|
||||||
|
|
||||||
|
console.log("Initializing settings...");
|
||||||
|
await initializeSettings();
|
||||||
|
|
||||||
|
console.log("Initializing types...");
|
||||||
|
await initializeTypes();
|
||||||
|
|
||||||
|
console.log("Initializing user rights...");
|
||||||
|
await initializeUserRights();
|
||||||
|
|
||||||
|
console.log("Initializing image types...");
|
||||||
|
await initializeImageTypes();
|
||||||
|
|
||||||
|
console.log("Initializing forums...");
|
||||||
|
await initializeForum();
|
||||||
|
|
||||||
|
console.log("Initializing Falukant...");
|
||||||
|
await initializeFalukant();
|
||||||
|
|
||||||
|
console.log("Creating triggers...");
|
||||||
|
await createTriggers();
|
||||||
|
|
||||||
|
console.log("Initializing chat...");
|
||||||
|
await initializeChat();
|
||||||
|
|
||||||
|
// Match3-Initialisierung NACH der Model-Synchronisation
|
||||||
|
console.log("Updating existing Match3 levels...");
|
||||||
|
await updateExistingMatch3Levels();
|
||||||
|
|
||||||
|
console.log("Initializing Match3...");
|
||||||
|
await initializeMatch3Data();
|
||||||
|
|
||||||
|
console.log('Database synchronization for deployment complete.');
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Unable to synchronize the database for deployment:', error);
|
||||||
|
throw error; // Fehler weiterwerfen
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export { syncDatabase, syncDatabaseForDeployment };
|
||||||
|
|||||||
@@ -1,6 +1,10 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
|
# Optionaler Parameter für STAGE (Standard: production)
|
||||||
|
STAGE=${1:-production}
|
||||||
|
|
||||||
echo "Deploying YourPart Backend..."
|
echo "Deploying YourPart Backend..."
|
||||||
|
echo "Using STAGE: $STAGE"
|
||||||
|
|
||||||
# Zum Backend-Verzeichnis wechseln
|
# Zum Backend-Verzeichnis wechseln
|
||||||
cd backend
|
cd backend
|
||||||
@@ -31,8 +35,8 @@ fi
|
|||||||
echo "Running database synchronization..."
|
echo "Running database synchronization..."
|
||||||
cd /opt/yourpart/backend
|
cd /opt/yourpart/backend
|
||||||
|
|
||||||
# STAGE auf dev setzen für Schema-Updates
|
# STAGE für Schema-Updates verwenden
|
||||||
echo "Setting STAGE=dev for schema updates..."
|
echo "Running database sync with STAGE=$STAGE..."
|
||||||
sudo -u www-data bash -c 'export STAGE=dev && npm run sync-db'
|
sudo -u www-data bash -c "export STAGE=$STAGE && npm run sync-db"
|
||||||
|
|
||||||
echo "Backend deployment completed!"
|
echo "Backend deployment completed!"
|
||||||
|
|||||||
Reference in New Issue
Block a user