From cc8f3e89a7eb775fef862aaa27f7a9a648052c6a Mon Sep 17 00:00:00 2001 From: Tomasz Zielinski Date: Mon, 2 Jan 2012 19:54:04 +0100 Subject: Fixed migration 0095 which was failing for fresh databases --- askbot/migrations/0095_postize_award_and_repute.py | 38 +++++++++++++--------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/askbot/migrations/0095_postize_award_and_repute.py b/askbot/migrations/0095_postize_award_and_repute.py index a65e5224..be74d688 100644 --- a/askbot/migrations/0095_postize_award_and_repute.py +++ b/askbot/migrations/0095_postize_award_and_repute.py @@ -11,24 +11,30 @@ class Migration(DataMigration): if 'test' in sys.argv: # This migration fails when testing... Ah those: crappy Askbot test suite and broken Django test framework ;;-) return - ct_question = orm['contenttypes.ContentType'].objects.get(app_label='askbot', model='question') - ct_answer = orm['contenttypes.ContentType'].objects.get(app_label='askbot', model='answer') - ct_comment = orm['contenttypes.ContentType'].objects.get(app_label='askbot', model='comment') + if orm.Award.objects.exists(): + # Note that for an empty database (most likely a fresh one) + # there are no contenttypes for Question/Answer/Comment models installed, + # because there are no such models anymore + # Therefore we only run this section if there are any Award-s to migrate, + # i.e. there is some legacy data and legacy contenttypes are likely to be present in the database + ct_question = orm['contenttypes.ContentType'].objects.get(app_label='askbot', model='question') + ct_answer = orm['contenttypes.ContentType'].objects.get(app_label='askbot', model='answer') + ct_comment = orm['contenttypes.ContentType'].objects.get(app_label='askbot', model='comment') - ct_post = orm['contenttypes.ContentType'].objects.get(app_label='askbot', model='post') + ct_post = orm['contenttypes.ContentType'].objects.get(app_label='askbot', model='post') - for aw in orm.Award.objects.all(): - ct = aw.content_type - aw.content_type = ct_post - if ct == ct_question: - aw.object_id = orm.Post.objects.get(self_question__id=aw.object_id).id - elif ct == ct_answer: - aw.object_id = orm.Post.objects.get(self_answer__id=aw.object_id).id - elif ct == ct_comment: - aw.object_id = orm.Post.objects.get(self_comment__id=aw.object_id).id - else: - raise ValueError('Unknown award subject!') - aw.save() + for aw in orm.Award.objects.all(): + ct = aw.content_type + aw.content_type = ct_post + if ct == ct_question: + aw.object_id = orm.Post.objects.get(self_question__id=aw.object_id).id + elif ct == ct_answer: + aw.object_id = orm.Post.objects.get(self_answer__id=aw.object_id).id + elif ct == ct_comment: + aw.object_id = orm.Post.objects.get(self_comment__id=aw.object_id).id + else: + raise ValueError('Unknown award subject!') + aw.save() ### -- cgit v1.2.3-1-g7c22