181 lines
5.3 KiB
Bash
181 lines
5.3 KiB
Bash
#!/bin/bash
|
|
|
|
# Deployment Database Fix Script
|
|
# This script helps fix database schema issues on the deployed server
|
|
|
|
echo "================================================"
|
|
echo "LittleShop Database Schema Fix"
|
|
echo "================================================"
|
|
echo ""
|
|
|
|
# Configuration
|
|
DB_FILE="littleshop.db"
|
|
BACKUP_DIR="db-backups"
|
|
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
|
|
|
|
# Colors for output
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
NC='\033[0m' # No Color
|
|
|
|
# Function to check if database exists
|
|
check_database() {
|
|
if [ ! -f "$DB_FILE" ]; then
|
|
echo -e "${RED}Error: Database file $DB_FILE not found!${NC}"
|
|
echo "Please ensure you're running this script in the correct directory."
|
|
exit 1
|
|
fi
|
|
echo -e "${GREEN}✓ Database file found: $DB_FILE${NC}"
|
|
}
|
|
|
|
# Function to create backup
|
|
create_backup() {
|
|
echo -e "${YELLOW}Creating backup...${NC}"
|
|
|
|
# Create backup directory if it doesn't exist
|
|
mkdir -p "$BACKUP_DIR"
|
|
|
|
# Create backup with timestamp
|
|
BACKUP_FILE="$BACKUP_DIR/littleshop_${TIMESTAMP}.db"
|
|
cp "$DB_FILE" "$BACKUP_FILE"
|
|
|
|
if [ -f "$BACKUP_FILE" ]; then
|
|
echo -e "${GREEN}✓ Backup created: $BACKUP_FILE${NC}"
|
|
else
|
|
echo -e "${RED}Error: Failed to create backup!${NC}"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
# Function to check current schema
|
|
check_schema() {
|
|
echo -e "${YELLOW}Checking current database schema...${NC}"
|
|
|
|
# Check if migrations table exists
|
|
MIGRATIONS_TABLE=$(sqlite3 "$DB_FILE" "SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='__EFMigrationsHistory';")
|
|
|
|
if [ "$MIGRATIONS_TABLE" -eq "0" ]; then
|
|
echo -e "${YELLOW}! Migrations table not found - database may need full initialization${NC}"
|
|
else
|
|
echo -e "${GREEN}✓ Migrations table exists${NC}"
|
|
|
|
# List applied migrations
|
|
echo ""
|
|
echo "Applied migrations:"
|
|
sqlite3 "$DB_FILE" "SELECT MigrationId FROM __EFMigrationsHistory ORDER BY MigrationId;"
|
|
fi
|
|
|
|
# Check for key tables
|
|
echo ""
|
|
echo "Checking core tables..."
|
|
|
|
TABLES_TO_CHECK=("Products" "Orders" "Users" "Categories" "Customers" "ProductMultiBuys" "SystemSettings" "VariantCollections" "ProductVariants" "SalesLedger" "PushSubscriptions")
|
|
|
|
for table in "${TABLES_TO_CHECK[@]}"; do
|
|
EXISTS=$(sqlite3 "$DB_FILE" "SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='$table';")
|
|
if [ "$EXISTS" -eq "1" ]; then
|
|
COUNT=$(sqlite3 "$DB_FILE" "SELECT COUNT(*) FROM $table;")
|
|
echo -e "${GREEN}✓ $table exists (rows: $COUNT)${NC}"
|
|
else
|
|
echo -e "${YELLOW}✗ $table missing${NC}"
|
|
fi
|
|
done
|
|
}
|
|
|
|
# Function to apply migration
|
|
apply_migration() {
|
|
echo ""
|
|
echo -e "${YELLOW}Applying migration scripts...${NC}"
|
|
|
|
# Check which migration file to use
|
|
if [ -f "safe-migration.sql" ]; then
|
|
echo "Using safe-migration.sql"
|
|
MIGRATION_FILE="safe-migration.sql"
|
|
elif [ -f "apply-migration.sql" ]; then
|
|
echo "Using apply-migration.sql"
|
|
MIGRATION_FILE="apply-migration.sql"
|
|
elif [ -f "LittleShop/apply-migration.sql" ]; then
|
|
echo "Using LittleShop/apply-migration.sql"
|
|
MIGRATION_FILE="LittleShop/apply-migration.sql"
|
|
else
|
|
echo -e "${RED}Error: No migration file found!${NC}"
|
|
echo "Please ensure safe-migration.sql or apply-migration.sql is in the current directory."
|
|
exit 1
|
|
fi
|
|
|
|
# Apply the migration
|
|
echo "Applying $MIGRATION_FILE..."
|
|
OUTPUT=$(sqlite3 "$DB_FILE" < "$MIGRATION_FILE" 2>&1)
|
|
|
|
if [ $? -eq 0 ]; then
|
|
echo -e "${GREEN}✓ Migration applied successfully${NC}"
|
|
echo "$OUTPUT"
|
|
else
|
|
echo -e "${RED}Error applying migration:${NC}"
|
|
echo "$OUTPUT"
|
|
echo ""
|
|
echo -e "${YELLOW}Attempting to restore from backup...${NC}"
|
|
cp "$BACKUP_FILE" "$DB_FILE"
|
|
echo -e "${GREEN}✓ Database restored from backup${NC}"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
# Function to verify migration
|
|
verify_migration() {
|
|
echo ""
|
|
echo -e "${YELLOW}Verifying migration...${NC}"
|
|
|
|
# Check migrations table
|
|
MIGRATION_COUNT=$(sqlite3 "$DB_FILE" "SELECT COUNT(*) FROM __EFMigrationsHistory;" 2>/dev/null)
|
|
|
|
if [ -z "$MIGRATION_COUNT" ] || [ "$MIGRATION_COUNT" -eq "0" ]; then
|
|
echo -e "${RED}Warning: No migrations found in history${NC}"
|
|
else
|
|
echo -e "${GREEN}✓ $MIGRATION_COUNT migrations registered${NC}"
|
|
fi
|
|
|
|
# Final schema check
|
|
echo ""
|
|
echo "Final schema status:"
|
|
check_schema
|
|
}
|
|
|
|
# Main execution
|
|
main() {
|
|
echo "This script will:"
|
|
echo "1. Create a backup of your database"
|
|
echo "2. Check the current schema"
|
|
echo "3. Apply missing migrations"
|
|
echo "4. Verify the results"
|
|
echo ""
|
|
|
|
read -p "Continue? (y/n): " -n 1 -r
|
|
echo ""
|
|
|
|
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
|
|
echo "Aborted."
|
|
exit 1
|
|
fi
|
|
|
|
# Execute steps
|
|
check_database
|
|
create_backup
|
|
check_schema
|
|
apply_migration
|
|
verify_migration
|
|
|
|
echo ""
|
|
echo -e "${GREEN}================================================${NC}"
|
|
echo -e "${GREEN}Database migration completed successfully!${NC}"
|
|
echo -e "${GREEN}================================================${NC}"
|
|
echo ""
|
|
echo "Next steps:"
|
|
echo "1. Test the application to ensure it works correctly"
|
|
echo "2. If issues occur, restore from: $BACKUP_FILE"
|
|
echo ""
|
|
}
|
|
|
|
# Run main function
|
|
main |