-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathimport_table.sh
33 lines (23 loc) · 1.25 KB
/
import_table.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env bash
echo "++++++++++++++++++ HELLO, DB = $POSTGRES_DB"
TBL_TO_LOAD=$1
echo "++++++++++++++++++ LOADING TABLE = $TBL_TO_LOAD"
first_loop_flag=true
for FULLPATH in `find $PATH_TO_DBF_FILES/$TBL_TO_LOAD* -type f`
do
FILE="${FULLPATH##*/}"
TABLE="${FILE%.*}"
pgdbf $FULLPATH | iconv -f cp866 -t utf-8 | psql postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@$POSTGRES_HOST:$POSTGRES_PORT/$POSTGRES_DB
echo "++++++++++++++++++ TEMP TABLE $TABLE LOADED"
if [[ "$first_loop_flag" = true ]]; then
psql postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@$POSTGRES_HOST:$POSTGRES_PORT/$POSTGRES_DB -c "CREATE TABLE IF NOT EXISTS $TBL_TO_LOAD AS SELECT * FROM $TABLE WHERE 0 = 1;"
echo "++++++++++++++++++ TARGET TABLE $TBL_TO_LOAD CREATED"
first_loop_flag=false
fi
echo "++++++++++++++++++ INSERT $TABLE DATA INTO $TBL_TO_LOAD"
if [[ "$TABLE" != "$TBL_TO_LOAD" ]]; then
echo "++++++++++++++++++ DROP TABLE $TABLE"
psql postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@$POSTGRES_HOST:$POSTGRES_PORT/$POSTGRES_DB -c "INSERT INTO $TBL_TO_LOAD SELECT * FROM $TABLE;"
psql postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@$POSTGRES_HOST:$POSTGRES_PORT/$POSTGRES_DB -c "DROP TABLE $TABLE;"
fi
done