-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate-data-settings.sh
executable file
·144 lines (115 loc) · 3.96 KB
/
generate-data-settings.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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/bin/bash
# Generates the configuration settings starting from .patch.json file
variables_file=$1
mip_cde_variables_file=$2
: ${mip_cde_variables_file:=../mip-cde-meta-db-setup/variables.json}
target_table="besta_features"
dataset="besta_only"
migration_version="V1_3_1"
force_dataset_generation=true
view="mip_local_features"
if ! [ -f "$variables_file" ] ; then
echo "Generates the configuration settings starting from .patch.json file"
echo "Usage:"
echo " ./generate-data-settings.sh ../besta-meta-db-setup/besta.patch.json"
echo "Please run this script from the root of your data project."
exit 1
fi
echo
mkdir -p config/
cat << EOF > config/${target_table}_columns.properties
# suppress inspection "UnusedProperty" for whole file
# Name of the target table
__TABLE=${target_table^^}
# Columns of the table
__COLUMNS=subjectcode,$(cat $variables_file | jq --raw-output '[(.. | objects | select(has("methodology")) | .code)] | sort | join(",")' )
# Description of the type and constraints for each column in the table
subjectcode.type=char(20)
subjectcode.constraints=is_index
EOF
cat $variables_file | jq --raw-output '
def char_type(v):
if (v|has("length")) then
"char(" + (v.length|tostring) + ")"
else
"varchar(256)"
end
;
.. | objects | select(has("methodology")) | ( .code + ".type=" + (if has("sql_type") then
.sql_type
elif .type == "real" then
"numeric"
elif .type == "integer" then
"int"
elif .type == "binominal" then
char_type(.)
elif .type == "polynominal" then
char_type(.)
else
char_type(.)
end))' | sort >> config/${target_table}_columns.properties
echo "Generated config/${target_table}_columns.properties"
cat << EOF > config/${dataset}_dataset.properties
# suppress inspection "UnusedProperty" for whole file
# Name of the dataset
__DATASET=$dataset
# Name of the target table
__TABLE=${target_table^^}
# CSV file containing the data to inject in the table
__CSV_FILE=/data/${dataset}.csv
# SQL statement to remove all data from a previous execution
__DELETE_SQL=DELETE FROM ${target_table^^}
EOF
echo "Generated config/${dataset}_dataset.properties"
mkdir -p sql/
cat << EOF > sql/${migration_version}__${target_table}.sql
SET datestyle to 'European';
CREATE TABLE ${target_table^^}
(
"subjectcode" char(20),
EOF
cat $variables_file | jq --raw-output '
def char_type(v):
if (v|has("length")) then
"char(" + (v.length|tostring) + ")"
else
"varchar(256)"
end
;
.. | objects | select(has("methodology")) | ( " !" + .code + "! " + (if has("sql_type") then
.sql_type
elif .type == "real" then
"numeric"
elif .type == "integer" then
"int"
elif .type == "binominal" then
char_type(.)
elif .type == "polynominal" then
char_type(.)
else
char_type(.)
end) + ",")' | tr "!" '"' | sort >> sql/${migration_version}__${target_table}.sql
cat << EOF >> sql/${migration_version}__${target_table}.sql
CONSTRAINT pk_$target_table PRIMARY KEY (subjectcode)
)
WITH (
OIDS=FALSE
);
EOF
echo "Generated sql/${migration_version}__${target_table}.sql"
if [ ! -f sql/$dataset.csv ] || [ $force_dataset_generation ]; then
echo "subjectcode,$(cat $variables_file | jq --raw-output '[(.. | objects | select(has("methodology")) | .code)] | sort | join(",")' )" > sql/$dataset.csv
echo "Generated sql/$dataset.csv"
fi
cat << EOF > config/${view}_view.properties
# suppress inspection "UnusedProperty" for whole file
# Name of the view
__VIEW=${view^^}
# List of tables used in the view
__TABLES=mip_cde_features,$target_table
# Columns of the view
__COLUMNS=subjectcode,$(cat $mip_cde_variables_file | jq --raw-output '[(.. | objects | select(has("methodology")) | .code)] | sort | join(",")' ),$(cat $variables_file | jq --raw-output '[(.. | objects | select(has("methodology")) | .code)] | sort | join(",")' )
# Template of the SQL statements to execute to create the view
__SQL_TEMPLATE=join_view.mustache.sql
EOF
echo "Generated config/${view}_view.properties"