Skip to content

Instantly share code, notes, and snippets.

@N3X15
Created July 18, 2015 03:26
Show Gist options
  • Save N3X15/0df8aa159f1bebac9afd to your computer and use it in GitHub Desktop.
Save N3X15/0df8aa159f1bebac9afd to your computer and use it in GitHub Desktop.
pg2mysql Output SQL Fixer
# pg2mysql SQL Fixer
# (c)2015 Rob "N3X15" Nelson <nexisentertainment@gmail.com>
#
# Available under the MIT Open-Source License
#
import os
import sys
import re
REG_FUCKED_TYPE = re.compile('` `([a-z]+)`')
REG_FUCKED_CONSTRAINT = re.compile(r'ADD CONSTRAINT "([a-z_]+)" PRIMARY KEY \("([a-z_]+)"\)')
def fixFile(infn, outfn):
with open(outfn, 'w') as outf:
with open(infn, 'r') as inf:
prefixedWith = []
for line in inf:
line = REG_FUCKED_TYPE.sub(r'` \1', line)
line = REG_FUCKED_CONSTRAINT.sub(r'ADD CONSTRAINT `\1` PRIMARY KEY (`\2`)', line)
lines = line.strip()
if lines.startswith('DROP TABLE IF EXISTS'):
prefixedWith.append('DROP TABLE IF EXISTS')
if lines.startswith('CREATE TABLE'):
if 'DROP TABLE IF EXISTS' not in prefixedWith:
tablename = lines.split(' ')[2].strip('`')
outf.write('DROP TABLE IF EXISTS `{0}`;\n'.format(tablename))
print(" + DROP TABLE IF EXISTS")
prefixedWith = []
outf.write(line.rstrip() + '\n')
print('Wrote {0}.'.format(outfn))
if __name__ == '__main__':
fixFile(sys.argv[1], sys.argv[2])
#!/bin/bash
# Script for switching GitLab to MySQL/MariaDB from PostgreSQL.
mkdir -p ~/pg2mysql/work
cd /home/git
sudo -u postgres pg_dump gitlabhq_production --inserts --disable-dollar-quoting --quote-all-identifiers --format p -c > /tmp/gitlab.sql
rm -fv ~/pg2mysql/work/gitlab.psql
mv /tmp/gitlab.sql ~/pg2mysql/work/gitlab.psql
cd ~
cd pg2mysql
php pg2mysql_cli.php work/gitlab.psql work/gitlab.mysql
python fixSQL.py work/gitlab.mysql ~/gitlab.sql
cd ~
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment