#!/bin/bash
# Extract only INSERT statements from production backup
# This avoids schema conflicts

BACKUP_FILE="/home/camp/projects/windows/shopdb/sql/prod_printers.sql"
OUTPUT_DIR="/home/camp/projects/windows/shopdb/sql"

echo "Extracting only INSERT statements from production data..."

# Extract only notifications INSERTs
echo "Extracting notifications INSERTs..."
grep "^INSERT INTO \`notifications\`" "$OUTPUT_DIR/prod_notifications.sql" > "$OUTPUT_DIR/prod_notifications_inserts.sql"
echo "SET FOREIGN_KEY_CHECKS = 0;" | cat - "$OUTPUT_DIR/prod_notifications_inserts.sql" > temp && mv temp "$OUTPUT_DIR/prod_notifications_inserts.sql"
echo "SET FOREIGN_KEY_CHECKS = 1;" >> "$OUTPUT_DIR/prod_notifications_inserts.sql"

# Extract only notificationtypes INSERTs
echo "Extracting notificationtypes INSERTs..."
grep "^INSERT INTO \`notificationtypes\`" "$OUTPUT_DIR/prod_notificationtypes.sql" > "$OUTPUT_DIR/prod_notificationtypes_inserts.sql"
echo "SET FOREIGN_KEY_CHECKS = 0;" | cat - "$OUTPUT_DIR/prod_notificationtypes_inserts.sql" > temp && mv temp "$OUTPUT_DIR/prod_notificationtypes_inserts.sql"
echo "SET FOREIGN_KEY_CHECKS = 1;" >> "$OUTPUT_DIR/prod_notificationtypes_inserts.sql"

# Extract only printers INSERTs
echo "Extracting printers INSERTs..."
grep "^INSERT INTO \`printers\`" "$OUTPUT_DIR/prod_printers.sql" > "$OUTPUT_DIR/prod_printers_inserts.sql"
echo "SET FOREIGN_KEY_CHECKS = 0;" | cat - "$OUTPUT_DIR/prod_printers_inserts.sql" > temp && mv temp "$OUTPUT_DIR/prod_printers_inserts.sql"
echo "SET FOREIGN_KEY_CHECKS = 1;" >> "$OUTPUT_DIR/prod_printers_inserts.sql"

# Extract only knowledgebase INSERTs
echo "Extracting knowledgebase INSERTs..."
grep "^INSERT INTO \`knowledgebase\`" "$OUTPUT_DIR/prod_knowledgebase.sql" > "$OUTPUT_DIR/prod_knowledgebase_inserts.sql"
echo "ALTER TABLE knowledgebase DISABLE KEYS;" | cat - "$OUTPUT_DIR/prod_knowledgebase_inserts.sql" > temp && mv temp "$OUTPUT_DIR/prod_knowledgebase_inserts.sql"
echo "ALTER TABLE knowledgebase ENABLE KEYS;" >> "$OUTPUT_DIR/prod_knowledgebase_inserts.sql"

echo ""
echo "Extraction complete!"
echo "Files created:"
ls -lh "$OUTPUT_DIR"/*_inserts.sql

echo ""
echo "INSERT statement counts:"
echo "Notifications: $(grep -c "^INSERT" "$OUTPUT_DIR/prod_notifications_inserts.sql")"
echo "Notification Types: $(grep -c "^INSERT" "$OUTPUT_DIR/prod_notificationtypes_inserts.sql")"
echo "Printers: $(grep -c "^INSERT" "$OUTPUT_DIR/prod_printers_inserts.sql")"
echo "Knowledgebase: $(grep -c "^INSERT" "$OUTPUT_DIR/prod_knowledgebase_inserts.sql")"
