-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathload_release.sh
executable file
·187 lines (168 loc) · 6.22 KB
/
load_release.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/bin/bash
set -e;
releasePath=$1
dbName=$2
loadType=$3
if [ -z ${loadType} ]
then
echo "Usage <release location> <db schema name> <DELTA|SNAP|FULL|ALL>"
exit -1
fi
moduleStr=INT
echo "Enter module string used in filenames [$moduleStr]:"
read newModuleStr
if [ -n "$newModuleStr" ]
then
moduleStr=$newModuleStr
fi
langCode=en
echo "Enter the language code string used in filenames [$langCode]:"
read newLangCode
if [ -n "$newLangCode" ]
then
langCode=$newLangCode
fi
dbUsername=root
echo "Enter database username [$dbUsername]:"
read newDbUsername
if [ -n "$newDbUsername" ]
then
dbUsername=$newDbUsername
fi
dbUserPassword=""
echo "Enter database password (or return for none):"
read newDbPassword
if [ -n "$newDbPassword" ]
then
dbUserPassword="-p${newDbPassword}"
fi
includeTransitiveClosure=false
echo "Calculate and store inferred transitive closure? [Y/N]:"
read tcResponse
if [[ "${tcResponse}" == "Y" || "${tcResponse}" == "y" ]]
then
echo "Including transitive closure table - transclos"
includeTransitiveClosure=true
fi
#Unzip the files here, junking the structure
localExtract="tmp_extracted"
generatedLoadScript="tmp_loader.sql"
generatedEnvScript="tmp_environment-mysql.sql"
#What types of files are we loading - delta, snapshot, full or all?
case "${loadType}" in
'DELTA') fileTypes=(Delta)
unzip -j ${releasePath} "*Delta*" -d ${localExtract}
;;
'SNAP') fileTypes=(Snapshot)
unzip -j ${releasePath} "*Snapshot*" -d ${localExtract}
;;
'FULL') fileTypes=(Full)
unzip -j ${releasePath} "*Full*" -d ${localExtract}
;;
'ALL') fileTypes=(Delta Snapshot Full)
unzip -j ${releasePath} -d ${localExtract}
;;
*) echo "File load type ${loadType} not recognised"
exit -1;
;;
esac
#Determine the release date from the filenames
releaseDate=`ls -1 ${localExtract}/*.txt | head -1 | egrep -o '[0-9]{8}'`
#Generate the environment script by running through the template as
#many times as required
now=`date +"%Y%m%d_%H%M%S"`
echo -e "\nGenerating Environment script for ${loadType} type(s)"
echo "/* Script Generated Automatically by load_release.sh ${now} */" > ${generatedEnvScript}
for fileType in ${fileTypes[@]}; do
fileTypeLetter=`echo "${fileType}" | head -c 1 | tr '[:upper:]' '[:lower:]'`
tail -n +2 environment-mysql-template.sql | while read thisLine
do
echo "${thisLine/TYPE/${fileTypeLetter}}" >> ${generatedEnvScript}
done
done
function addLoadScript() {
for fileType in ${fileTypes[@]}; do
fileName=${1/TYPE/${fileType}}
fileName=${fileName/DATE/${releaseDate}}
fileName=${fileName/MOD/${moduleStr}}
fileName=${fileName/LANG/${langCode}}
parentPath="${localExtract}/"
tableName=${2}_`echo $fileType | head -c 1 | tr '[:upper:]' '[:lower:]'`
snapshotOnly=false
#Check file exists - try beta version, or filepath directly if not
if [ ! -f ${parentPath}${fileName} ]; then
origFilename=${fileName}
fileName="x${fileName}"
if [ ! -f ${parentPath}${fileName} ]; then
parentPath=""
fileName=${origFilename}
tableName=${2} #Files loaded outside of extract directory use own names for table
snapshotOnly=true
if [ ! -f ${parentPath}${fileName} ]; then
echo "Unable to find ${origFilename} or beta version, skipping..."
return
fi
fi
fi
if [[ $snapshotOnly = false || ($snapshotOnly = true && "$fileType" = "Snapshot") ]]
then
echo "alter table ${tableName} disable keys;" >> ${generatedLoadScript}
echo "load data local" >> ${generatedLoadScript}
echo -e "\tinfile '"${parentPath}${fileName}"'" >> ${generatedLoadScript}
echo -e "\tinto table ${tableName}" >> ${generatedLoadScript}
echo -e "\tcolumns terminated by '\\\t'" >> ${generatedLoadScript}
echo -e "\tlines terminated by '\\\r\\\n'" >> ${generatedLoadScript}
echo -e "\tignore 1 lines;" >> ${generatedLoadScript}
echo -e "" >> ${generatedLoadScript}
echo "alter table ${tableName} enable keys;" >> ${generatedLoadScript}
echo -e "select 'Loaded ${fileName} into ${tableName}' as ' ';" >> ${generatedLoadScript}
echo -e "" >> ${generatedLoadScript}
fi
done
}
echo -e "\nGenerating loading script for $releaseDate"
echo "/* Generated Loader Script */" > ${generatedLoadScript}
addLoadScript sct2_Concept_TYPE_MOD_DATE.txt concept
addLoadScript sct2_Description_TYPE-LANG_MOD_DATE.txt description
addLoadScript sct2_StatedRelationship_TYPE_MOD_DATE.txt stated_relationship
addLoadScript sct2_Relationship_TYPE_MOD_DATE.txt relationship
addLoadScript sct2_sRefset_OWLExpressionTYPE_MOD_DATE.txt owlexpression
addLoadScript sct2_TextDefinition_TYPE-LANG_MOD_DATE.txt textdefinition
addLoadScript der2_cRefset_AttributeValueTYPE_MOD_DATE.txt attributevaluerefset
addLoadScript der2_cRefset_LanguageTYPE-LANG_MOD_DATE.txt langrefset
addLoadScript der2_cRefset_AssociationTYPE_MOD_DATE.txt associationrefset
addLoadScript der2_iissscRefset_ComplexMapTYPE_MOD_DATE.txt complexmaprefset
addLoadScript der2_iisssccRefset_ExtendedMapTYPE_MOD_DATE.txt extendedmaprefset
mysql -u ${dbUsername} ${dbUserPassword} --local-infile << EOF
select 'Ensuring schema ${dbName} exists' as ' ';
create database IF NOT EXISTS ${dbName};
use ${dbName};
select '(re)Creating Schema using ${generatedEnvScript}' as ' ';
source ${generatedEnvScript};
EOF
if [ "${includeTransitiveClosure}" = true ]
then
echo "Generating Transitive Closure file..."
tempFile=$(mktemp)
infRelFile=${localExtract}/sct2_Relationship_Snapshot_${moduleStr}_${releaseDate}.txt
if [ ! -f ${infRelFile} ]; then
infRelFile=${localExtract}/xsct2_Relationship_Snapshot_${moduleStr}_${releaseDate}.txt
fi
perl ./transitiveClosureRf2Snap_dbCompatible.pl ${infRelFile} ${tempFile}
mysql -u ${dbUsername} ${dbUserPassword} ${dbName} << EOF
DROP TABLE IF EXISTS transclos;
CREATE TABLE transclos (
sourceid varchar(18) DEFAULT NULL,
destinationid varchar(18) DEFAULT NULL,
KEY idx_tc_source (sourceid),
KEY idx_tc_destination (destinationid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
EOF
addLoadScript ${tempFile} transclos
fi
mysql -u ${dbUsername} ${dbUserPassword} ${dbName} --local-infile << EOF
select 'Loading RF2 Data using ${generatedLoadScript}' as ' ';
source ${generatedLoadScript};
EOF
rm -rf $localExtract
#We'll leave the generated environment & load scripts for inspection