Skip to content

Instantly share code, notes, and snippets.

@paulfariello
Created January 21, 2019 13:30
Show Gist options
  • Save paulfariello/3f7d855c8adbce911512aac969a5caa7 to your computer and use it in GitHub Desktop.
Save paulfariello/3f7d855c8adbce911512aac969a5caa7 to your computer and use it in GitHub Desktop.
Convert mnesia database to postgresql new schema
#!/bin/sh
tables=$(grep "ADD COLUMN server_host" /usr/obj/ejabberd-18.06/sql/pg.new.sql | awk '{print $4}')
domains="localhost example.org"
for domain in $domains; do
echo -n $domain: >&2
dump=$(mktemp /tmp/$domain.sql.XXXXXX)
chown _ejabberd:_ejabberd $dump
echo $dump >&2
su - -s /bin/sh _ejabberd -c "ejabberdctl export2sql $domain $dump"
src=$dump
for table in $tables; do
echo -n " $table: " >&2
dst=$(mktemp /tmp/$domain.$table.sql.XXXXXX)
echo $dst >&2
gsed -e "s/INSERT\s\+INTO\s\+$table\s*(\(.*\))\s*VALUES\s*(/INSERT INTO $table(server_host, \1) VALUES ('$domain', /I" $src > $dst
src=$dst
done
cat $dst
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment