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
if false; then #### whenever this script is changed:
8
(slash=/; for my in inputs/{.,}*/_MySQL/*.schema.sql; do
9
	pg="${my/_MySQL$slash/}"
10
	make "$pg"-remake
11
done)
12
fi ####
13

    
14
. "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"/../lib/sh/util.sh
15

    
16
sed \
17
-e 's/`/"/g' \
18
-e 's! UNIQUE KEY ("[^"]*")! /*CONSTRAINT \1 */UNIQUE!g' \
19
-e 's/ UNIQUE KEY/ UNIQUE/g' \
20
-e 's!^  ((CONSTRAINT "[^"]*" )?((FULLTEXT|FOREIGN) )?KEY .*[^,])(,?)$!  /*\1*/CHECK (true)\5!' \
21
-e "s!/\*.* (SET NAMES )([0-9A-Za-z_]+) \*/;!\1'\2';!" \
22
-e 's/ (CHARACTER SET|COLLATE) [0-9A-Za-z_]+//g' \
23
-e "s! (COMMENT '.*')! /*\1*/!g
24
# COMMENTs never span multiple lines because newlines are encoded as \n" \
25
-e 's!^\) (.*);$!) /*\1*/;!g
26
# comment out table options' \
27
${sql_ascii:+-e 's/ varbinary/ text/g' }\
28
-e 's/ (binary|blob|varbinary)/ bytea/g' \
29
-e 's/ (tinytext|mediumtext|longtext)/ text/g' \
30
-e 's/ (int|mediumint|smallint|tinyint)([ (])/ integer\2/g' \
31
-e 's/ double/ double precision/g' \
32
-e 's/ (integer|bigint|bytea|double precision|float|text)(\([^)]*\))?( unsigned)?/ \1/g' \
33
-e 's/ (char)\(0\)/ \1/g' \
34
-e 's! (date|datetime|time|timestamp)! text/*\1*/!g' \
35
-e 's/ ("[^"]*") enum(\([^()]*\))/ \1 text CHECK (\1 in \2)/g' \
36
-e "s/ set\('([^()]*)'\)/ \1/g" \
37
"$@"\
38
|"$top_dir/my2pg.data"
(48-48/87)