Project

General

Profile

1
#!/bin/bash -e
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
. "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"/../lib/sh/util.sh
8

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