littleshop/force-upgrade-production-db.sh

96 lines
2.9 KiB
Bash

#!/bin/bash
# Force upgrade production database - Preserves configuration and wallet data
# This script will recreate the database structure while keeping critical config
set -e # Exit on error
PRODUCTION_DB_PATH="/opt/littleshop/littleshop-production.db"
BACKUP_PATH="/opt/littleshop/backups/littleshop-production-backup-$(date +%Y%m%d_%H%M%S).db"
CONFIG_EXPORT="/tmp/littleshop-config-export.sql"
echo "🔧 Force Upgrade Production Database (Preserving Config)"
echo "========================================================"
echo ""
echo "📍 Database: $PRODUCTION_DB_PATH"
echo "💾 Will preserve: SystemSettings, Users, BotRegistrations"
echo ""
# Create backups directory if it doesn't exist
mkdir -p /opt/littleshop/backups
# Backup and export configuration if database exists
if [ -f "$PRODUCTION_DB_PATH" ]; then
echo "📦 Backing up existing database..."
cp "$PRODUCTION_DB_PATH" "$BACKUP_PATH"
echo "✅ Backup saved to: $BACKUP_PATH"
echo ""
echo "💾 Exporting configuration data..."
# Export critical tables to SQL
sqlite3 "$PRODUCTION_DB_PATH" <<'EXPORT_SQL' > "$CONFIG_EXPORT"
.mode insert SystemSettings
SELECT * FROM SystemSettings WHERE 1=1;
.mode insert Users
SELECT * FROM Users WHERE 1=1;
.mode insert BotRegistrations
SELECT * FROM BotRegistrations WHERE 1=1;
EXPORT_SQL
echo "✅ Configuration exported to: $CONFIG_EXPORT"
echo ""
fi
# Remove old database files
echo "🗑️ Removing old database files..."
rm -f "$PRODUCTION_DB_PATH"
rm -f "${PRODUCTION_DB_PATH}-shm"
rm -f "${PRODUCTION_DB_PATH}-wal"
echo "✅ Old database files removed"
echo ""
# Apply all migrations to create fresh database
echo "🔄 Applying all migrations..."
cd /opt/littleshop
dotnet ef database update --project LittleShop.csproj
if [ $? -eq 0 ]; then
echo "✅ Migrations applied successfully"
echo ""
# Restore configuration data if export exists
if [ -f "$CONFIG_EXPORT" ]; then
echo "♻️ Restoring configuration data..."
# Import the exported data
sqlite3 "$PRODUCTION_DB_PATH" < "$CONFIG_EXPORT"
echo "✅ Configuration data restored"
echo " - SystemSettings (wallet addresses, API keys)"
echo " - Users (admin accounts)"
echo " - BotRegistrations (Telegram bot config)"
echo ""
# Clean up export file
rm -f "$CONFIG_EXPORT"
fi
echo "✅ Database successfully upgraded!"
echo ""
echo "📊 Database info:"
echo " Path: $PRODUCTION_DB_PATH"
echo " Backup: $BACKUP_PATH"
echo ""
echo "🎉 Production database is now ready!"
echo ""
echo "⚠️ Note: Product catalog and orders were reset"
echo " Configuration and users were preserved"
else
echo ""
echo "❌ Migration failed!"
echo "🔄 Restoring from backup..."
cp "$BACKUP_PATH" "$PRODUCTION_DB_PATH"
echo "✅ Backup restored"
rm -f "$CONFIG_EXPORT"
exit 1
fi