Created
February 13, 2012 22:20
-
-
Save sleepsonthefloor/1820955 to your computer and use it in GitHub Desktop.
keystone migrate snippets
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
from sqlalchemy import create_engine | |
db = create_engine('mysql://root:[email protected]/keystone') | |
table_query = db.execute("show tables") | |
migration_data = {} | |
for table_name in table_query.fetchall(): | |
table_name = table_name[0] | |
query = db.execute("describe %s" % table_name) | |
column_names = [] | |
column_attributes = {} | |
for column in query.fetchall(): | |
column_name = column[0] | |
column_attributes[column_name] = column | |
column_name = table_name + "." + column_name | |
column_names.append(column_name) | |
table_dump = {} | |
table_dump['name'] = table_name | |
table_dump['column_attributes'] = column_attributes | |
query = db.execute("select %s from %s" % (",".join(column_names), table_name)) | |
table_data = [] | |
for row in query.fetchall(): | |
entry = {} | |
for (i, c) in enumerate(column_names): | |
entry[c.split('.')[1]] = row[i] | |
table_data.append(entry) | |
table_dump['data'] = table_data | |
migration_data[table_name] = table_dump | |
print migration_data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment