diff --git a/extensions/postfix_autoreply/migrations/0002_auto__add_armessage__add_arhistoric.py b/extensions/postfix_autoreply/migrations/0002_auto__add_armessage__add_arhistoric.py index 18d1096b7..5a7fe478f 100644 --- a/extensions/postfix_autoreply/migrations/0002_auto__add_armessage__add_arhistoric.py +++ b/extensions/postfix_autoreply/migrations/0002_auto__add_armessage__add_arhistoric.py @@ -3,14 +3,15 @@ import datetime from south.db import db from south.v2 import SchemaMigration from django.db import models +from modoboa.lib import db_table_exists class Migration(SchemaMigration): def forwards(self, orm): # Renaming/Creating model 'ARmessage' - try: + if db_table_exists('main_armessage'): db.rename_table('main_armessage', 'postfix_autoreply_armessage') - except Exception: + else: db.create_table('postfix_autoreply_armessage', ( ('mbox', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['admin.Mailbox'])), ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)), @@ -24,9 +25,9 @@ class Migration(SchemaMigration): models.DateTimeField(null=True)) # Renaming model 'ARhistoric' - try: + if db_table_exists('main_arhistoric'): db.rename_table('main_arhistoric', 'postfix_autoreply_arhistoric') - except Exception: + else: db.create_table('postfix_autoreply_arhistoric', ( ('armessage', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['postfix_autoreply.ARmessage'])), ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)), diff --git a/lib/__init__.py b/lib/__init__.py index b69a2a59d..a5da010d2 100644 --- a/lib/__init__.py +++ b/lib/__init__.py @@ -89,6 +89,25 @@ def is_not_localadmin(errortpl="error"): return wrapped_f return dec +def db_table_exists(table, cursor=None): + """Check if table exists + + Taken from here: + https://gist.github.com/527113/307c2dec09ceeb647b8fa1d6d49591f3352cb034 + + """ + try: + if not cursor: + from django.db import connection + cursor = connection.cursor() + if not cursor: + raise Exception + table_names = connection.introspection.get_table_list(cursor) + except: + raise Exception("unable to determine if the table '%s' exists" % table) + else: + return table in table_names + class Singleton(type): def __init__(cls, name, bases, dict): super(Singleton, cls).__init__(name, bases, dict)