SQLAlchemy Migrate with Pylons
Instructions on using sqlalchemy migrate with Pylons, especially to convert an existing pylons project to use sqlalchemy migrate
This is based off several excellent sources including this guide and these threads.
One important point to note is that you are likely to end up with two versions of your model tables: one in yourapp/model and one in yourapp/migration/versions/*.py with the former representing your tables at HEAD and the second containing upgrade/downgrade scripts whose final result is HEAD. This duplication is a bit annoying and I discuss how it can be avoided below.
1. Install sqlalchemy migrate for your project e.g.
pip -E {your-virtualenv} install sqlalchemy-migrate
# or
easy_install sqlalchemy-migrate
NB: latest version of migrate are only compatible with sqlalchemy >= 0.5 (for previous version of sqlalchemy you need migrate <= 0.4.5)
2. Create the migrate repository (i.e. store for upgrade scripts …).
In your project directory
migrate create myapp/migration/ "MyApp"
Now create a temporary helper script:
migrate manage dbmanage.py --repository=myapp/migration/ --url={your-sqlalchemy-db-uri}
3. Set up db version control
python dbmanage.py version_control
Check the current version (should be 0)
python dbmanage.py version
4. Create a migration script for your existing db
python dbmanage.py script "Add existing tables"
This will create a script in myapp/migration/versions/001_add_existing_tables.py
Copy into that file the definition for all your existing tables (and other database stuff such as constraints) and then create those tables in the upgrade() function (and delete them in downgrade()).
That’s it! (in theory)
Additional Issues
1. Duplication of model/db code
You now have two places for model/db code:
- Your migration scripts
- Your actual model
This doesn’t have to be a problem but it is an obvious way for bugs to creep especially when some people start by creating their DB from the model code and others from the migration scripts.
Warning: this method will not work if do stuff in your table creation that is not persisted into the actual DB sql (e.g. column default values based on a function, custom db types …).
One way to avoid the duplication is to have all table creation and alteration confined to your migration scripts and then have your model tables set up directly from the DB using the autoload=True option. The one disadvantage of this is you can’t see the complete DB set up in one places as tables construction may be spread over several migrate scripts. One solution to this is provided by the experimental ‘create_model’ command which dumps the current DB model in the required sqlalchemy table code.
More discussion in this migrate-users thread
Bringing the Migration DB up to date
If you do set up your DB (from new) directly from your model code rather than the migration scripts then this requires that you set up the migration stuff and update the migrate version to the correct number. (I note there is an experimental update_db_to_model command which is supposed to do this for you). You can do this as follows (assuming your migrate repository is at YOURAPP:
from migrate.versioning.api import version_control, version
import YOURAPP.migration.versions
v = version(YOURAPP.migration.__path__[0])
# log.info( "Setting current version to '%s'" % v )
# url is your sqlalchemy db url
version_control(url, YOURAPP.migration.__path__[0], v)
Extras
- Should wrap upgrade/downgrade in transactions. I found one way to do this here but testing indicated this didn’t work for me (rollback was not working properly when there was an error)
-
Categories
- *nix
- Academic
- Activity Updates
- Books
- Cinema
- Code
- Command Line
- Copyright
- Culture and Society
- Data Digging
- Economics
- EUPD
- External
- Filesharing
- Governance
- Hacks
- Happiness
- Hardware
- History
- Innovation and Intellectual Property
- Intellectual Myths
- Javascript
- Knowledge Systems
- Miscellaneous
- Musings
- Notes
- Open Bibliographic Data
- Open Data
- Open Knowledge Foundation
- Openness
- Own Work
- Papers
- People
- Photos
- Platforms
- Poetry
- Policy
- PSI
- Python
- Quote
- RDF
- Shuttleworth Fellow
- Software
- Sysadmin
- Talks
- Transaction Costs
- Work In Progress
-
Articles
- February 2012
- January 2012
- December 2011
- November 2011
- October 2011
- September 2011
- August 2011
- July 2011
- May 2011
- April 2011
- March 2011
- February 2011
- January 2011
- December 2010
- November 2010
- October 2010
- September 2010
- July 2010
- June 2010
- May 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- October 2006
- September 2006
- August 2006
- July 2006
- June 2006
- May 2006
- April 2006
- March 2006
- February 2006
- January 2006
- December 2005
- November 2005
- October 2005
- September 2005
- August 2005
- July 2005
- June 2005
- April 2005
- March 2005
- February 2005
- January 2005
- December 2004
- November 2004
- October 2004
- June 2004
- May 2004
- March 2004
- October 2003
-
Meta




