Common Pitfalls with Django and South
andrewingram.net
andrewingram.net
I'm hoping I can resolve some of these issues (like the permissions thing) when we start moving parts of the code into Django proper.
Then its quite tricky to untangle them. Maybe there's a way to do a dry run full migration (fresh install) test or even a check and warning when creating a new migration. of course it usually involves multiple sibling apps that have too much knowledge of each other's FKs.
anyway, thanks for south !
Not really HN discussion points I guess but I have recently started using South on a project and the lack of clear direction here confused me. This bug has been known for quite a while but I couldn't find any best practise recommendations for either situation.
For Git stuff, just make sure you're aware of who is writing migrations where and know what to do when you get a conflict. Making sure you have lots of smaller apps rather than one giant one really helps.
Another pain point with South is that you run your migrations after pushing a new release. That makes for hard zero-downtime updates, because your codebase and the database are out of sync for a while before you can run `django-admin.py migrate`.
After having used South, I feel the best approach still is to write your own SQL migrations by hand and run it against your database pre-deploy, never destroy data (erase/update columns/tables), and then make sure your codebase handles the new/absent columns/tables transparently. This way you can revert a broken release just pushing the previous version.
If all your changes are additions, and your app is managed by supervisord, it's fairly trivial to pull the code updates, run the migrations, then bounce your supervisor instance.
Like I said though, if you're deleting, the process is to stage out the deletes into a separate migration so that your codebase is using new or added/replacement columns before you remove tables or you're kind of screwed.
Also bear in mind that this knowledge is somewhat dated as all my recent Django ORM experience is based on Oracle (ugh) which I haven't gotten South to work with.
The problem is that some configurations need workers to restart every X requests to avoid memory leaks, and that makes the deployment process completely non-deterministic if you're pushing new code and expecting workers to still serve the old one. There are troubles with dynamic imports too, which happen on large codebases.
I've never noticed any errors deploying as described, but that doesn't necessarily mean anything in light of this information.
Also, not a great solution, but we monkeypatched south to simply use the number in the file name, ie 0001.py. This way if two developers try to commit a migration, they cause a conflict, and we can fix it then instead of finding out later. It makes the filename less friendly, but we usually use grep/ack to find a migration anyways.
[Update: I've changed the last paragraph of the article to reference your comment and this solution]
I think the solution is to create a version of loaddata that knows how to work with South's frozen ORM. A quick search turned this up: http://stackoverflow.com/questions/5472925/django-loading-da...
http://code.google.com/p/django-command-extensions/wiki/Dump...
When we switched to db.execute, we lost the ability to do backwards migrations, but that's ok. Backwards migrations are really, really dangerous. In fact, deleting columns, ever, is really dangerous. If you are deleting data permanently it is always better to hand craft precisely what you want to do, and have everyone look it over, rather than running a backwards migration.
Caveat/qualifier: every project was on Postgres.
It's entirely possible to shoot yourself in the foot, but that's also what makes South versatile enough. Hell, there's a good reason that I recommend raw SQL in migrations as a solution sometimes - you'll still get the record-keeping and dependency stuff, so it's not like you're throwing it all away.
If you look at your history and think 'oh, migration 0005 hasn't been run', don't do 'migrate 0005'. That will of course take you back to 0005, deleting any modifications made after 0005.
Does anyone know an easy way to run the migration on a Heroku Postgres database without pushing the code to a live server?
heroku pg:credentials DATABASE_URL
And then you can just set it up as the database of a different install. Probably better to deploy the code before migrating though. Use maintenance mode to keep people out while you do.Where I work, we have a templated Django project that has a fabfile to do this: https://github.com/tangentlabs/tangent-django-boilerplate
If you look in the deploy function (https://github.com/tangentlabs/tangent-django-boilerplate/bl...), you can see the flow is something like:
def deploy():
deploy_codebase()
...
migrate()
...
switch_symlink()Great article.
Care to elaborate? I've always found South pretty straightforward.
When model-based migrations work, they work really well, and help reinforce code schema/database schema being entirely in sync.
That said, I think introduced properly/carefully South is the easiest way to do migrations by a landslide. If anything, I'd expect people to eventually grow out of it, not into it.. And not most people either. I think a number of the commenters talking about hitting bumps in the road once they got to (gasp!) dozens of models or migrations, probably bailed prematurely. Our project is 300+ models, and well over 500 migrations now and South still suits us pretty nicely.
If you'd like any help reviewing tutorial materials, I'd be happy to lend a hand. I haven't looked lately, but I'd love to see a better intro blog post for South show up. A couple years ago when I was learning I found what's out there just a little bit obtuse (not to knock Andrew's very solid tutorial at all, I just could have used something even more stripped down and careful about introducing concepts to get started).
I have found it has very nice/shallow "curve". In that if you are doing simple stuff, South is simple. Don't even have to look at generated migrations, just use management commends. As you need more complex things, you can learn more details about South.