forked from openmaptiles/openmaptiles-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport-sql
executable file
·86 lines (75 loc) · 3.1 KB
/
import-sql
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/bin/bash
set -o errexit
set -o pipefail
set -o nounset
shopt -s nullglob
# For backward compatibility, allow both PG* and POSTGRES_* forms,
# with the non-standard POSTGRES_* form taking precedence.
# An error will be raised if neither form is given, except for the PGPORT
export PGHOST="${POSTGRES_HOST:-${PGHOST?}}"
export PGDATABASE="${POSTGRES_DB:-${PGDATABASE?}}"
export PGUSER="${POSTGRES_USER:-${PGUSER?}}"
export PGPASSWORD="${POSTGRES_PASSWORD:-${PGPASSWORD?}}"
export PGPORT="${POSTGRES_PORT:-${PGPORT:-5432}}"
function exec_psql_file() {
local file_name="$1"
# Allws additional parameters to be passed to psql
# For example, PSQL_OPTIONS='-a -A' would echo everything and disable output alignment
# Using eval allows complex cases with quotes, like PSQL_OPTIONS=" -a -c 'select ...' "
eval "local psql_opts=(${PSQL_OPTIONS:-})"
echo "Importing $file_name (md5 $(md5sum < "$file_name") $(wc -l < "$file_name") lines) into Postgres..."
# shellcheck disable=SC2154
psql \
-v ON_ERROR_STOP="1" \
-c '\timing on' \
-f "$file_name" \
"${psql_opts[@]}"
}
function import_all_sql_files() {
local dir="$1"
local sql_file
if [[ -d "$dir/parallel" ]]; then
# Asseme this dir may contain run_first.sql, parallel/*.sql, and run_last.sql
# use parallel execution
if [[ -f "$dir/run_first.sql" ]]; then
exec_psql_file "$dir/run_first.sql"
else
echo "File $dir/run_first.sql not found, skipping"
fi
# Run import_sql script in parallel, up to MAX_PARALLEL_PSQL processes at the same time
: "${MAX_PARALLEL_PSQL:=5}"
echo "Importing $(find "$dir/parallel" -name "*.sql" | wc -l) sql files from $dir/parallel/, up to $MAX_PARALLEL_PSQL files at the same time"
find "$dir/parallel" -name "*.sql" -print0 | xargs -0 -I{} -P "$MAX_PARALLEL_PSQL" "$0" "{}"
echo "Finished importing sql files matching '$dir/parallel/*.sql'"
if [[ -f "$dir/run_last.sql" ]]; then
exec_psql_file "$dir/run_last.sql"
else
echo "File $dir/run_last.sql not found, skipping"
fi
else
for sql_file in "$dir"/*.sql; do
exec_psql_file "$sql_file"
done
fi
}
# If there are no arguments, imports everything,
# otherwise the first argument is the name of a file to be imported.
if [[ $# -eq 0 ]]; then
if [[ "${OMT_UTIL_DIR:-}" == "" ]]; then
echo "ENV variable OMT_UTIL_DIR is not set. It should contain directory with .sql files, e.g. language.sql "
exit 1
fi
if [[ "${VT_UTIL_DIR:-}" == "" ]]; then
echo "ENV variable VT_UTIL_DIR is not set. It should contain directory with .sql files, e.g. postgis-vt-util.sql"
exit 1
fi
if [[ "${SQL_DIR:-}" == "" ]]; then
echo "ENV variable SQL_DIR is not set. It should contain directory with .sql files, e.g. tileset.sql, or run_first.sql, run_last.sql, and parallel/*.sql"
exit 1
fi
import_all_sql_files "$OMT_UTIL_DIR" # import language.sql
import_all_sql_files "$VT_UTIL_DIR" # import postgis-vt-util.sql
import_all_sql_files "$SQL_DIR" # import compiled tileset.sql
else
exec_psql_file "$1"
fi