exercises

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

reset_schema.sh (1932B)


      1 #!/bin/bash
      2 # ============================================================
      3 # Reset Products Schema
      4 # ============================================================
      5 # Drops existing products schema and recreates with fresh data
      6 
      7 set -e
      8 
      9 # Database connection parameters
     10 DB_HOST="${DB_HOST:-localhost}"
     11 DB_PORT="${DB_PORT:-5433}"
     12 DB_NAME="${DB_NAME:-opentel_db}"
     13 DB_USER="${DB_USER:-opentel_user}"
     14 DB_PASSWORD="${DB_PASSWORD:-opentel_pass}"
     15 
     16 export PGPASSWORD="$DB_PASSWORD"
     17 
     18 echo "========================================="
     19 echo "Resetting Products Schema"
     20 echo "========================================="
     21 echo ""
     22 
     23 # Drop existing schema
     24 echo "1. Dropping existing products schema..."
     25 psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" <<EOF
     26 DROP SCHEMA IF EXISTS products CASCADE;
     27 EOF
     28 echo "   ✓ Schema dropped"
     29 echo ""
     30 
     31 # Apply new schema
     32 echo "2. Applying new schema..."
     33 psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -f init/00_init.sql
     34 psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -f init/products_schema_1.sql
     35 echo "   ✓ Schema created"
     36 echo ""
     37 
     38 # Ingest test data
     39 echo "3. Ingesting test data..."
     40 psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -f data/products_test_data.sql
     41 echo "   ✓ Data loaded"
     42 echo ""
     43 
     44 # Verify
     45 echo "4. Verifying data..."
     46 PRODUCT_COUNT=$(psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -t -c "SELECT COUNT(*) FROM products.products;")
     47 RATING_COUNT=$(psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -t -c "SELECT COUNT(*) FROM products.ratings;")
     48 CATEGORY_COUNT=$(psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -t -c "SELECT COUNT(*) FROM products.categories;")
     49 
     50 echo "   Products: $PRODUCT_COUNT"
     51 echo "   Ratings: $RATING_COUNT"
     52 echo "   Categories: $CATEGORY_COUNT"
     53 echo ""
     54 
     55 echo "========================================="
     56 echo "Schema reset complete!"
     57 echo "========================================="