mirror of
https://github.com/django/django.git
synced 2025-07-04 17:59:13 +00:00
schema-evolution:
backported on request to mysql_old (untested) git-svn-id: http://code.djangoproject.com/svn/django/branches/schema-evolution@5791 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
ba056e68c8
commit
ce1c58421a
@ -258,6 +258,51 @@ def get_sql_sequence_reset(style, model_list):
|
||||
# No sequence reset required
|
||||
return []
|
||||
|
||||
def get_change_table_name_sql( table_name, old_table_name ):
|
||||
return ['ALTER TABLE '+ quote_name(old_table_name) +' RENAME TO '+ quote_name(table_name) + ';']
|
||||
|
||||
def get_change_column_name_sql( table_name, indexes, old_col_name, new_col_name, col_def ):
|
||||
# mysql doesn't support column renames (AFAIK), so we fake it
|
||||
# TODO: only supports a single primary key so far
|
||||
pk_name = None
|
||||
for key in indexes.keys():
|
||||
if indexes[key]['primary_key']: pk_name = key
|
||||
output = []
|
||||
output.append( 'ALTER TABLE '+ quote_name(table_name) +' CHANGE COLUMN '+ quote_name(old_col_name) +' '+ quote_name(new_col_name) +' '+ col_def + ';' )
|
||||
return output
|
||||
|
||||
def get_change_column_def_sql( table_name, col_name, col_type, null, unique, primary_key ):
|
||||
output = []
|
||||
col_def = col_type +' '+ ('%sNULL' % (not null and 'NOT ' or ''))
|
||||
if unique:
|
||||
col_def += ' '+ 'UNIQUE'
|
||||
if primary_key:
|
||||
col_def += ' '+ 'PRIMARY KEY'
|
||||
output.append( 'ALTER TABLE '+ quote_name(table_name) +' MODIFY COLUMN '+ quote_name(col_name) +' '+ col_def + ';' )
|
||||
return output
|
||||
|
||||
def get_add_column_sql( table_name, col_name, col_type, null, unique, primary_key ):
|
||||
output = []
|
||||
field_output = []
|
||||
field_output.append('ALTER TABLE')
|
||||
field_output.append(quote_name(table_name))
|
||||
field_output.append('ADD COLUMN')
|
||||
field_output.append(quote_name(col_name))
|
||||
field_output.append(col_type)
|
||||
field_output.append(('%sNULL' % (not null and 'NOT ' or '')))
|
||||
if unique:
|
||||
field_output.append(('UNIQUE'))
|
||||
if primary_key:
|
||||
field_output.append(('PRIMARY KEY'))
|
||||
output.append(' '.join(field_output) + ';')
|
||||
return output
|
||||
|
||||
def get_drop_column_sql( table_name, col_name ):
|
||||
output = []
|
||||
output.append( 'ALTER TABLE '+ quote_name(table_name) +' DROP COLUMN '+ quote_name(col_name) + ';' )
|
||||
return output
|
||||
|
||||
|
||||
OPERATOR_MAPPING = {
|
||||
'exact': '= %s',
|
||||
'iexact': 'LIKE %s',
|
||||
|
@ -73,6 +73,43 @@ def get_indexes(cursor, table_name):
|
||||
indexes[row[4]] = {'primary_key': (row[2] == 'PRIMARY'), 'unique': not bool(row[1])}
|
||||
return indexes
|
||||
|
||||
def get_columns(cursor, table_name):
|
||||
try:
|
||||
cursor.execute("describe %s" % quote_name(table_name))
|
||||
return [row[0] for row in cursor.fetchall()]
|
||||
except:
|
||||
return []
|
||||
|
||||
def get_known_column_flags( cursor, table_name, column_name ):
|
||||
cursor.execute("describe %s" % quote_name(table_name))
|
||||
dict = {}
|
||||
for row in cursor.fetchall():
|
||||
if row[0] == column_name:
|
||||
|
||||
# maxlength check goes here
|
||||
if row[1][0:7]=='varchar':
|
||||
dict['maxlength'] = row[1][8:len(row[1])-1]
|
||||
|
||||
# default flag check goes here
|
||||
if row[2]=='YES': dict['allow_null'] = True
|
||||
else: dict['allow_null'] = False
|
||||
|
||||
# primary/foreign/unique key flag check goes here
|
||||
if row[3]=='PRI': dict['primary_key'] = True
|
||||
else: dict['primary_key'] = False
|
||||
if row[3]=='FOR': dict['foreign_key'] = True
|
||||
else: dict['foreign_key'] = False
|
||||
if row[3]=='UNI': dict['unique'] = True
|
||||
else: dict['unique'] = False
|
||||
|
||||
# default value check goes here
|
||||
# if row[4]=='NULL': dict['default'] = None
|
||||
# else: dict['default'] = row[4]
|
||||
dict['default'] = row[4]
|
||||
|
||||
# print table_name, column_name, dict
|
||||
return dict
|
||||
|
||||
DATA_TYPES_REVERSE = {
|
||||
FIELD_TYPE.BLOB: 'TextField',
|
||||
FIELD_TYPE.CHAR: 'CharField',
|
||||
|
Loading…
x
Reference in New Issue
Block a user