diff options
author | Tomasz Zielinski <tomasz.zielinski@pyconsultant.eu> | 2012-01-06 18:50:56 +0100 |
---|---|---|
committer | Tomasz Zielinski <tomasz.zielinski@pyconsultant.eu> | 2012-01-06 22:37:35 +0100 |
commit | 51b6be4b04ec97d464c41d81050dde48ac488d20 (patch) | |
tree | d10a245fbe07cbce96a650ae977376e6649a3aad | |
parent | f303ea0f462e156d28fdc65f3c1b05b673da4ebe (diff) | |
download | askbot-51b6be4b04ec97d464c41d81050dde48ac488d20.tar.gz askbot-51b6be4b04ec97d464c41d81050dde48ac488d20.tar.bz2 askbot-51b6be4b04ec97d464c41d81050dde48ac488d20.zip |
Added Post.id sequence check for migration 0090; minor Postgres-related fixes;
-rw-r--r-- | askbot/migrations/0022_init_postgresql_full_text_search.py | 4 | ||||
-rw-r--r-- | askbot/migrations/0090_postize_q_a_c.py | 46 | ||||
-rw-r--r-- | askbot/migrations/0092_postize_vote_and_activity.py | 3 | ||||
-rw-r--r-- | askbot/models/post.py | 4 | ||||
-rw-r--r-- | askbot/views/writers.py | 17 |
5 files changed, 54 insertions, 20 deletions
diff --git a/askbot/migrations/0022_init_postgresql_full_text_search.py b/askbot/migrations/0022_init_postgresql_full_text_search.py index b01c1285..9f2a4b4b 100644 --- a/askbot/migrations/0022_init_postgresql_full_text_search.py +++ b/askbot/migrations/0022_init_postgresql_full_text_search.py @@ -11,8 +11,8 @@ class Migration(DataMigration): def forwards(self, orm): "Write your forwards methods here." - if 'postgresql_psycopg2' in askbot.get_database_engine_name(): - management.call_command('init_postgresql_full_text_search') +# if 'postgresql_psycopg2' in askbot.get_database_engine_name(): +# management.call_command('init_postgresql_full_text_search') def backwards(self, orm): "Write your backwards methods here." diff --git a/askbot/migrations/0090_postize_q_a_c.py b/askbot/migrations/0090_postize_q_a_c.py index 62bb214e..91fdf02c 100644 --- a/askbot/migrations/0090_postize_q_a_c.py +++ b/askbot/migrations/0090_postize_q_a_c.py @@ -1,18 +1,20 @@ # encoding: utf-8 import datetime +import sys from south.db import db from south.v2 import DataMigration from django.db import models +from django.conf import settings -from askbot.migrations import TERM_YELLOW, TERM_RESET +from askbot.migrations import TERM_RED_BOLD, TERM_GREEN, TERM_RESET class Migration(DataMigration): def forwards(self, orm): orm.Post.objects.all().delete() # in case there are some leftovers after this migration failed before - # TODO: start post.id from max(q.id, a.id, c.id) + store old_q_id, old_a_id, old_c_id inside - this is to make sure - # we can make old URLs work flawlessly + if 'test' not in sys.argv: # Don't confuse users + print TERM_RED_BOLD, "!!! Remember to not remove the old content types for Question, Answer and Comment models until further notice from migration 0101!", TERM_RESET post_id = max( # 'or 0' protects against None @@ -21,7 +23,8 @@ class Migration(DataMigration): orm.Comment.objects.aggregate(max_id=models.Max('id'))['max_id'] or 0, ) - print TERM_YELLOW, '[DEBUG] Initial Post.id ==', post_id + 1, TERM_RESET + if 'test' not in sys.argv: # Don't confuse users + print TERM_GREEN, '[DEBUG] Initial Post.id ==', post_id + 1, TERM_RESET for q in orm.Question.objects.all(): post_id += 1 @@ -159,6 +162,41 @@ class Migration(DataMigration): is_anonymous=False, ) + if orm.Post.objects.exists(): + # Check Post.id sequence/auto-increment to make sure new Post-s don't start from id=1 + if db.backend_name == 'mysql': + # Docs: + # - http://stackoverflow.com/questions/933565/get-auto-increment-value-with-mysql-query + # - + autoincrement = db.execute("SELECT auto_increment FROM information_schema.tables WHERE table_name='askbot_post' AND table_schema=DATABASE()")[0][0] + + elif db.backend_name == 'sqlite3': + # Docs: + # - http://www.sqlite.org/autoinc.html + # - http://www.sqlite.org/c3ref/last_insert_rowid.html + # - http://www.sqlite.org/faq.html#q1 + # - http://stackoverflow.com/questions/531109/how-to-return-the-value-of-auto-increment-column-in-sqlite-with-vb6 + autoincrement = db.execute("SELECT last_insert_rowid() FROM askbot_post")[0][0] + 1 + + elif db.backend_name == 'postgres': + # Docs: + # - http://lindsaar.net/2008/11/2/how-to-reset-a-sequence-with-postgresql + # - http://www.postgresql.org/docs/8.3/static/functions-sequence.html + # Note that SELECT SETVAL(...) returns the value being set + autoincrement = db.execute("SELECT SETVAL('askbot_post_id_seq', %s)", params=[post_id + 1])[0][0] + + else: + autoincrement = -1 + print TERM_RED_BOLD, "You are using `%s` database backend which is not officially supported. " \ + "Therefore after migrations are applied you should make sure that autoincrement/sequence value for " \ + "table `askbot_post` is set to %d" % (post_id + 1), TERM_RESET + + if autoincrement != -1: + if autoincrement != (post_id + 1): + raise ValueError('Auto_increment for askbot_post table should be %d but is %d!' % (post_id + 1, autoincrement)) + print TERM_GREEN, '[DEBUG] %s: auto-increment/sequence-value for askbot_post table is OK (%d)' % ( + db.backend_name, autoincrement), TERM_RESET + # Verify that numbers match sum_all = orm.Question.objects.count() + orm.Answer.objects.count() + orm.Comment.objects.count() if sum_all != orm.Post.objects.count(): diff --git a/askbot/migrations/0092_postize_vote_and_activity.py b/askbot/migrations/0092_postize_vote_and_activity.py index 65fb1cf3..77d72c1f 100644 --- a/askbot/migrations/0092_postize_vote_and_activity.py +++ b/askbot/migrations/0092_postize_vote_and_activity.py @@ -29,9 +29,6 @@ class Migration(DataMigration): # (if migrations are applied in a row then contenttypes update is not called between them) ct_post, c = orm['contenttypes.ContentType'].objects.get_or_create(app_label='askbot', model='post', defaults={'name': 'post'}) - if 'test' not in sys.argv: # Don't confuse users - print TERM_RED_BOLD, "!!! Remember to not remove the old content types for Question, Answer and Comment models until further notice from migration 0101!", TERM_RESET - abandoned_activities = [] for a in orm.Activity.objects.all(): diff --git a/askbot/models/post.py b/askbot/models/post.py index 0ed57101..ed98a26d 100644 --- a/askbot/models/post.py +++ b/askbot/models/post.py @@ -696,10 +696,10 @@ class Post(models.Model): def save(self, *args, **kwargs): if self.is_answer() and self.is_anonymous: raise ValueError('Answer cannot be anonymous!') - models.Model.save(self, *args, **kwargs) # TODO: figure out how to use super() here + super(Post, self).save(self, *args, **kwargs) if self.is_answer() and 'postgres' in settings.DATABASE_ENGINE: #hit the database to trigger update of full text search vector - self.question.save() + self.thread._question_post().save() def _get_slug(self): if not self.is_question(): diff --git a/askbot/views/writers.py b/askbot/views/writers.py index d57af434..5ffc67cf 100644 --- a/askbot/views/writers.py +++ b/askbot/views/writers.py @@ -522,15 +522,14 @@ def answer(request, id):#process a new answer request.user.message_set.create(message = unicode(e)) else: request.session.flush() - anon = models.AnonymousAnswer( - question_post=question, - wiki=wiki, - text=text, - summary=strip_tags(text)[:120], - session_key=request.session.session_key, - ip_addr=request.META['REMOTE_ADDR'], - ) - anon.save() + models.AnonymousAnswer.objects.create( + question=question, + wiki=wiki, + text=text, + summary=strip_tags(text)[:120], + session_key=request.session.session_key, + ip_addr=request.META['REMOTE_ADDR'], + ) return HttpResponseRedirect(url_utils.get_login_url()) return HttpResponseRedirect(question.get_absolute_url()) |