Django

Code

Changeset 5791

Show
Ignore:
Timestamp:
08/03/07 14:48:04 (1 year ago)
Author:
danderson
Message:

schema-evolution:
backported on request to mysql_old (untested)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/schema-evolution/django/db/backends/mysql_old/base.py

    r5785 r5791  
    259259    return [] 
    260260 
     261def get_change_table_name_sql( table_name, old_table_name ): 
     262    return ['ALTER TABLE '+ quote_name(old_table_name) +' RENAME TO '+ quote_name(table_name) + ';'] 
     263 
     264def get_change_column_name_sql( table_name, indexes, old_col_name, new_col_name, col_def ): 
     265    # mysql doesn't support column renames (AFAIK), so we fake it 
     266    # TODO: only supports a single primary key so far 
     267    pk_name = None 
     268    for key in indexes.keys(): 
     269        if indexes[key]['primary_key']: pk_name = key 
     270    output = [] 
     271    output.append( 'ALTER TABLE '+ quote_name(table_name) +' CHANGE COLUMN '+ quote_name(old_col_name) +' '+ quote_name(new_col_name) +' '+ col_def + ';' ) 
     272    return output 
     273 
     274def get_change_column_def_sql( table_name, col_name, col_type, null, unique, primary_key ): 
     275    output = [] 
     276    col_def = col_type +' '+ ('%sNULL' % (not null and 'NOT ' or '')) 
     277    if unique: 
     278        col_def += ' '+ 'UNIQUE' 
     279    if primary_key: 
     280        col_def += ' '+ 'PRIMARY KEY' 
     281    output.append( 'ALTER TABLE '+ quote_name(table_name) +' MODIFY COLUMN '+ quote_name(col_name) +' '+ col_def + ';' ) 
     282    return output 
     283 
     284def get_add_column_sql( table_name, col_name, col_type, null, unique, primary_key  ): 
     285    output = [] 
     286    field_output = [] 
     287    field_output.append('ALTER TABLE') 
     288    field_output.append(quote_name(table_name)) 
     289    field_output.append('ADD COLUMN') 
     290    field_output.append(quote_name(col_name)) 
     291    field_output.append(col_type) 
     292    field_output.append(('%sNULL' % (not null and 'NOT ' or ''))) 
     293    if unique: 
     294        field_output.append(('UNIQUE')) 
     295    if primary_key: 
     296        field_output.append(('PRIMARY KEY')) 
     297    output.append(' '.join(field_output) + ';') 
     298    return output 
     299 
     300def get_drop_column_sql( table_name, col_name ): 
     301    output = [] 
     302    output.append( 'ALTER TABLE '+ quote_name(table_name) +' DROP COLUMN '+ quote_name(col_name) + ';' ) 
     303    return output 
     304     
     305     
    261306OPERATOR_MAPPING = { 
    262307    'exact': '= %s', 
  • django/branches/schema-evolution/django/db/backends/mysql_old/introspection.py

    r5734 r5791  
    7474    return indexes 
    7575 
     76def get_columns(cursor, table_name): 
     77    try: 
     78        cursor.execute("describe %s" % quote_name(table_name)) 
     79        return [row[0] for row in cursor.fetchall()] 
     80    except: 
     81        return [] 
     82     
     83def get_known_column_flags( cursor, table_name, column_name ): 
     84    cursor.execute("describe %s" % quote_name(table_name)) 
     85    dict = {} 
     86    for row in cursor.fetchall(): 
     87        if row[0] == column_name: 
     88 
     89            # maxlength check goes here 
     90            if row[1][0:7]=='varchar': 
     91                dict['maxlength'] = row[1][8:len(row[1])-1] 
     92             
     93            # default flag check goes here 
     94            if row[2]=='YES': dict['allow_null'] = True 
     95            else: dict['allow_null'] = False 
     96             
     97            # primary/foreign/unique key flag check goes here 
     98            if row[3]=='PRI': dict['primary_key'] = True 
     99            else: dict['primary_key'] = False 
     100            if row[3]=='FOR': dict['foreign_key'] = True 
     101            else: dict['foreign_key'] = False 
     102            if row[3]=='UNI': dict['unique'] = True 
     103            else: dict['unique'] = False 
     104             
     105            # default value check goes here 
     106            # if row[4]=='NULL': dict['default'] = None 
     107            # else: dict['default'] = row[4] 
     108            dict['default'] = row[4] 
     109             
     110    # print table_name, column_name, dict 
     111    return dict 
     112     
    76113DATA_TYPES_REVERSE = { 
    77114    FIELD_TYPE.BLOB: 'TextField',