summaryrefslogtreecommitdiffstats
path: root/askbot/migrations_api/__init__.py
blob: 5a65f9cf845764e84053000eaaacbb5d5d5c0828 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""this module serves as a helper for the South orm
by mitigating absence of access to the django model api

since models change with time, this api is implemented in different
versions. Different versions do not need to have all the same functions.
"""
from south.db import db
from django.db import connection

def safe_add_column(table, column, column_data, keep_default = False):
    """when user calls syncdb with askbot the first time
    the auth_user table will be created together with the patched columns
    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
    """
    if db.backend_name in ('mysql', 'postgres'):
        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):
    """true, if engine is MyISAM"""
    cursor = connection.cursor()
    cursor.execute("SHOW CREATE TABLE %s" % table_name)
    data = cursor.fetchone()
    return 'ENGINE=MyISAM' in data[1]


def get_drop_index_sql(index_name, table_name):
    """returns sql for dropping index by name on table"""
    return 'ALTER TABLE %s DROP INDEX %s' % (table_name, index_name)


def get_create_full_text_index_sql(index_name, table_name, column_list):
    column_sql = '(%s)' % ','.join(column_list)
    query_template = 'CREATE FULLTEXT INDEX %s on %s %s'
    return query_template % (index_name, table_name, column_sql)


class BaseAPI(object):
    def __init__(self, orm):
        self.orm = orm