Project

General

Profile

1 10224 aaronmk
#!/bin/bash -e
2 4426 aaronmk
# 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 10224 aaronmk
. "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"/../lib/sh/util.sh
8
9 4426 aaronmk
sed \
10 7541 aaronmk
-e 's/ UNIQUE KEY ("[^"]*")/ \/*CONSTRAINT \1 *\/UNIQUE/g' \
11 7540 aaronmk
-e 's/ UNIQUE KEY/ UNIQUE/g' \
12
-e 's/^  ((CONSTRAINT "[^"]*" )?((FULLTEXT|FOREIGN) )?KEY .*[^,])(,?)$/  \/*\1*\/CHECK (true)\5/' \
13 4426 aaronmk
-e "s/\/\*.* (SET NAMES )([0-9A-Za-z_]+) \*\/;/\1'\2';/" \
14 7534 aaronmk
-e 's/ (CHARACTER SET|COLLATE) [0-9A-Za-z_]+//g' \
15 6070 aaronmk
-e "s/ COMMENT '[^']*'//g" \
16 8943 aaronmk
${sql_ascii:+-e 's/ varbinary/ text/g' }\
17 8932 aaronmk
-e 's/ (binary|blob|varbinary)/ bytea/g' \
18 7796 aaronmk
-e 's/ (tinytext|mediumtext|longtext)/ text/g' \
19 6165 aaronmk
-e 's/ (int|mediumint|smallint|tinyint)([ (])/ integer\2/g' \
20 4426 aaronmk
-e 's/ double/ double precision/g' \
21 8942 aaronmk
-e 's/ (integer|bigint|bytea|double precision|float|text)(\([^)]*\))?( unsigned)?/ \1/g' \
22 7695 aaronmk
-e 's/ (char)\(0\)/ \1/g' \
23 10237 aaronmk
-e 's! (date|datetime|time|timestamp)! text/*\1*/!g' \
24 8259 aaronmk
-e 's/ ("[^"]*") enum(\([^()]*\))/ \1 text CHECK (\1 in \2)/g' \
25 8938 aaronmk
-e "s/ set\('([^()]*)'\)/ \1/g" \
26 7542 aaronmk
"$@"\
27 10225 aaronmk
|"$top_dir/my2pg.data"