summaryrefslogtreecommitdiff
path: root/lib/support/mysql-postgresql-converter/splice_drop_indexes
blob: dc13f8166fee94ac1b1093b6814afc91de5393d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/bin/sh
# This script reorders database dumps generated by db_converter.py for
# efficient consumption by Postgres.

fail() {
  echo "$@" 1>&2
  exit 1
}

db_gz=$1
drop_indexes_sql=$2

if [ -z "$db_gz" ] || [ -z "$drop_indexes_sql" ] ; then
  fail "Usage: $0 database.sql.gz drop_indexes.sql"
fi

# Capture all text up to the first occurence of 'SET CONSTRAINTS'
preamble=$(gzip -cd "$db_gz" | sed '/SET CONSTRAINTS/q')
if [ -z "$preamble" ] ; then
  fail "Could not read preamble"
fi

drop_indexes=$(cat "$drop_indexes_sql")
if [ -z "$drop_indexes" ] ; then
  fail "Could not read DROP INDEXES file"
fi

# Print preamble and drop indexes
cat <<EOF
${preamble}

${drop_indexes}
EOF

# Print the rest of database.sql.gz. I don't understand this awk script but it
# prints all lines after the first match of 'SET CONSTRAINTS'.
gzip -cd "$db_gz" | awk 'f; /SET CONSTRAINTS/ { f = 1 }'