From e65ddb3b26457c0b4e467c60202702b05183b293 Mon Sep 17 00:00:00 2001 From: kanoi Date: Wed, 30 Sep 2015 10:48:01 +1000 Subject: [PATCH] sql - update rollback.sh to include a count report option and a blocks table option --- sql/rollback.sh | 115 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 97 insertions(+), 18 deletions(-) diff --git a/sql/rollback.sh b/sql/rollback.sh index c348d7ab..41df058d 100755 --- a/sql/rollback.sh +++ b/sql/rollback.sh @@ -13,12 +13,24 @@ # then you need to convince Kano to update the ckdb -y option to handle # that, with the current db, and also add code to the -y option to # update the DB -# i.e. the ckdb -y option needs updating +# i.e. the ckdb -y options/code need updating +# 6) Blocks commands manually entered using ckpmsg (confirm,orphan,reject,etc) +# will not be automatically restored if you use -b, you'll need to redo +# them again manually # # The basic use of this script is for when something goes wrong with the # database and the CCLs are OK - so you can roll back to before the # database problem and reload all the data # +# It may be unnecessary to delete/expire blocks records since the reload will +# attempt to load all old and new blocks records in the CCLs being +# reloaded, no matter what +# Also, deleting blocks history will lose all manual block changes +# done with confirms, orphans, rejects, etc +# However, if the block summarisations are wrong or the blocks table is +# corrupt, you will need to delete/expire them to correct them and then +# redo the manual blocks changes after reloading +# # Deleting markersummaries would only be an option if you know for sure # that all data is in the CCLs # It would be ideal to do a markersummary table dump before a rollback @@ -34,6 +46,8 @@ # to before the SEQ restart 0 point, then ckdb couldn't know this unless # the data was inconsistent # +# -c will show you record counts but not change anything +# # -e will of course allow you to compare all but markersummary, before and # after rolling back, by manually comparing the expired record with the # new replacement unexpired record - but it deletes the markersummary data @@ -53,11 +67,16 @@ # usAge() { - echo "usAge: `basename $0` [-n] -e|-r|-m workinfoid" + echo "usAge: `basename $0` [-b] [-n] -c|-e|-r|-m workinfoid" echo " Preferred option is just -e as long as you aren't missing reload data" + echo " The order of options must match the usAge order" + echo + echo " -b = include blocks when deleting/expiring (otherwise left untouched)" echo echo " -n = use 'now()' instead of the date timestamp of now, for expiry date" echo + echo " -c = report the counts of rows that would be affected, but don't do it" + echo echo " -e = rollback by deleteing expired data and expiring unexpired data," echo " but deleting markersummary" echo @@ -66,9 +85,9 @@ usAge() echo " but don't touch markersummary" echo " N.B. -m is expected to cause problems with ckdb" echo - echo " generates the sql on stdout" + echo " generates the sql on stdout, that you would feed into pgsql" echo - echo " Read `basename $0` for an explanation" + echo " Read `basename $0` for a more detailed explanation" exit 1 } # @@ -98,17 +117,37 @@ process() unex="expirydate > '6666-06-01'" oldex="expirydate < '6666-06-01'" # + if [ "$opt" = "-c" ] ; then + echo "\\echo 'count markersummary'" + echo "select count(*),min(markerid) as min_markerid,max(markerid) as max_markerid," + echo " sum(diffacc) as sum_diffacc,min(firstshareacc) as min_firstshareacc," + echo " max(lastshareacc) as max_lastshareacc" + echo " from markersummary where markerid in" + echo " (select distinct markerid from workmarkers where workinfoidend >= $wi);" + fi if [ "$opt" = "-r" -o "$opt" = "-e" ] ; then echo "\\echo 'delete markersummary'" echo "delete from markersummary where markerid in" echo " (select distinct markerid from workmarkers where workinfoidend >= $wi);" fi + # + if [ "$opt" = "-c" ] ; then + echo "\\echo 'count marks'" + echo "select count(*),min(workinfoid) as min_workinfoid," + echo " max(workinfoid) as max_workinfoid" + echo " from marks where workinfoid >= $wi;" + echo "\\echo 'count workmarkers'" + echo "select count(*),min(workinfoidstart) as min_workinfoidstart," + echo " max(workinfoidend) as max_workinfoidend" + echo " from workmarkers where workinfoidend >= $wi;" + fi if [ "$opt" = "-r" ] ; then echo "\\echo 'delete marks'" echo "delete from marks where workinfoid >= $wi;" echo "\\echo 'delete workmarkers'" echo "delete from workmarkers where workinfoidend >= $wi;" - else + fi + if [ "$opt" = "-e" -o "$opt" = "-m" ] ; then echo "\\echo 'delete/update marks'" echo "delete from marks where $oldex and workinfoid >= $wi;" echo "update marks set $ex where $unex and workinfoid >= $wi;" @@ -117,20 +156,51 @@ process() echo "update workmarkers set $ex where $unex and workinfoidend >= $wi;" fi # + if [ "$opt" = "-c" ] ; then + echo "\\echo 'count blocks'" + echo "select count(*),min(workinfoid) as min_workinfoid," + echo " max(workinfoid) as max_workinfoid" + echo " from blocks where workinfoid >= $wi;" + echo "\\echo 'count workinfo'" + echo "select count(*),min(workinfoid) as min_workinfoid," + echo " max(workinfoid) as max_workinfoid" + echo " from workinfo where workinfoid >= $wi;" + fi if [ "$opt" = "-r" ] ; then - echo "\\echo 'delete blocks'" - echo "delete from blocks where workinfoid >= $wi;" + if [ "$deblk" ] ; then + echo "\\echo 'delete blocks'" + echo "delete from blocks where workinfoid >= $wi;" + fi echo "\\echo 'delete workinfo'" echo "delete from workinfo where workinfoid >= $wi;" - else - echo "\\echo 'delete/update blocks'" - echo "delete from blocks where $oldex and workinfoid >= $wi;" - echo "update blocks set $ex where $unex and workinfoid >= $wi;" + fi + if [ "$opt" = "-e" -o "$opt" = "-m" ] ; then + if [ "$deblk" ] ; then + echo "\\echo 'delete/update blocks'" + echo "delete from blocks where $oldex and workinfoid >= $wi;" + echo "update blocks set $ex where $unex and workinfoid >= $wi;" + fi echo "\\echo 'delete/update workinfo'" echo "delete from workinfo where $oldex and workinfoid >= $wi;" echo "update workinfo set $ex where $unex and workinfoid >= $wi;" fi # + if [ "$opt" = "-c" ] ; then + echo "\\echo 'count miningpayouts'" + echo "select count(*),min(payoutid) as min_payoutid," + echo " max(payoutid) as max_payoutid" + echo " from miningpayouts where payoutid in" + echo " (select distinct payoutid from payouts where workinfoidend >= $wi);" + echo "\\echo 'count payments'" + echo "select count(*),min(payoutid) as min_payoutid," + echo " max(payoutid) as max_payoutid" + echo " from payments where payoutid in" + echo " (select distinct payoutid from payouts where workinfoidend >= $wi);" + echo "\\echo 'count payouts'" + echo "select count(*),min(workinfoidstart) as min_workinfoidstart," + echo " max(workinfoidend) as max_workinfoidend" + echo " from payouts where workinfoidend >= $wi;" + fi if [ "$opt" = "-r" ] ; then echo "\\echo 'delete miningpayouts'" echo "delete from miningpayouts where payoutid in" @@ -140,7 +210,8 @@ process() echo " (select distinct payoutid from payouts where workinfoidend >= $wi);" echo "\\echo 'delete payouts'" echo "delete from payouts where workinfoidend >= $wi;" - else + fi + if [ "$opt" = "-e" -o "$opt" = "-m" ] ; then echo "\\echo 'delete/update miningpayouts'" echo "delete from miningpayouts where $oldex and payoutid in" echo " (select distinct payoutid from payouts where workinfoidend >= $wi);" @@ -156,11 +227,13 @@ process() echo "update payouts set $ex where workinfoidend >= $wi and $unex;" fi # - idctl markerid workmarkers - idctl paymentid payments - idctl payoutid payouts - # this makes sure the worker data is consistent - don't remove this - idctl workerid workers + if [ "$opt" = "-e" -o "$opt" = "-m" -o "$opt" = "-r" ] ; then + idctl markerid workmarkers + idctl paymentid payments + idctl payoutid payouts + # this makes sure the worker data is consistent - don't remove this + idctl workerid workers + fi } # if [ -z "$1" ] ; then @@ -171,13 +244,19 @@ if [ "$1" = "-?" -o "$1" = "-h" -o "$1" = "-help" -o "$1" = "--help" ] ; then usAge fi # +deblk="" +if [ "$1" = "-b" ] ; then + deblk="y" + shift +fi +# usenow="" if [ "$1" = "-n" ] ; then usenow="y" shift fi # -if [ "$1" != "-r" -a "$1" != "-e" -a "$1" != "-m" ] ; then +if [ "$1" != "-c" -a "$1" != "-e" -a "$1" != "-r" -a "$1" != "-m" ] ; then echo "ERR: Unknown p1='$1'" usAge fi