-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsvToSQL.bash
More file actions
406 lines (348 loc) · 12.9 KB
/
csvToSQL.bash
File metadata and controls
406 lines (348 loc) · 12.9 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#!/bin/bash
# ---------------------------------------------------------------------------- #
# Initial configuration #
# ---------------------------------------------------------------------------- #
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
csvsqlBin=$(which csvsql) # csvsql binary
pythonBin=$(which python3) # python binary
chunk_size=100000 # how many rows to import per iteration
pipBin="$pythonBin -m pip" # python pip binary
connectionConfigFile="$SCRIPT_DIR/sqlcon.bash" # sql configuration file
createConfigFile=0 # create config file?
connector="mysql+mysqlconnector" # type of sql connector
verbose="--verbose" # the flag or empty if non-verbose
additionalFlags="--no-create $verbose --insert --blanks" # csvsql command line parameters
csv_separator=','
c_red="\033[0;31m"
c_yellow="\033[1;33m"
c_green="\033[0;32m"
c_cyan="\033[0;36m"
c_end="\033[0m"
# ---------------------------------------------------------------------------- #
# Functions #
# ---------------------------------------------------------------------------- #
hr() {
echo "----------------------------"
}
inf() {
printf "ℹ️ ${c_cyan}%s${c_end}\n" "$1"
}
warn() {
printf "❗ ${c_yellow}%s${c_end}\n" "$1"
}
err() {
printf "❌ ${c_red}%s${c_end}\n" "$1"
exit 1
}
succ() {
printf "✅ ${c_green}%s${c_end}\n" "$1"
}
dot() {
printf "🔵 ${c_cyan}%s${c_end}\n" "$1"
}
checkAptPackage() {
# checkPackage=$(apt-get list "$1")
checkPackage=$(dpkg -l | grep "$1")
if [[ ! $checkPackage ]]; then
inf "Package $1 doesn't seem to be installed. Attempting to install..."
command=$(apt-get install "$1" -y)
if [ ! "$command" ]; then
err "Installing $1 failed. Please run 'apt install $1' manually."
else
succ "Package $1 installed!"
fi
else
succ "Package $1 already installed!"
fi
}
checkPythonPackage() {
checkPackage=$($pipBin list | grep -F "$1")
if [[ ! $checkPackage ]]; then
inf "Python dependency $1 doesn't seem to be installed. Attempting to install..."
command=$(pip install --upgrade "$1")
if [ ! "$command" ]; then
err "Installing $1 failed. Please run 'pip install --upgrade $1' manually."
else
succ "Dependency $1 successfully installed!"
fi
else
succ "Python dependency $1 already installed!"
fi
}
showHelp() {
hr
inf "Help"
echo "You can run this bash script with the following parameters:"
echo "./csvtosql [FLAGS] [CSVFILE]"
echo
echo "Flags:"
echo "[-h] Help"
echo "[-f] Fast (skip pre-checks)"
hr
exit 0
}
str_split() {
if [ ! "$1" ] || [ ! "$2" ]; then
warn "str_split was used without 2 parameters."
exit 1
fi
input="$1"
delimiter="$2"
arr=(${input//delimiter/ })
echo ${arr[@]}
}
prompt() {
read -p "$1 [Y/n] " -n 1 -r
killonno="$2"
echo
if [[ $REPLY =~ ^[Yy]$ || $REPLY == "" ]]; then
reply=true
else
reply=false
if [ "$killonno" == true ]; then
err "Exiting..."
fi
fi
}
hr
# ---------------------------------------------------------------------------- #
# Custom flags #
# ---------------------------------------------------------------------------- #
if [ $# -eq 0 ]; then
inf "You ran this script without any parameters. You will be prompted for input.
To avoid this next time, run this script with the -[h]elp parameter."
else
while getopts hf flag
do
case "${flag}" in
h)
showHelp
;;
f)
fast=true # skip pre-checks
;;
esac
done
fi
if [ "$fast" != true ]; then
inf "Checking environment..."
# ---------------------------------------------------------------------------- #
# Check that current user is root #
# ---------------------------------------------------------------------------- #
if [ "$USER" != "root" ]; then
err "This script must be run as root."
else
succ "Script is running as root"
fi
# ---------------------------------------------------------------------------- #
# Check Python #
# ---------------------------------------------------------------------------- #
if [[ ! -f "$pythonBin" ]]; then
err "Unable to find Python binary"
else
succ "Python located ($($pythonBin -V))"
fi
# ---------------------------------------------------------------------------- #
# Check dependencies #
# ---------------------------------------------------------------------------- #
checkAptPackage "python3-dev"
checkAptPackage "python3-pip"
checkAptPackage "python3-setuptools"
checkAptPackage "build-essential"
checkPythonPackage "setuptools"
checkPythonPackage "csvkit"
# ---------------------------------------------------------------------------- #
# CSVSQL info #
# ---------------------------------------------------------------------------- #
hr
inf "CSVSQL Info:"
succ "Binary: $csvsqlBin"
succ "Options: $additionalFlags"
else
inf "Skipping environment check because [-f] flag was present."
fi
# ---------------------------------------------------------------------------- #
# Prompt for CSV if not supplied in parameter #
# ---------------------------------------------------------------------------- #
csvFile="${@: -1}"
if [[ $csvFile == *.csv ]]; then
inf "CSV file assumed to be parameter $csvFile"
else
inf "No CSV file seems to have been supplied to the script."
read -r -p "Type full path to CSV file you want to import: " csvFile
fi
# ---------------------------------------------------------------------------- #
# File does not exist #
# ---------------------------------------------------------------------------- #
if [[ ! -f "$csvFile" ]]; then
err "File $csvFile does not exist."
fi
# ---------------------------------------------------------------------------- #
# CSV File information #
# ---------------------------------------------------------------------------- #
hr
inf "CSV file information:"
filesize=$(wc -c "$csvFile" | awk '{print $1}')
filesize=$((filesize / 1000 / 1000))
tableName=$(basename "$csvFile" .csv)
succ "File: $csvFile"
succ "Chunk size: $chunk_size"
if [[ "$filesize" -gt 1000 ]]; then
warn "Filesize: $filesize MB (this could take some time)"
else
succ "Filesize: $filesize MB"
fi
lines=$(sed -n '$=' "$csvFile")
if [[ "$lines" -gt 1000000 ]]; then
warn "Lines: $lines (this could take some time)"
else
succ "Lines: $lines"
fi
# ---------------------------------------------------------------------------- #
# Database connection info #
# ---------------------------------------------------------------------------- #
hr
inf "Database connection info:"
if [[ -f "$connectionConfigFile" ]]; then
succ "Configuration file $connectionConfigFile found!"
source "$connectionConfigFile"
if [ -z "$host" ] || [ -z "$database" || -z "$username" || -z "$password" ]; then
err "Connection config file ($connectionConfigFile) was sourced but it doesn't contain the neccessary connection info.
Please delete this file and try again."
fi
else
inf "Configuration file not found. Prompting for database connection info"
echo "Database connection config file not found (expected $connectionConfigFile)"
echo "Please enter database connection info below"
read -r -i "localhost" -p "Host [default localhost]: " host
read -r -p "Database: " db
read -r -i "root" -p "Username [default root]: " username
read -r -i "" -s -p "Password [default blank]: " password
if ["$createConfigFile" !== 0]; then
touch "$connectionConfigFile"
echo "host='$host'" > "$connectionConfigFile"
echo "db='$database'" > "$connectionConfigFile"
echo "username='$username'" > "$connectionConfigFile"
echo "password='$password'" > "$connectionConfigFile"
echo
fi
fi
connectionString="$connector://$username:$password@$host/$db"
checkConnection="MYSQL_PWD='$password' mysql -u '$username' -e '\q'"
checkConnection=$(eval "$checkConnection"; echo $?)
if [[ "$checkConnection" != 0 ]]; then
err "Unable to connect to MySQL on $host as user $username."
exit 1
else
succ "Connection established!"
fi
# TODO: You also need to check for database presence
exists_query=$(printf 'SHOW TABLES LIKE "%s"' "$tableName")
exists_check=$(MYSQL_PWD="$password" mysql -u "$username" -e "$exists_query" "$db")
# empty_query=$(printf 'SELECT COUNT(*) as records FROM %s' "$tableName")
# empty_check=$(MYSQL_PWD="$password" mysql -u "$username" -e "$empty_query" "$db")
succ "Connector: $connector"
succ "Host: $host"
succ "Database: $db"
if [[ "$exists_check" ]]; then
warn "Table: $tableName (Already exists!)"
else
succ "Table: $tableName"
fi
succ "Username: $username"
succ "Password: [HIDDEN]"
prompt "Do you want to empty the table before importing?" false
if [ "$reply" == true ]; then
warn "Table $tableName will be deleted and recreated! (all data will be lost)"
prompt "Please confirm your choice " true
else
inf "Table will be appended to"
fi
# ---------------------------------------------------------------------------- #
# Get headers and create table if doesn't exist #
# ---------------------------------------------------------------------------- #
# inf "Fetching headers..."
# headers=$(head -n 1 "$csvFile")
# commandToRun=$(echo "$headers" | tee /dev/tty | $csvsqlBin --db "$connectionString" --tables "$tableName" --insert --verbose)
# # commandToRun="echo '$headers' | $csvsqlBin --db $connectionString --tables $tableName --insert"
# inf "Creating table $tableName with headers:"
# echo "$headers"
# if [ ! "$commandToRun" ]; then
# err "Failed to create headers: $commandToRun"
# else
# succ "Headers created!"
# fi
# exit 0
# cat foo.csv | awk 'FNR==1 { printf "$headers\n" } { print $0 }'
# headers
headers=$(head -n 1 "$csvFile")
headersNoQuotes=$(echo $headers | tr -d '"')
echo "$headersNoQuotes"
prompt "Do you want to create table '$tableName' with the above columns?" true
hr
inf "Creating table from CSV..."
dataDef=""
while IFS="$csv_separator" read -ra ADDR; do
for i in "${ADDR[@]}"; do
if [ "$i" == "id" ]; then
dataDef+="$i int,\n"
else
dataDef+="$i varchar(255),\n"
fi
done
done <<< "$headersNoQuotes"
# TODO: Please fix this...
search='[,\n\s]'
dataDef=${dataDef%$search}
search='[\n\s]'
dataDef=${dataDef%$search}
search='[,\\]'
dataDef=${dataDef%$search}
search='[,\n)]'
dataDef=${dataDef%$search}
if [ "$reply" == true ]; then
# empty table
createTable=$(
echo -e "DROP TABLE $tableName;";
echo -e "
CREATE TABLE IF NOT EXISTS $tableName (
${dataDef}
);")
else
createTable=$(
echo -e "
CREATE TABLE IF NOT EXISTS $tableName (
${dataDef}
)")
fi
inf "Running queries: $createTable"
createTable=$(MYSQL_PWD="$password" mysql -u "$username" -e "$createTable" "$db")
inf "Fetching preview with sample values..."
# data
sampleCommand="sed -n 5,15p $csvFile"
sampleValues=$($sampleCommand)
sampleValuesNoQuotes=$($sampleCommand | tr -d '"')
# type check
sampleTypes=$(echo "$sampleValuesNoQuotes" | awk -F"$csv_separator" \
'
{
printf("[%s],", typeof($1))
}
')
(echo "$sampleTypes"; echo "$sampleValuesNoQuotes") | column -t --separator "$csv_separator" --output-separator " " --table-columns "$headersNoQuotes"
hr
inf "Importing '$csvFile' to $db -> $tableName"
startTime=$(date +%s.%N | awk '{printf "%.2f",$0}')
csvsqlFullCommand="$csvsqlBin --db $connectionString --tables $tableName $additionalFlags"
for ((from=2; from <= lines; from=from+chunk_size))
do
to=$((from+chunk_size))
(echo "$headers"; sed -n "${from},${to}p" "$csvFile") | $csvsqlFullCommand
endTime=$(date +%s.%N | awk '{printf "%.2f",$0}')
totalTime=$(echo "${endTime} - ${startTime}" | bc -l)
percentage=$(( (from*100) / lines ))
succ "[${totalTime}s] [$percentage%] Imported lines $to / $lines!"
done
succ "[${totalTime}s] [100%] Successfully imported $csvFile to $tableName!"
inf "Remember to modify the columns (to ensure unique values and indexing etc...)"
exit 0