forked from superloop-ltd/gnaf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
import-mysql
executable file
·47 lines (37 loc) · 1.07 KB
/
import-mysql
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
#!/usr/bin/env bash
function extract_fields {
sed -n "/^CREATE TABLE.*\`$1\`/,/ENGINE/p" structure.sql \
| grep -v ENGINE \
| grep -v CREATE\ TABLE
}
function sql_fields {
echo -n "("
extract_fields $table \
| sed '/DEFAULT NULL/ s/`\(.*\)`/`@\1`/' \
| cut -d \` -f2 \
| paste -sd , - \
| perl -pe 'chomp'
echo ")"
}
function sql_set_null {
extract_fields $table \
| grep DEFAULT\ NULL \
| cut -d \` -f2 \
| sed 's/.*/\0 = NULLIF(@\0, "")/g ; $!s/$/,/'
}
cat structure.sql
find . -name *.psv | while read -r file; do
table=$(basename "$file" | sed 's/_psv\|.psv//g' \
| sed 's/Authority_Code_\|ACT_\|NSW_\|SA_\|NT_\|OT_\|NSW_\|VIC_\|QLD_\|TAS_\|WA_//g')
echo "LOAD DATA LOCAL INFILE '$file'"
echo "INTO TABLE $table FIELDS TERMINATED BY '|' IGNORE 1 LINES"
sql_fields
set_null=$(sql_set_null)
if [ ! -z "$set_null" ]
then
echo -n "SET $set_null"
fi
echo ";"
echo
done
cat fixes.sql indexes.sql address_search.sql australian_postcodes.sql