Advertisement
Guest User

dbregionremoval

a guest
Feb 23rd, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.65 KB | None | 0 0
  1. #!/bin/bash
  2. echo
  3. echo Removing georegions, their associated regions and all links/entities that belong to those.
  4.  
  5. function runsql {
  6.  
  7.   if [ "$DRYRUN" = true ]; then
  8.     SQLTORUN="select count(*) $1"
  9.   else
  10.     SQLTORUN="delete $1"
  11.   fi
  12.  
  13.   echo "Executing: $SQLTORUN";
  14.   mysql --user="$USER" --password="$PASSWORD" --database="$DATABASE" --port "$PORT" -h "$HOST" --execute="$SQLTORUN" 2>&1 | grep -v "can be insecure"
  15. }
  16.  
  17. function help {
  18.     echo "Usage: removeGeoRegions.sh --user databaseusername --password databasepassword --database databasename --host hostname --removeGeoRegions comma_delimited_geo_region_ids --dryrun"
  19.     exit;
  20. }
  21.  
  22. while true; do
  23.   case "$1" in
  24.     --help )
  25.     help;
  26.     ;;
  27.     --host ) HOST="$2"; shift 2 ;;
  28.     --port ) PORT="$2"; shift 2 ;;
  29.     --user ) USER="$2"; shift 2 ;;
  30.     --password ) PASSWORD="$2"; shift 2 ;;
  31.     --database ) DATABASE="$2"; shift 2 ;;
  32.     --dryrun ) DRYRUN=true; shift ;;
  33.     --removeGeoRegions ) GEOREGIONSTOREMOVE="$2"; shift 2 ;;
  34.     * ) break ;;
  35.   esac
  36. done
  37.  
  38. if [ -z ${HOST+x} ]; then
  39.   echo "You must pass a --host option";
  40.   help;
  41.   exit;
  42. fi
  43.  
  44. if [ -z ${PORT+x} ]; then
  45.   echo "You must pass a --port option";
  46.   help;
  47.   exit;
  48. fi
  49.  
  50. if [ -z ${USER+x} ]; then
  51.   echo "You must pass a --user option";
  52.   help;
  53.   exit;
  54. fi
  55.  
  56. if [ -z ${PASSWORD+x} ]; then
  57.   echo "You must pass a --password option";
  58.   help;
  59.   exit;
  60. fi
  61.  
  62. if [ -z ${DATABASE+x} ]; then
  63.   echo "You must pass a --database option";
  64.   help;
  65.   exit;
  66. fi
  67.  
  68. if [ -z ${GEOREGIONSTOREMOVE+x} ]; then
  69.   echo "You must pass a --removeGeoRegions option";
  70.   help;
  71.   exit;
  72. fi
  73.  
  74.  
  75. if [ "$DRYRUN" = true ]; then
  76.   echo "NOTE: Running as a DRY RUN, this will only SELECT counts from the DB and will not alter anything"
  77. else
  78.   echo "WARNING: This is NOT a dry run, data will be deleted from the DB!";
  79. fi
  80. read -p "This will remove georegions: $GEOREGIONSTOREMOVE from database $DATABASE as well as the associated regions and all data linked to them.
  81. Continue? <y/N> " prompt
  82. if [[ $prompt =~ [yY](es)* ]]; then
  83.   echo "Removing data..."
  84.  
  85.   # Remove appointments and appointment_service
  86.   runsql "from appointments where region_id in (select id from regions where geographic_region_id in ($GEOREGIONSTOREMOVE));"
  87.  
  88.   # Remove service_accreditation
  89.   runsql "from service_accreditation where service_id in (select id from services where region_id in (select id from regions where geographic_region_id in ($GEOREGIONSTOREMOVE)));"
  90.  
  91.   # Remove services
  92.   runsql "from services where region_id in (select id from regions where geographic_region_id in ($GEOREGIONSTOREMOVE));"
  93.  
  94.   # Remove servicecats
  95.   runsql "from service_categories where region_id in (select id from regions where geographic_region_id in ($GEOREGIONSTOREMOVE));"
  96.  
  97.   # Remove calendar_accreditation
  98.   runsql "from calendar_accreditation where calendar_id in (select id from calendars where region_id in (select id from regions where geographic_region_id in ($GEOREGIONSTOREMOVE)));"
  99.  
  100.   # Remove calendar_service
  101.   runsql "from calendar_service where calendar_id in (select id from calendars where region_id in (select id from regions where geographic_region_id in ($GEOREGIONSTOREMOVE)));"
  102.  
  103.   # Remove counter_user
  104.   runsql "from counter_user where counter_id in (select id from counters where region_id in (select id from regions where geographic_region_id in ($GEOREGIONSTOREMOVE)));"
  105.  
  106.   # Remove favourite_availability_periods
  107.   runsql "from favourite_availability_periods where counter_id in (select id from counters where region_id in (select id from regions where geographic_region_id in ($GEOREGIONSTOREMOVE)));"
  108.  
  109.  
  110.   # Remove calendars
  111.   runsql "from calendars where region_id in (select id from regions where geographic_region_id in ($GEOREGIONSTOREMOVE));"
  112.  
  113.   # Remove counters
  114.   runsql "from counters where region_id in (select id from regions where geographic_region_id in ($GEOREGIONSTOREMOVE));"
  115.   # Remove references for users
  116.   # Special case since we are updating not deleting!
  117.  
  118.   if [ "$DRYRUN" = true ]; then
  119.     SQLTORUN="select count(*) from users where region_id in (select id from regions where geographic_region_id in ($GEOREGIONSTOREMOVE));"
  120.   else
  121.     SQLTORUN="update users set region_id = null where region_id in (select id from regions where geographic_region_id in ($GEOREGIONSTOREMOVE));"
  122.   fi
  123.   echo "Executing: $SQLTORUN";
  124.   mysql --user="$USER" --password="$PASSWORD" --database="$DATABASE" --port "$PORT" -h "$HOST" --execute="$SQLTORUN" 2>&1 | grep -v "can be insecure"
  125.  
  126.   # Remove help section entries
  127.   runsql "from help_section_region_mapping_brands where help_section_region_mappings_id in (select id from help_section_region_mapping where region_id in (select id from regions where geographic_region_id in (1)));"
  128.  
  129.   runsql "from help_section_region_mapping where region_id in (select id from regions where geographic_region_id in (1));"
  130.  
  131.   # Remove oauth clients:
  132.   runsql "from oauth_client_scopes where client_id in (select id from oauth_clients where region_id in (select id from regions where geographic_region_id in ($GEOREGIONSTOREMOVE)));"
  133.  
  134.   runsql "from oauth_clients where region_id in (select id from regions where geographic_region_id in ($GEOREGIONSTOREMOVE));"
  135.   # Remove accreditations
  136.   runsql "from accreditations where region_id in (select id from regions where geographic_region_id in ($GEOREGIONSTOREMOVE));"
  137.  
  138.   # Remove region_user
  139.   runsql "from region_user where region_id in (select id from regions where geographic_region_id in ($GEOREGIONSTOREMOVE));"
  140.  
  141.   # Remove regions
  142.   runsql "from regions where geographic_region_id in ($GEOREGIONSTOREMOVE);"
  143.  
  144.  
  145.   # Remove geographic_region_user
  146.   runsql "from geographic_region_user where geographic_region_id in ($GEOREGIONSTOREMOVE);"
  147.  
  148.   # Remove geographic_regions
  149.   runsql "from geographic_regions where id in ($GEOREGIONSTOREMOVE);"
  150.  
  151.   # Set passwords to null for users that no longer have a georegion or region (disable users):
  152.   # Special case since we are updating not deleting!
  153.   if [ "$DRYRUN" = true ]; then
  154.     SQLTORUN="select count(*) from users where has_regions_scope = 0 and id not in (select user_id from region_user) and id not in (select user_id from geographic_region_user) and deleted_at is null;"
  155.   else
  156.     SQLTORUN="update users set password = null where has_regions_scope = 0 and id not in (select user_id from region_user) and id not in (select user_id from geographic_region_user) and deleted_at is null;"
  157.   fi
  158.   echo "Executing: $SQLTORUN";
  159.   mysql --user="$USER" --password="$PASSWORD" --database="$DATABASE" --port "$PORT" -h "$HOST" --execute="$SQLTORUN" 2>&1 | grep -v "can be insecure"
  160.  
  161.  
  162.  
  163.   exit;
  164. else
  165.   echo "Operation cancelled."
  166.   exit;
  167. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement