From cab49ee6f0a896fe51d15a0af0978b340a3b03a8 Mon Sep 17 00:00:00 2001 From: Monty Taylor Date: Mon, 24 Dec 2012 21:39:53 +0000 Subject: Add MySQL support for safe_add_column. --- askbot/migrations_api/__init__.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/askbot/migrations_api/__init__.py b/askbot/migrations_api/__init__.py index 0260f51b..3ea250f2 100644 --- a/askbot/migrations_api/__init__.py +++ b/askbot/migrations_api/__init__.py @@ -13,14 +13,19 @@ def safe_add_column(table, column, column_data, keep_default = False): so, we need to add these columns here in separate transactions and roll back if they fail, if we want we could also record - which columns clash """ - try: - db.start_transaction() - db.add_column(table, column, column_data, keep_default = keep_default) - db.commit_transaction() - return True - except: - db.rollback_transaction() - return False + if db.backend_name=='mysql': + if len(db.execute('select column_name from information_schema.columns where table_name=%s and column_name=%s', params=[table, column])) == 0: + db.add_column(table, column, column_data, keep_default = keep_default) + + else: + try: + db.start_transaction() + db.add_column(table, column, column_data, keep_default = keep_default) + db.commit_transaction() + return True + except: + db.rollback_transaction() + return False def mysql_table_supports_full_text_search(table_name): -- cgit v1.2.3-1-g7c22 From 13823c0ed6e0ec54e3068efa62b2fe889ef8d799 Mon Sep 17 00:00:00 2001 From: Monty Taylor Date: Mon, 24 Dec 2012 21:39:27 +0000 Subject: Make column changes actually work with InnoDB --- askbot/migrations/__init__.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/askbot/migrations/__init__.py b/askbot/migrations/__init__.py index 86377b7b..b9683daf 100644 --- a/askbot/migrations/__init__.py +++ b/askbot/migrations/__init__.py @@ -1,5 +1,9 @@ from south.db import db from south.utils import ask_for_it_by_name +from south.v2 import SchemaMigration + +if not db.has_ddl_transactions: + SchemaMigration.no_dry_run = True # Terminal ANSI codes for printing colored text: # - http://code.google.com/p/testoob/source/browse/trunk/src/testoob/reporting/colored.py#20 @@ -67,3 +71,4 @@ def innodb_ready_rename_column(orm, models, table, old_column_name, new_column_n # INFO: ask_for_it_by_name() if equivalent to self.gf() which is usually used in migrations, e.g.: # db.alter_column('askbot_badgedata', 'slug', self.gf('django.db.models.fields.SlugField')(unique=True, max_length=50)) db.alter_column(table, new_column_name, field) + db.clear_deferred_sql() -- cgit v1.2.3-1-g7c22 From 2f8e4244747948e6bf2fe967d326dfa2d6f6366c Mon Sep 17 00:00:00 2001 From: Monty Taylor Date: Mon, 24 Dec 2012 21:40:01 +0000 Subject: Fix fake migration errors so we can find the real ones. --- .../migrations/0013_add_response_count__to_user.py | 17 +++++------- .../0014_rename_schema_from_forum_to_askbot.py | 21 +++++++------- .../0018_add___status__field_to_user_model.py | 16 +++++------ ...026_add_seen_and_new_response_counts_to_user.py | 8 ++---- ..._consecutive_days_visit_count__to__auth_user.py | 16 +++++------ .../migrations/0035_add_country_fields_to_user.py | 8 ++---- .../0037_add_marked_tags_to_user_profile.py | 12 ++++---- .../migrations/0038_add_tag_filter_strategies.py | 32 ++++++++++------------ ..._add_temporal_extra_column_for_datamigration.py | 6 ++-- .../0122_auth_user__add_subscribed_tag_field.py | 8 ++---- ...private__add_field_replyaddress_reply_action.py | 15 +++++----- .../migrations/0125_add_show_tags_field_to_user.py | 18 ++++++------ .../0126_add_field__auth_user__is_fake.py | 11 +++----- 13 files changed, 83 insertions(+), 105 deletions(-) diff --git a/askbot/migrations/0013_add_response_count__to_user.py b/askbot/migrations/0013_add_response_count__to_user.py index f3d724e3..68ac0c3c 100644 --- a/askbot/migrations/0013_add_response_count__to_user.py +++ b/askbot/migrations/0013_add_response_count__to_user.py @@ -3,6 +3,7 @@ import datetime from south.db import db from south.v2 import SchemaMigration from django.db import models +from askbot.migrations_api import safe_add_column class Migration(SchemaMigration): @@ -13,16 +14,12 @@ class Migration(SchemaMigration): a bit hacky but we have to do it as long as we keep patching auth models within the forum application """ - try: - db.add_column( - u'auth_user', - 'response_count', - self.gf('django.db.models.fields.IntegerField')(default=0, ), - keep_default=False - ) - except: - print 'probably already have column User.response_count' - pass + safe_add_column( + u'auth_user', + 'response_count', + self.gf('django.db.models.fields.IntegerField')(default=0, ), + keep_default=False + ) def backwards(self, orm): diff --git a/askbot/migrations/0014_rename_schema_from_forum_to_askbot.py b/askbot/migrations/0014_rename_schema_from_forum_to_askbot.py index f5f913da..f9a5b739 100644 --- a/askbot/migrations/0014_rename_schema_from_forum_to_askbot.py +++ b/askbot/migrations/0014_rename_schema_from_forum_to_askbot.py @@ -10,19 +10,20 @@ app_dir_name = os.path.basename(os.path.dirname(os.path.dirname(__file__))) class Migration(SchemaMigration): def forwards(self, orm): - try: - db.rename_table('forum_anonymousanswer', 'askbot_anonymousanswer') - db.rename_table('forum_anonymousquestion', 'askbot_anonymousquestion') - db.rename_table('forum_emailfeedsetting', 'askbot_emailfeedsetting') - db.rename_table('forum_markedtag', 'askbot_markedtag') - db.rename_table('forum_questionview', 'askbot_questionview') - db.rename_table('forum_validationhash', 'askbot_validationhash') - except: - pass + if app_dir_name == 'forum': + try: + db.rename_table('forum_anonymousanswer', 'askbot_anonymousanswer') + db.rename_table('forum_anonymousquestion', 'askbot_anonymousquestion') + db.rename_table('forum_emailfeedsetting', 'askbot_emailfeedsetting') + db.rename_table('forum_markedtag', 'askbot_markedtag') + db.rename_table('forum_questionview', 'askbot_questionview') + db.rename_table('forum_validationhash', 'askbot_validationhash') + except: + pass def backwards(self, orm): - if app_dirname == 'forum': + if app_dir_name == 'forum': db.rename_table('askbot_anonymousanswer', 'forum_anonymousanswer') db.rename_table('askbot_anonymousquestion', 'forum_anonymousquestion') db.rename_table('askbot_emailfeedsetting', 'forum_emailfeedsetting') diff --git a/askbot/migrations/0018_add___status__field_to_user_model.py b/askbot/migrations/0018_add___status__field_to_user_model.py index 5a713866..b30f30d7 100644 --- a/askbot/migrations/0018_add___status__field_to_user_model.py +++ b/askbot/migrations/0018_add___status__field_to_user_model.py @@ -3,21 +3,19 @@ import datetime from south.db import db from south.v2 import SchemaMigration from django.db import models +from askbot.migrations_api import safe_add_column class Migration(SchemaMigration): def forwards(self, orm): # Adding field 'User.status' - try: - db.add_column( - u'auth_user', - 'status', - self.gf('django.db.models.fields.CharField')(default ='w', max_length = 2), - keep_default=False - ) - except: - pass + safe_add_column( + u'auth_user', + 'status', + self.gf('django.db.models.fields.CharField')(default ='w', max_length = 2), + keep_default=False + ) def backwards(self, orm): diff --git a/askbot/migrations/0026_add_seen_and_new_response_counts_to_user.py b/askbot/migrations/0026_add_seen_and_new_response_counts_to_user.py index 4cd05f83..8b6002bf 100644 --- a/askbot/migrations/0026_add_seen_and_new_response_counts_to_user.py +++ b/askbot/migrations/0026_add_seen_and_new_response_counts_to_user.py @@ -3,16 +3,14 @@ import datetime from south.db import db from south.v2 import SchemaMigration from django.db import models +from askbot.migrations_api import safe_add_column class Migration(SchemaMigration): def forwards(self, orm): # Adding fields - try: - db.add_column('auth_user', 'new_response_count', self.gf('django.db.models.fields.IntegerField')(default=0), keep_default=False) - db.add_column('auth_user', 'seen_response_count', self.gf('django.db.models.fields.IntegerField')(default=0), keep_default=False) - except: - pass + safe_add_column('auth_user', 'new_response_count', self.gf('django.db.models.fields.IntegerField')(default=0), keep_default=False) + safe_add_column('auth_user', 'seen_response_count', self.gf('django.db.models.fields.IntegerField')(default=0), keep_default=False) def backwards(self, orm): # Deleting fields diff --git a/askbot/migrations/0033_add__consecutive_days_visit_count__to__auth_user.py b/askbot/migrations/0033_add__consecutive_days_visit_count__to__auth_user.py index 580c2f25..e5a10faf 100644 --- a/askbot/migrations/0033_add__consecutive_days_visit_count__to__auth_user.py +++ b/askbot/migrations/0033_add__consecutive_days_visit_count__to__auth_user.py @@ -3,21 +3,19 @@ import datetime from south.db import db from south.v2 import SchemaMigration from django.db import models +from askbot.migrations_api import safe_add_column class Migration(SchemaMigration): def forwards(self, orm): # Adding field 'User.consecutive_days_visit_count' - try: - db.add_column( - u'auth_user', - 'consecutive_days_visit_count', - self.gf('django.db.models.fields.IntegerField')(default = 0, max_length = 2), - keep_default=False - ) - except: - pass + safe_add_column( + u'auth_user', + 'consecutive_days_visit_count', + self.gf('django.db.models.fields.IntegerField')(default = 0, max_length = 2), + keep_default=False + ) def backwards(self, orm): diff --git a/askbot/migrations/0035_add_country_fields_to_user.py b/askbot/migrations/0035_add_country_fields_to_user.py index 02dd404c..340883ba 100644 --- a/askbot/migrations/0035_add_country_fields_to_user.py +++ b/askbot/migrations/0035_add_country_fields_to_user.py @@ -3,17 +3,15 @@ import datetime from south.db import db from south.v2 import SchemaMigration from django.db import models +from askbot.migrations_api import safe_add_column class Migration(SchemaMigration): def forwards(self, orm): # Adding model country fields to the model auth_user - try: - db.add_column(u'auth_user', 'country', self.gf('django_countries.fields.CountryField')(max_length=2, blank=True, null=True)) - db.add_column(u'auth_user', 'show_country', self.gf('django.db.models.fields.BooleanField')(default=False, blank=True)) - except: - pass + safe_add_column(u'auth_user', 'country', self.gf('django_countries.fields.CountryField')(max_length=2, blank=True, null=True)) + safe_add_column(u'auth_user', 'show_country', self.gf('django.db.models.fields.BooleanField')(default=False, blank=True)) def backwards(self, orm): diff --git a/askbot/migrations/0037_add_marked_tags_to_user_profile.py b/askbot/migrations/0037_add_marked_tags_to_user_profile.py index f10f53be..306e16e6 100644 --- a/askbot/migrations/0037_add_marked_tags_to_user_profile.py +++ b/askbot/migrations/0037_add_marked_tags_to_user_profile.py @@ -3,18 +3,16 @@ import datetime from south.db import db from south.v2 import SchemaMigration from django.db import models +from askbot.migrations_api import safe_add_column class Migration(SchemaMigration): def forwards(self, orm): - try: - # Adding field 'User.interesting_tags' - db.add_column(u'auth_user', 'interesting_tags', self.gf('django.db.models.fields.TextField')(blank=True, default = ''), keep_default=False) - # Adding field 'User.ignored_tags' - db.add_column(u'auth_user', 'ignored_tags', self.gf('django.db.models.fields.TextField')(blank=True, default = ''), keep_default=False) - except: - pass + # Adding field 'User.interesting_tags' + safe_add_column(u'auth_user', 'interesting_tags', self.gf('django.db.models.fields.TextField')(blank=True, default = ''), keep_default=False) + # Adding field 'User.ignored_tags' + safe_add_column(u'auth_user', 'ignored_tags', self.gf('django.db.models.fields.TextField')(blank=True, default = ''), keep_default=False) def backwards(self, orm): diff --git a/askbot/migrations/0038_add_tag_filter_strategies.py b/askbot/migrations/0038_add_tag_filter_strategies.py index c2596366..a24d12bf 100644 --- a/askbot/migrations/0038_add_tag_filter_strategies.py +++ b/askbot/migrations/0038_add_tag_filter_strategies.py @@ -4,29 +4,27 @@ from south.db import db from south.v2 import SchemaMigration from django.db import models from askbot import const +from askbot.migrations_api import safe_add_column class Migration(SchemaMigration): def forwards(self, orm): # Adding model country fields to the model auth_user - try: - db.add_column( - u'auth_user', - 'email_tag_filter_strategy', - self.gf( - 'django.db.models.fields.SmallIntegerField' - )(default = const.EXCLUDE_IGNORED) - ) - db.add_column( - u'auth_user', - 'display_tag_filter_strategy', - self.gf( - 'django.db.models.fields.SmallIntegerField' - )(default = const.INCLUDE_ALL) - ) - except: - pass + safe_add_column( + u'auth_user', + 'email_tag_filter_strategy', + self.gf( + 'django.db.models.fields.SmallIntegerField' + )(default = const.EXCLUDE_IGNORED) + ) + safe_add_column( + u'auth_user', + 'display_tag_filter_strategy', + self.gf( + 'django.db.models.fields.SmallIntegerField' + )(default = const.INCLUDE_ALL) + ) def backwards(self, orm): diff --git a/askbot/migrations/0043_add_temporal_extra_column_for_datamigration.py b/askbot/migrations/0043_add_temporal_extra_column_for_datamigration.py index 4a8140a4..7582dc04 100644 --- a/askbot/migrations/0043_add_temporal_extra_column_for_datamigration.py +++ b/askbot/migrations/0043_add_temporal_extra_column_for_datamigration.py @@ -3,14 +3,12 @@ import datetime from south.db import db from south.v2 import SchemaMigration from django.db import models +from askbot.migrations_api import safe_add_column class Migration(SchemaMigration): def forwards(self, orm): - try: - db.add_column(u'auth_user', 'avatar_type', self.gf('django.db.models.fields.CharField')(max_length=1, default='n'), keep_default=False) - except: - pass + safe_add_column(u'auth_user', 'avatar_type', self.gf('django.db.models.fields.CharField')(max_length=1, default='n'), keep_default=False) def backwards(self, orm): db.delete_column(u'auth_user', 'avatar_type') diff --git a/askbot/migrations/0122_auth_user__add_subscribed_tag_field.py b/askbot/migrations/0122_auth_user__add_subscribed_tag_field.py index d84666fb..7f7ff971 100644 --- a/askbot/migrations/0122_auth_user__add_subscribed_tag_field.py +++ b/askbot/migrations/0122_auth_user__add_subscribed_tag_field.py @@ -3,15 +3,13 @@ import datetime from south.db import db from south.v2 import SchemaMigration from django.db import models +from askbot.migrations_api import safe_add_column class Migration(SchemaMigration): def forwards(self, orm): - try: - # Adding field 'User.interesting_tags' - db.add_column(u'auth_user', 'subscribed_tags', self.gf('django.db.models.fields.TextField')(blank=True, default = ''), keep_default=False) - except: - pass + # Adding field 'User.interesting_tags' + safe_add_column(u'auth_user', 'subscribed_tags', self.gf('django.db.models.fields.TextField')(blank=True, default = ''), keep_default=False) def backwards(self, orm): # Deleting field 'User.interesting_tags' diff --git a/askbot/migrations/0124_auto__add_field_post_is_private__add_field_replyaddress_reply_action.py b/askbot/migrations/0124_auto__add_field_post_is_private__add_field_replyaddress_reply_action.py index b5a1e0c9..11b891da 100644 --- a/askbot/migrations/0124_auto__add_field_post_is_private__add_field_replyaddress_reply_action.py +++ b/askbot/migrations/0124_auto__add_field_post_is_private__add_field_replyaddress_reply_action.py @@ -3,6 +3,7 @@ import datetime from south.db import db from south.v2 import SchemaMigration from django.db import models +from askbot.migrations_api import safe_add_column class Migration(SchemaMigration): @@ -10,14 +11,14 @@ class Migration(SchemaMigration): def forwards(self, orm): # Adding field 'Post.is_private' db.start_transaction() - db.add_column('askbot_post', 'is_private', - self.gf('django.db.models.fields.BooleanField')(default=False), - keep_default=False) + safe_add_column('askbot_post', 'is_private', + self.gf('django.db.models.fields.BooleanField')(default=False), + keep_default=False) # Adding field 'ReplyAddress.reply_action' - db.add_column('askbot_replyaddress', 'reply_action', - self.gf('django.db.models.fields.CharField')(default='auto_answer_or_comment', max_length=32), - keep_default=False) + safe_add_column('askbot_replyaddress', 'reply_action', + self.gf('django.db.models.fields.CharField')(default='auto_answer_or_comment', max_length=32), + keep_default=False) # Changing field 'ReplyAddress.post' db.alter_column('askbot_replyaddress', 'post_id', self.gf('django.db.models.fields.related.ForeignKey')(null=True, to=orm['askbot.Post'])) @@ -26,7 +27,7 @@ class Migration(SchemaMigration): try: db.start_transaction() # Adding field 'User.interesting_tags' - db.add_column(u'auth_user', 'email_signature', self.gf('django.db.models.fields.TextField')(blank=True, default = ''), keep_default=False) + safe_add_column(u'auth_user', 'email_signature', self.gf('django.db.models.fields.TextField')(blank=True, default = ''), keep_default=False) db.commit_transaction() except: db.rollback_transaction() diff --git a/askbot/migrations/0125_add_show_tags_field_to_user.py b/askbot/migrations/0125_add_show_tags_field_to_user.py index cb7ab2bd..fc299d18 100644 --- a/askbot/migrations/0125_add_show_tags_field_to_user.py +++ b/askbot/migrations/0125_add_show_tags_field_to_user.py @@ -3,6 +3,7 @@ import datetime from south.db import db from south.v2 import SchemaMigration from django.db import models +from askbot.migrations_api import safe_add_column class Migration(SchemaMigration): @@ -10,16 +11,13 @@ class Migration(SchemaMigration): def forwards(self, orm): # Adding model show_marked_tags fields to the model auth_user - try: - db.add_column( - u'auth_user', - 'show_marked_tags', - self.gf( - 'django.db.models.fields.BooleanField' - )(default=True, blank=True) - ) - except: - pass + safe_add_column( + u'auth_user', + 'show_marked_tags', + self.gf( + 'django.db.models.fields.BooleanField' + )(default=True, blank=True) + ) def backwards(self, orm): diff --git a/askbot/migrations/0126_add_field__auth_user__is_fake.py b/askbot/migrations/0126_add_field__auth_user__is_fake.py index e0928ed7..783d1e27 100644 --- a/askbot/migrations/0126_add_field__auth_user__is_fake.py +++ b/askbot/migrations/0126_add_field__auth_user__is_fake.py @@ -1,17 +1,14 @@ # -*- coding: utf-8 -*- from south.db import db from south.v2 import SchemaMigration +from askbot.migrations_api import safe_add_column class Migration(SchemaMigration): def forwards(self, orm): - try: - # Adding field 'User.is_fake' - db.add_column( - u'auth_user', 'is_fake', - self.gf('django.db.models.fields.BooleanField')(default=False), keep_default=False) - except: - pass + safe_add_column( + u'auth_user', 'is_fake', + self.gf('django.db.models.fields.BooleanField')(default=False), keep_default=False) def backwards(self, orm): db.delete_column('auth_user', 'is_fake') -- cgit v1.2.3-1-g7c22