From b43b9ae8b3a7d0538f1147512c31c02556333e64 Mon Sep 17 00:00:00 2001 From: Samuel Githengi Date: Wed, 13 Jun 2018 18:15:31 +0300 Subject: [PATCH 1/3] View configuration setup script add postgres support --- assets/tbreach_default_view_configs/README.txt | 13 +++++++++++++ .../setup_view_configs.sh | 18 +++++++++++++++--- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/assets/tbreach_default_view_configs/README.txt b/assets/tbreach_default_view_configs/README.txt index f45f81b56e..cb7e7e9f26 100644 --- a/assets/tbreach_default_view_configs/README.txt +++ b/assets/tbreach_default_view_configs/README.txt @@ -1,11 +1,24 @@ This folder contains the default view configuration json files as deployed with this Release Version of TB Reach as well as the Shell Script to populate the db with them. + +The script works with both postgres and couchdb. + +For couchDb Make sure you have set up couch database on this machine and that couch is accessible via port 5894 (You can modify the script if you use a different port) Run the command below: ./setup_view_configs.sh -d (e.g. ./setup_view_configs.sh -d opensrp_tbreach) +For Postgres + +You must specify the parameter -t with value postgres. The default is couchdb +Make sure you have set up postgres database on this machine and that postgres is accessible via port 5432 (You can modify the script if you use a different port) +Run the command below: + +./setup_view_configs.sh -t postgres -u -pwd -d opensrp -d (e.g. ./setup_view_configs.sh -t postgres -u opensrp_admin -pwd admin -d opensrp_tbreach) + Run ./setup_view_configs.sh --help to get a list of available options + Specify (Optional) database type like ./setup_view_configs.sh -t postgres Specify (Optional) a database name like ./setup_view_configs.sh -d diff --git a/assets/tbreach_default_view_configs/setup_view_configs.sh b/assets/tbreach_default_view_configs/setup_view_configs.sh index d0e6343ae7..d81b353a75 100755 --- a/assets/tbreach_default_view_configs/setup_view_configs.sh +++ b/assets/tbreach_default_view_configs/setup_view_configs.sh @@ -8,10 +8,12 @@ PASSWORD=0 CONFIG_FOLDER=. count="$#" PROTO=0 +TYPE='couchdb' if [ "$1" == "--help" ] then + echo -e "Specify (Optional) database type like \033[33m./setup_view_configs.sh -t postgres \033[0m" echo -e "Specify (Optional) a database name like \033[33m./setup_view_configs.sh -d \033[0m" echo -e "Specify (Optional) a host like \033[33m ./setup_view_configs.sh -h \033[0m" echo -e "Specify (Optional) a port name like \033[33m ./setup_view_configs.sh -p \033[0m" @@ -34,6 +36,10 @@ else #set the params for var in "$@" do + if [ $varPrev == "-t" ];then + TYPE="$var" + fi + if [ $varPrev == "-d" ];then DATABASE="$var" fi @@ -69,7 +75,8 @@ else echo -e "\033[;32m Connecting to host \033[1;37m$HOST ...\033[0m" echo -e "\033[;32m Connecting on port \033[0m$PORT" - echo -e "\033[;32m Couch Database specified \033[0m$DATABASE" + echo -e "\033[;32m Database specified \033[0m$TYPE" + echo -e "\033[;32m Database specified \033[0m$DATABASE" if [ $CONFIG_FOLDER == "." ]; then echo -e "\033[;32m View Configuration folder supplied \033[0m $(pwd)" @@ -107,8 +114,13 @@ else if [ $PASSWORD != 0 ]; then - - curl -H 'Content-Type: application/json' -vX POST $PROTO://$USERNAME:$PASSWORD@$HOST:$PORT/$DATABASE -d @$f + if [ $TYPE = 'couchdb' ]; then + curl -H 'Content-Type: application/json' -vX POST $PROTO://$USERNAME:$PASSWORD@$HOST:$PORT/$DATABASE -d @$f + else + jsonString=`cat $f | sed "s/'/''/g"` + view_id=$(PGPASSWORD=$PASSWORD psql -qtAX -U $USERNAME -h $HOST -d $DATABASE -c "INSERT INTO core.view_configuration(json) VALUES('$jsonString');SELECT currval('core.\"view_configuration_id_seq\"');") + PGPASSWORD=$PASSWORD psql -U $USERNAME -h $HOST -d $DATABASE -c "INSERT INTO core.view_configuration_metadata(view_configuration_id,document_id,identifier,server_version) SELECT $view_id,json->>'_id',json->>'identifier',(json->>'serverVersion')::BIGINT FROM core.view_configuration WHERE id=$view_id" + fi else curl -H 'Content-Type: application/json' -vX POST $PROTO://$USERNAME@$HOST:$PORT/$DATABASE -d @$f From 5180158eddb69620be476eaca08b7647d77ae660 Mon Sep 17 00:00:00 2001 From: Samuel Githengi Date: Thu, 14 Jun 2018 10:12:38 +0300 Subject: [PATCH 2/3] Code cleanup --- assets/tbreach_default_view_configs/setup_view_configs.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assets/tbreach_default_view_configs/setup_view_configs.sh b/assets/tbreach_default_view_configs/setup_view_configs.sh index d81b353a75..b42bc83237 100755 --- a/assets/tbreach_default_view_configs/setup_view_configs.sh +++ b/assets/tbreach_default_view_configs/setup_view_configs.sh @@ -117,7 +117,7 @@ else if [ $TYPE = 'couchdb' ]; then curl -H 'Content-Type: application/json' -vX POST $PROTO://$USERNAME:$PASSWORD@$HOST:$PORT/$DATABASE -d @$f else - jsonString=`cat $f | sed "s/'/''/g"` + jsonString=$(cat $f | sed "s/'/''/g") view_id=$(PGPASSWORD=$PASSWORD psql -qtAX -U $USERNAME -h $HOST -d $DATABASE -c "INSERT INTO core.view_configuration(json) VALUES('$jsonString');SELECT currval('core.\"view_configuration_id_seq\"');") PGPASSWORD=$PASSWORD psql -U $USERNAME -h $HOST -d $DATABASE -c "INSERT INTO core.view_configuration_metadata(view_configuration_id,document_id,identifier,server_version) SELECT $view_id,json->>'_id',json->>'identifier',(json->>'serverVersion')::BIGINT FROM core.view_configuration WHERE id=$view_id" fi From f99546de412fac60d5647f1e514e52d37156784f Mon Sep 17 00:00:00 2001 From: Samuel Githengi Date: Thu, 14 Jun 2018 10:33:24 +0300 Subject: [PATCH 3/3] fix codacy issue UUOC --- assets/tbreach_default_view_configs/setup_view_configs.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assets/tbreach_default_view_configs/setup_view_configs.sh b/assets/tbreach_default_view_configs/setup_view_configs.sh index b42bc83237..f7ad02db89 100755 --- a/assets/tbreach_default_view_configs/setup_view_configs.sh +++ b/assets/tbreach_default_view_configs/setup_view_configs.sh @@ -117,7 +117,7 @@ else if [ $TYPE = 'couchdb' ]; then curl -H 'Content-Type: application/json' -vX POST $PROTO://$USERNAME:$PASSWORD@$HOST:$PORT/$DATABASE -d @$f else - jsonString=$(cat $f | sed "s/'/''/g") + jsonString=$(sed "s/'/''/g" < $f) view_id=$(PGPASSWORD=$PASSWORD psql -qtAX -U $USERNAME -h $HOST -d $DATABASE -c "INSERT INTO core.view_configuration(json) VALUES('$jsonString');SELECT currval('core.\"view_configuration_id_seq\"');") PGPASSWORD=$PASSWORD psql -U $USERNAME -h $HOST -d $DATABASE -c "INSERT INTO core.view_configuration_metadata(view_configuration_id,document_id,identifier,server_version) SELECT $view_id,json->>'_id',json->>'identifier',(json->>'serverVersion')::BIGINT FROM core.view_configuration WHERE id=$view_id" fi