FoxComm / highlander

Because there can only be one
MIT License
9 stars 3 forks source link

Migrate SKUs script integration #2057

Open pavel-main opened 7 years ago

pavel-main commented 7 years ago

https://github.com/FoxComm/highlander/blob/migrate_sku_script/migrate_sku.sh

TODO:

narma commented 7 years ago

Result of script execution: https://gist.github.com/narma/938513b67822c27c113d45560d4f5beb

IP: 10.240.0.53

some skus are failed, it's because of old bug and broken db, details and how to fix available here: https://github.com/FoxComm/highlander/issues/1594#issuecomment-298285056

select
  sku.code,
  illuminate_text(f, s, 'context') as ctx
  from skus as sku
  inner join object_forms as f on (sku.form_id = f.id)
  inner join object_shadows as s on (sku.shadow_id = s.id)
  where illuminate_text(f, s, 'context') is not null;

Gives to us list of broken skus

GFT-010
GFT-090
GFT-025
GFT-050
GFT-100
PG281
PG283

Also, I'm against writing such important things on bash, even python will be better.

I've used slightly modified version of scripts with some fixes:

#!/bin/bash 
set -ueo pipefail

usage() { echo "Usage: $0 -h <hostname> -e <email> -p <password> -o <org>" 1>&2; exit 1; }

while getopts ":h:e:p:o:" o; do
    case "${o}" in
        h)
            HOSTNAME=${OPTARG}
            ;;
        e)
            EMAIL=${OPTARG}
            ;;
        p)
            PASSWORD=${OPTARG}
            ;;
        o)
            ORG=${OPTARG}
            ;;
        *)
            usage
            ;;
    esac
done
shift $((OPTIND-1))

if [ -z "${HOSTNAME}" ] || [ -z "${EMAIL}" ] || [ -z "${PASSWORD}" ] || [ -z "${ORG}" ]; then
    usage
fi

echo "HOSTNAME = ${HOSTNAME}"
echo "EMAIL = ${EMAIL}"
echo "PASSWORD = ${PASSWORD}"
echo "ORG = ${ORG}"

CHUNK_SIZE=100

#GENERATE new JWT
JWT=$(curl "https://$HOSTNAME/api/v1/public/login" -H "Content-Type: application/json" --data-binary "{\"email\":\"$EMAIL\",\"password\":\"$PASSWORD\",\"org\":\"$ORG\"}" --compressed --silent -D - | grep Jwt: | cut -d ' ' -f 2)

if [[ $JWT == "" ]]; then
  echo "Unable to authenticate"
  exit 1
fi

#GET SKUS
SKU_NUM=$(curl "https://$HOSTNAME/api/search/admin/sku_search_view/_search?from=0&size=1" -H "JWT:$JWT" --silent | jq -r ".pagination.total")
echo "Found $SKU_NUM count of skus in ES"

END=$((SKU_NUM / CHUNK_SIZE))
for i in $(seq 0 $END);
do
  echo "Iteration $i out of $END"
  START=$(($i*$CHUNK_SIZE))
  SKUS=$(curl "https://$HOSTNAME/api/search/admin/sku_search_view/_search?from=$START&size=$CHUNK_SIZE" -H "JWT:$JWT" --silent --compressed | jq -r ".result | .[] | .skuCode")

  for sku in $SKUS
  do
    echo "Trying to GET sku: $sku"
    sku_id=$(curl "https://$HOSTNAME/api/v1/skus/default/$sku" -H "JWT:$JWT" --silent --compressed | jq -r ".id")
    echo "sku_id = $sku_id"
    if [[ -z "$sku_id" ]]; then
      echo "SKU with code: $sku does not exist in Phoenix"
      continue
    fi 

    echo "Patching: $sku with id: $sku_id"
    curl "https://$HOSTNAME/api/v1/skus/default/$sku" -X PATCH -H "JWT:$JWT" -H 'Content-Type: application/json;charset=UTF-8' --data-binary "{\"id\":$sku_id, \"attributes\":{\"code\":{\"t\":\"string\",\"v\":\"$sku\"}}}" --compressed --silent
done
done
annappropriate commented 7 years ago

@lurym ^

lurym commented 7 years ago

Thank you for verifying that. I agree with Python comment. I thought this would be simple script which got larger and larger. Sorry for that. What are the next steps? We need to run it in production environment.