Created
June 16, 2014 07:10
-
-
Save xupeng/7744d27432e0c8d0f499 to your computer and use it in GitHub Desktop.
Migrate GitLab from PostgreSQL to MySQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
''' Migrate GitLab DB from PostgreSQL to MySQL | |
I do not know why there is no `rake db:data:dump` for my GitLab installation, | |
so I just use `rake db:setup` to create tables in MySQL, and then use this | |
query-and-dirty script to migrate the data. | |
''' | |
import psycopg2 | |
import MySQLdb | |
def main(): | |
pg_conn = psycopg2.connect("dbname=gitlabhq_production user=git") | |
pg_cursor = pg_conn.cursor() | |
mysql_conn = MySQLdb.connect(host='127.0.0.1', user='git', passwd='xxxxxxxxxx', | |
db='gitlabhq_production') | |
mysql_cursor = mysql_conn.cursor() | |
mysql_cursor.execute('show tables') | |
for table, in mysql_cursor.fetchall(): | |
pg_cursor.execute('select * from %s' % table) | |
for row in pg_cursor.fetchall(): | |
stubs = ', '.join(['%s'] * len(row)) | |
sql = 'insert into `%s` values (%s)' % (table, stubs) | |
mysql_cursor.execute(sql, row) | |
mysql_cursor.connection.commit() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment