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..f7ad02db89 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=$(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 else curl -H 'Content-Type: application/json' -vX POST $PROTO://$USERNAME@$HOST:$PORT/$DATABASE -d @$f