This is a minimalist Python migration management script based on Fabric and sqlwitch.
It assumes a migrations/ directory under the project's root path. The fabfile.py attached in this Gist should be added to this directory.
Under migrations/ also, you're expected to add your migration files, which can be .py, .sql or both. Raw SQL files are used for raw create statements. Python scripts are used to move data around (if needed).
The script expects yyyy-mm-dd-hh-mm-migration-name.(py|sql) as the format used in your migration filenames.
The fabfile.py contains two extremely simple tasks: list and migrate.
fab list displays all files that conform the above migration filename format.
fab migrate:$migration runs the migration specified with a filename (extension included). If it's a .sql file, it tries to run it with the mysql binary. If it's a .py file, it loads it as a module and executes its migrate() function.
The script assumes you have a support.py file on your project's root path that is responsible for loading all dependencies, including sqlwitch, and a function called get_db_handler() which returns a sqlwitch object. This is how this function looks in one of my projects (might have to be different in yours):
def get_db_handler(db_auth): conn = MySQLdb.connect(**db_auth) cursor = conn.cursor(MySQLdb.cursors.DictCursor) conn.set_character_set('utf8') cursor.execute('set names utf8;') cursor.execute('set character_set_connection = utf8;') return sqlwitch(cursor, conn)
The script also assumes you have a dictionary called config defined in support.py, containing another dictionary called db with the properties host, user, passwd and db defined (for use in MySQLdb.connect()).