Skip to content

Instantly share code, notes, and snippets.

@sleepsonthefloor
Created February 13, 2012 22:20
Show Gist options
  • Save sleepsonthefloor/1820955 to your computer and use it in GitHub Desktop.
Save sleepsonthefloor/1820955 to your computer and use it in GitHub Desktop.
keystone migrate snippets
from sqlalchemy import create_engine
db = create_engine('mysql://root:secrete@192.168.2.10/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