feat(database): add migration and sync-db scripts to deployment process
Some checks failed
Deploy to production / deploy (push) Failing after 3m20s
Some checks failed
Deploy to production / deploy (push) Failing after 3m20s
- Introduced a new npm script for database migrations in package.json. - Updated update-backend.sh to execute migrations and conditionally run legacy sync-db based on environment variable. - Enhanced syncDatabase.js to skip initialization routines if APP_INIT_ON_START is not set, promoting best practices for schema changes during deployment.
This commit is contained in:
32
backend/config/sequelize-cli.cjs
Normal file
32
backend/config/sequelize-cli.cjs
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
const path = require('path');
|
||||||
|
const dotenv = require('dotenv');
|
||||||
|
|
||||||
|
const envPath = process.env.SEQUELIZE_ENV_FILE
|
||||||
|
? path.resolve(process.cwd(), process.env.SEQUELIZE_ENV_FILE)
|
||||||
|
: path.resolve(process.cwd(), '.env');
|
||||||
|
|
||||||
|
dotenv.config({ path: envPath });
|
||||||
|
|
||||||
|
const dialectOptions = {};
|
||||||
|
if (process.env.DB_SSL === '1' || process.env.PGSSLMODE === 'require') {
|
||||||
|
dialectOptions.ssl = process.env.DB_SSL_REJECT_UNAUTHORIZED === '0'
|
||||||
|
? { rejectUnauthorized: false }
|
||||||
|
: true;
|
||||||
|
}
|
||||||
|
|
||||||
|
const shared = {
|
||||||
|
username: process.env.DB_USER,
|
||||||
|
password: process.env.DB_PASS || '',
|
||||||
|
database: process.env.DB_NAME,
|
||||||
|
host: process.env.DB_HOST || '127.0.0.1',
|
||||||
|
port: Number.parseInt(process.env.DB_PORT || '5432', 10),
|
||||||
|
dialect: 'postgres',
|
||||||
|
logging: false,
|
||||||
|
dialectOptions
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
development: shared,
|
||||||
|
test: shared,
|
||||||
|
production: shared
|
||||||
|
};
|
||||||
@@ -9,6 +9,7 @@
|
|||||||
"dev": "NODE_ENV=development node server.js",
|
"dev": "NODE_ENV=development node server.js",
|
||||||
"start-daemon": "node daemonServer.js",
|
"start-daemon": "node daemonServer.js",
|
||||||
"sync-db": "node sync-database.js",
|
"sync-db": "node sync-database.js",
|
||||||
|
"db:migrate": "sequelize-cli db:migrate --config config/sequelize-cli.cjs --migrations-path migrations --env production",
|
||||||
"sync-tables": "node sync-tables-only.js",
|
"sync-tables": "node sync-tables-only.js",
|
||||||
"check-connections": "node check-connections.js",
|
"check-connections": "node check-connections.js",
|
||||||
"cleanup-connections": "node cleanup-connections.js",
|
"cleanup-connections": "node cleanup-connections.js",
|
||||||
|
|||||||
@@ -77,6 +77,13 @@ import initializeWidgetTypes from './initializeWidgetTypes.js';
|
|||||||
// Normale Synchronisation (nur bei STAGE=dev Schema-Updates)
|
// Normale Synchronisation (nur bei STAGE=dev Schema-Updates)
|
||||||
const syncDatabase = async () => {
|
const syncDatabase = async () => {
|
||||||
try {
|
try {
|
||||||
|
const runInitOnStart = process.env.APP_INIT_ON_START === '1';
|
||||||
|
if (!runInitOnStart) {
|
||||||
|
console.log('⏭️ APP_INIT_ON_START!=1: Überspringe datenbankseitige Initialisierungs-/Ensure-Routinen beim Start.');
|
||||||
|
console.log('💡 Empfehlung: Schema-Änderungen via Migrationen im Deploy ausführen (npm run db:migrate).');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Zeige den aktuellen Stage an
|
// Zeige den aktuellen Stage an
|
||||||
const currentStage = process.env.STAGE || 'nicht gesetzt';
|
const currentStage = process.env.STAGE || 'nicht gesetzt';
|
||||||
console.log(`🚀 Starte Datenbank-Synchronisation (Stage: ${currentStage})`);
|
console.log(`🚀 Starte Datenbank-Synchronisation (Stage: ${currentStage})`);
|
||||||
|
|||||||
@@ -62,4 +62,14 @@ echo "Running database synchronization..."
|
|||||||
cd "$BACKEND_DIR"
|
cd "$BACKEND_DIR"
|
||||||
export STAGE="$STAGE"
|
export STAGE="$STAGE"
|
||||||
|
|
||||||
|
echo "Führe Migrationen aus..."
|
||||||
|
npm run db:migrate
|
||||||
|
|
||||||
|
if [ "${RUN_LEGACY_SYNC_DB_ON_DEPLOY:-0}" = "1" ]; then
|
||||||
|
echo "RUN_LEGACY_SYNC_DB_ON_DEPLOY=1 gesetzt -> führe zusätzlich legacy sync-db aus..."
|
||||||
|
npm run sync-db
|
||||||
|
else
|
||||||
|
echo "Legacy sync-db übersprungen (Standard)."
|
||||||
|
fi
|
||||||
|
|
||||||
echo "Backend update completed!"
|
echo "Backend update completed!"
|
||||||
|
|||||||
Reference in New Issue
Block a user