nextav/tests/diagnostics/migrate-folder-bookmarks.sh

258 lines
7.7 KiB
Bash
Executable File

#!/bin/bash
# NextAV Folder Bookmarks Database Migration Script
# This script safely migrates your existing database to support folder bookmarks
set -e # Exit on any error
echo "🔧 NextAV Folder Bookmarks Database Migration"
echo "=============================================="
# Configuration
DB_PATH="${DB_PATH:-./data/media.db}"
BACKUP_DIR="./backups"
TIMESTAMP=$(date +%Y%m%d-%H%M%S)
BACKUP_FILE="${BACKUP_DIR}/media.db.backup-${TIMESTAMP}"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Function to print colored output
print_status() {
echo -e "${GREEN}[INFO]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Check if SQLite is installed
check_sqlite() {
if ! command -v sqlite3 &> /dev/null; then
print_error "SQLite3 is not installed. Please install SQLite3 first."
exit 1
fi
}
# Check if database file exists
check_database() {
if [ ! -f "$DB_PATH" ]; then
print_error "Database file not found at: $DB_PATH"
print_error "Please ensure the DB_PATH environment variable is set correctly."
exit 1
fi
print_status "Found database at: $DB_PATH"
}
# Create backup directory
create_backup_dir() {
if [ ! -d "$BACKUP_DIR" ]; then
mkdir -p "$BACKUP_DIR"
print_status "Created backup directory: $BACKUP_DIR"
fi
}
# Create database backup
create_backup() {
print_status "Creating database backup..."
# Check if database is in use (basic check)
if lsof "$DB_PATH" &> /dev/null; then
print_warning "Database appears to be in use. It's recommended to stop the application first."
read -p "Continue anyway? (y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
print_error "Migration cancelled."
exit 1
fi
fi
cp "$DB_PATH" "$BACKUP_FILE"
if [ $? -eq 0 ]; then
print_status "✅ Backup created successfully: $BACKUP_FILE"
else
print_error "❌ Failed to create backup"
exit 1
fi
}
# Verify current database schema
verify_schema() {
print_status "Verifying current database schema..."
# Check existing tables
TABLES=$(sqlite3 "$DB_PATH" ".tables")
if echo "$TABLES" | grep -q "media"; then
print_status "✅ Media table found"
else
print_error "❌ Media table not found"
exit 1
fi
if echo "$TABLES" | grep -q "bookmarks"; then
print_status "✅ Bookmarks table found"
else
print_error "❌ Bookmarks table not found"
exit 1
fi
# Check if folder_bookmarks already exists
if echo "$TABLES" | grep -q "folder_bookmarks"; then
print_warning "folder_bookmarks table already exists. Migration may have been run before."
read -p "Continue anyway? (y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
print_error "Migration cancelled."
exit 1
fi
fi
}
# Execute migration
execute_migration() {
print_status "Executing database migration..."
# SQL commands to add folder bookmarks support
sqlite3 "$DB_PATH" << EOF
-- Create folder_bookmarks table
CREATE TABLE IF NOT EXISTS folder_bookmarks (
id INTEGER PRIMARY KEY AUTOINCREMENT,
folder_path TEXT NOT NULL UNIQUE,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
-- Create index for performance
CREATE INDEX IF NOT EXISTS idx_folder_bookmarks_path ON folder_bookmarks(folder_path);
-- Verify the migration
SELECT 'folder_bookmarks table created successfully' as status
WHERE EXISTS (SELECT 1 FROM sqlite_master WHERE type='table' AND name='folder_bookmarks');
SELECT 'idx_folder_bookmarks_path index created successfully' as status
WHERE EXISTS (SELECT 1 FROM sqlite_master WHERE type='index' AND name='idx_folder_bookmarks_path');
EOF
if [ $? -eq 0 ]; then
print_status "✅ Migration executed successfully"
else
print_error "❌ Migration failed"
exit 1
fi
}
# Verify migration results
verify_migration() {
print_status "Verifying migration results..."
# Check if table was created
RESULT=$(sqlite3 "$DB_PATH" "SELECT name FROM sqlite_master WHERE type='table' AND name='folder_bookmarks';")
if [ -n "$RESULT" ]; then
print_status "✅ folder_bookmarks table verified"
else
print_error "❌ folder_bookmarks table not found"
exit 1
fi
# Check if index was created
RESULT=$(sqlite3 "$DB_PATH" "SELECT name FROM sqlite_master WHERE type='index' AND name='idx_folder_bookmarks_path';")
if [ -n "$RESULT" ]; then
print_status "✅ idx_folder_bookmarks_path index verified"
else
print_warning "⚠️ idx_folder_bookmarks_path index not found (this is not critical)"
fi
# Test that we can query the new table
COUNT=$(sqlite3 "$DB_PATH" "SELECT COUNT(*) FROM folder_bookmarks;")
print_status "✅ New table is accessible (current count: $COUNT)"
}
# Test API endpoints (if server is running)
test_api() {
print_status "Testing API endpoints..."
# Check if server is running
if curl -s -f "http://localhost:3000/api/health" &> /dev/null; then
print_status "Server is running, testing API endpoints..."
# Test folder bookmarks endpoint
if curl -s -f "http://localhost:3000/api/folder-bookmarks" &> /dev/null; then
print_status "✅ Folder bookmarks API endpoint is working"
else
print_warning "⚠️ Folder bookmarks API endpoint not responding (server may need restart)"
fi
# Test combined bookmarks endpoint
if curl -s -f "http://localhost:3000/api/bookmarks" &> /dev/null; then
print_status "✅ Combined bookmarks API endpoint is working"
else
print_warning "⚠️ Combined bookmarks API endpoint not responding"
fi
else
print_warning "⚠️ Server is not running - API tests skipped"
print_status "Please start your application and test the new functionality manually"
fi
}
# Print migration summary
print_summary() {
echo
echo "🎉 Migration completed successfully!"
echo "====================================="
echo
echo "📋 Summary:"
echo " • Database backup created: $BACKUP_FILE"
echo " • folder_bookmarks table added"
echo " • Performance index created"
echo " • API endpoints ready for use"
echo
echo "🚀 Next steps:"
echo " 1. Start your NextAV application"
echo " 2. Navigate to a folder in the folder viewer"
echo " 3. Click the bookmark icon to test folder bookmarking"
echo " 4. Go to the Bookmarks page to see your folder bookmarks"
echo
echo "🔄 Rollback instructions:"
echo " If you need to rollback, restore the backup:"
echo " cp $BACKUP_FILE $DB_PATH"
echo
echo "📞 Support:"
echo " If you encounter issues, check:"
echo " • Application logs: docker logs nextav-nextav-1"
echo " • Database integrity: sqlite3 $DB_PATH \".tables\""
echo " • API health: curl http://localhost:3000/api/health"
}
# Main execution
main() {
print_status "Starting NextAV folder bookmarks migration..."
check_sqlite
check_database
create_backup_dir
create_backup
verify_schema
execute_migration
verify_migration
test_api
print_summary
}
# Handle script interruption
trap 'print_error "Migration interrupted"; exit 1' INT TERM
# Run main function
main
echo
echo "✨ Migration process completed!"
echo " Your NextAV instance now supports folder bookmarking."
echo " Enjoy organizing your media libraries with bookmarks!"