From: Nicolas Boisselier Date: Wed, 6 Dec 2017 03:19:45 +0000 (+0000) Subject: share/db/update.sh X-Git-Url: https://git.nbdom.net/?a=commitdiff_plain;h=ce697d27ed0571ff059ae360d2c33a432deac8a2;p=nb.git share/db/update.sh --- diff --git a/etc/profile b/etc/profile index 03ab2827..2532e38b 100755 --- a/etc/profile +++ b/etc/profile @@ -12,20 +12,24 @@ # 4. etc/profile.d/*.sh # #-------------------------------------------------------------------------------- -[ "$1" = "--nb-debug" ] && export NB_DEBUG="echo DEBUG: " && shift +[ "$1" = "--nb-debug" ] && export NB_DEBUG="yes" && shift +nb_debug() { + [ -n "$NB_DEBUG" -a "$NB_DEBUG" = "yes" ] || return 0 + echo "DEBUG: $*" 1>&2 + return 0 +} # # Avoid infinit loop. Eg: profile -> .bashrc -> profile # -#NB_DEBUG='echo ' if [ -n "$NB_LOOP" ]; then - [ -n "$NB_DEBUG" ] && $NB_DEBUG "WARN: NB_LOOP=$NB_LOOP: BASH_SOURCE=$BASH_SOURCE \$0=$0" + nb_debug "WARN: NB_LOOP=$NB_LOOP: BASH_SOURCE=$BASH_SOURCE \$0=$0" echo "WARN: NB_LOOP=$NB_LOOP: BASH_SOURCE=$BASH_SOURCE \$0=$0 | Won't load !" NB_LOOP=$(($NB_LOOP+1)) unset NB_LOOP return 2>/dev/null || exit # if executable fi -[ -n "$NB_DEBUG" ] && $NB_DEBUG "ENVS: BASH_SOURCE=$BASH_SOURCE \$0=$0" +nb_debug "ENVS: BASH_SOURCE=$BASH_SOURCE \$0=$0" #trap 'unset NB_LOOP' EXIT export NB_LOOP=1 @@ -77,7 +81,7 @@ _nb_unset() { if [ -z "$NB_ETC_DIR" -o "$NB_ETC_DIR" = "/" ]; then export NB_UNSUPORTED=1 - [ -n "$NB_DEBUG" ] && $NB_DEBUG "ERR: Unsupported shell. Please use bash, ksh93 or zsh." + nb_debug "ERR: Unsupported shell. Please use bash, ksh93 or zsh." else unset NB_UNSUPORTED @@ -85,7 +89,7 @@ else #echo $NB_ETC_DIR; #return # Check to avoid system crash ! - [ -n "$NB_DEBUG" ] && $NB_DEBUG ". $NB_ETC_DIR/profile.d/functions" + nb_debug ". $NB_ETC_DIR/profile.d/functions" if [ ! -e "$NB_ETC_DIR/profile.d/functions" ] || ! . "$NB_ETC_DIR/profile.d/functions"; then echo "ERR: $NB_ETC_DIR: Wrong directory (no profile.d/functions)" 1>&2 _nb_unset @@ -93,7 +97,7 @@ else fi export NB_ROOT=$(realpath $NB_ETC_DIR/.. 2>/dev/null) - [ -n "$NB_DEBUG" ] && $NB_DEBUG "NB_ROOT=$NB_ROOT" + nb_debug "NB_ROOT=$NB_ROOT" # # Profiles @@ -106,7 +110,7 @@ else ${NB_ROOT}/etc/profile.d/aliases \ $(nb_repos_ls "etc/profile" "etc/profile.d/*.sh" | awk '$0 != "'${NB_ROOT}/etc/profile'"') \ ;do - [ -n "$NB_DEBUG" ] && $NB_DEBUG ". $i" + nb_debug ". $i" [ -r "$i" -a -f "$i" ] && . "$i" done unalias which diff --git a/etc/profile.d/functions b/etc/profile.d/functions index e55e8f6c..77036322 100644 --- a/etc/profile.d/functions +++ b/etc/profile.d/functions @@ -43,7 +43,7 @@ nb_repos_ls() { for f in "$@"; do - #[ -n "$NB_DEBUG" ] && $NB_DEBUG "====$d/$f" + #nb_debug "====$d/$f" for repo in "$d"/$f; do [ -e "$repo" ] && echo "$repo" done @@ -847,14 +847,15 @@ which_cache() { # Cache case "$_which_cache|" in *"|$1=1|"*) - [ -n "$NB_DEBUG" ] && $NB_DEBUG "which_cache() $1=1" + nb_debug "which_cache() $1=1" return 0 ;; *"|$1=0|"*) - [ -n "$NB_DEBUG" ] && $NB_DEBUG "which_cache() $1=0" + nb_debug "which_cache() $1=0" return 1 ;; esac + #nb_debug "which_cache() $1=?" for p in $PATH; do test -x "$p/$1" || continue diff --git a/share/db/update.sh b/share/db/update.sh index 104f843f..e83cb31e 100755 --- a/share/db/update.sh +++ b/share/db/update.sh @@ -1,4 +1,9 @@ #!/bin/bash --login +# +# +# We want a new proper bash shell, we are going to search with `set` +# +# set +e DIR=$(dirname "$0") tmp=`mktemp -d` @@ -8,7 +13,9 @@ csv2table() { local file="$1"; shift cat << EOF +CREATE TEMPORARY TABLE IF NOT EXISTS _var (k varchar(10),v varhcar(10)); DELETE FROM _var; + DROP TABLE IF EXISTS ${name}_tmp; CREATE TEMPORARY TABLE ${name}_tmp AS SELECT * FROM $name WHERE 0; @@ -24,7 +31,7 @@ INSERT INTO _var VALUES('import',(SELECT count(*) FROM ${name}_tmp)); INSERT OR IGNORE INTO $name SELECT * FROM ${name}_tmp; INSERT INTO _var VALUES('post',(SELECT count(*) FROM ${name})); -SELECT 'Update sys:$name (' +SELECT 'Update $name (' || ((SELECT v FROM _var WHERE k='post')-(SELECT v FROM _var WHERE k='pre')) || '/' ||(SELECT v FROM _var WHERE k='import') @@ -47,7 +54,6 @@ cat "$DIR/*.sql" cat << EOF PRAGMA foreign_keys=OFF; .separator \t -CREATE TEMPORARY TABLE _var (k varchar(10),v varhcar(10)); `data_src`