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 10442 aaronmk
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 10224 aaronmk
. "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"/../lib/sh/util.sh
15
16 4426 aaronmk
sed \
17 10439 aaronmk
-e 's/`/"/g' \
18 10448 aaronmk
-e 's! UNIQUE KEY ("[^"]*")! /*CONSTRAINT \1 */UNIQUE!g' \
19 7540 aaronmk
-e 's/ UNIQUE KEY/ UNIQUE/g' \
20 10448 aaronmk
-e 's!^  ((CONSTRAINT "[^"]*" )?((FULLTEXT|FOREIGN) )?KEY .*[^,])(,?)$!  /*\1*/CHECK (true)\5!' \
21
-e "s!/\*.* (SET NAMES )([0-9A-Za-z_]+) \*/;!\1'\2';!" \
22 7534 aaronmk
-e 's/ (CHARACTER SET|COLLATE) [0-9A-Za-z_]+//g' \
23 10444 aaronmk
-e "s! (COMMENT '.*')! /*\1*/!g
24 10441 aaronmk
# COMMENTs never span multiple lines because newlines are encoded as \n" \
25 10447 aaronmk
-e 's!^\) (.*);$!) /*\1*/;!g
26
# comment out table options' \
27 8943 aaronmk
${sql_ascii:+-e 's/ varbinary/ text/g' }\
28 8932 aaronmk
-e 's/ (binary|blob|varbinary)/ bytea/g' \
29 7796 aaronmk
-e 's/ (tinytext|mediumtext|longtext)/ text/g' \
30 6165 aaronmk
-e 's/ (int|mediumint|smallint|tinyint)([ (])/ integer\2/g' \
31 4426 aaronmk
-e 's/ double/ double precision/g' \
32 8942 aaronmk
-e 's/ (integer|bigint|bytea|double precision|float|text)(\([^)]*\))?( unsigned)?/ \1/g' \
33 7695 aaronmk
-e 's/ (char)\(0\)/ \1/g' \
34 10237 aaronmk
-e 's! (date|datetime|time|timestamp)! text/*\1*/!g' \
35 8259 aaronmk
-e 's/ ("[^"]*") enum(\([^()]*\))/ \1 text CHECK (\1 in \2)/g' \
36 8938 aaronmk
-e "s/ set\('([^()]*)'\)/ \1/g" \
37 7542 aaronmk
"$@"\
38 10225 aaronmk
|"$top_dir/my2pg.data"