Project

General

Profile

1
#!/bin/sh
2
# Translates a MySQL DB export to PostgreSQL
3
# The export must be created with:
4
# `--compatible=postgresql --add-locks=false --set-charset`
5
# Usage: self {input...|<input} >translated
6

    
7
selfDir="$(dirname -- "$0")"
8

    
9
sedEreFlag="$(test "$(uname)" = Darwin && echo E || echo r)"
10

    
11
sed () { "$(which sed)" -"$sedEreFlag" "$@";}
12

    
13
sed \
14
-e 's/ UNIQUE KEY ("[^"]*")/ \/*CONSTRAINT \1 *\/UNIQUE/g' \
15
-e 's/ UNIQUE KEY/ UNIQUE/g' \
16
-e 's/^  ((CONSTRAINT "[^"]*" )?((FULLTEXT|FOREIGN) )?KEY .*[^,])(,?)$/  \/*\1*\/CHECK (true)\5/' \
17
-e "s/\/\*.* (SET NAMES )([0-9A-Za-z_]+) \*\/;/\1'\2';/" \
18
-e 's/ (CHARACTER SET|COLLATE) [0-9A-Za-z_]+//g' \
19
-e "s/ COMMENT '[^']*'//g" \
20
${sql_ascii:+-e 's/ varbinary/ text/g' }\
21
-e 's/ (binary|blob|varbinary)/ bytea/g' \
22
-e 's/ (tinytext|mediumtext|longtext)/ text/g' \
23
-e 's/ (int|mediumint|smallint|tinyint)([ (])/ integer\2/g' \
24
-e 's/ double/ double precision/g' \
25
-e 's/ (integer|bigint|bytea|double precision|float|text)(\([^)]*\))?( unsigned)?/ \1/g' \
26
-e 's/ (char)\(0\)/ \1/g' \
27
-e 's/ datetime/ timestamp/g' \
28
-e 's/ ("[^"]*") enum(\([^()]*\))/ \1 text CHECK (\1 in \2)/g' \
29
-e "s/ set\('([^()]*)'\)/ \1/g" \
30
"$@"\
31
|"$selfDir/my2pg.data"
(43-43/81)