1
0
mirror of https://github.com/django/django.git synced 2025-07-04 17:59:13 +00:00

schema-evolution: fixed f.aka bug; moved sql commenting from the base classes into management.py; added unit tests

git-svn-id: http://code.djangoproject.com/svn/django/branches/schema-evolution@5779 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Derek Anderson 2007-07-30 20:05:02 +00:00
parent 993ac8db97
commit 6dab0824be
5 changed files with 95 additions and 4 deletions

View File

@ -563,7 +563,7 @@ def get_sql_evolution_check_for_new_fields(klass, new_table_name):
db_table = new_table_name
for f in opts.fields:
existing_fields = introspection.get_columns(cursor,db_table)
if f.column not in existing_fields and f.aka and f.aka not in existing_fields and len(set(f.aka) & set(existing_fields))==0:
if f.column not in existing_fields and (not f.aka or f.aka not in existing_fields and len(set(f.aka) & set(existing_fields))==0):
rel_field = f
data_type = f.get_internal_type()
col_type = data_types[data_type]
@ -702,7 +702,7 @@ def get_sql_evolution_check_for_dead_fields(klass, new_table_name):
if len(suspect_fields)>0:
output.append( '-- warning: as the following may cause data loss, it/they must be run manually' )
for suspect_field in suspect_fields:
output.append( backend.get_drop_column_sql( db_table, suspect_field ) )
output.append( '-- '+ backend.get_drop_column_sql( db_table, suspect_field ) )
output.append( '-- end warning' )
return output

View File

@ -280,7 +280,7 @@ def get_add_column_sql( table_name, col_name, col_type, null, unique, primary_ke
def get_drop_column_sql( table_name, col_name ):
output = []
output.append( '-- ALTER TABLE '+ quote_name(table_name) +' DROP COLUMN '+ quote_name(col_name) + ';' )
output.append( 'ALTER TABLE '+ quote_name(table_name) +' DROP COLUMN '+ quote_name(col_name) + ';' )
return '\n'.join(output)

View File

@ -315,7 +315,7 @@ def get_add_column_sql( table_name, col_name, col_type, null, unique, primary_ke
def get_drop_column_sql( table_name, col_name ):
output = []
output.append( '-- ALTER TABLE '+ quote_name(table_name) +' DROP COLUMN '+ quote_name(col_name) + ';' )
output.append( 'ALTER TABLE '+ quote_name(table_name) +' DROP COLUMN '+ quote_name(col_name) + ';' )
return '\n'.join(output)
# Register these custom typecasts, because Django expects dates/times to be

View File

@ -0,0 +1,91 @@
"""
Schema Evolution Tests
"""
from django.db import models
GENDER_CHOICES = (
('M', 'Male'),
('F', 'Female'),
)
class Person(models.Model):
name = models.CharField(maxlength=20)
gender = models.CharField(maxlength=1, choices=GENDER_CHOICES)
gender2 = models.CharField(maxlength=1, choices=GENDER_CHOICES, aka='gender_old')
def __unicode__(self):
return self.name
class Meta:
aka = ('PersonOld', 'OtherBadName')
__test__ = {'API_TESTS':"""
>>> import django
>>> from django.core import management
>>> from django.db import backend, models
>>> from django.db import connection, get_introspection_module
>>> app = models.get_apps()[-1]
>>> cursor = connection.cursor()
# the table as it is supposed to be
>>> create_table_sql = management.get_sql_all(app)
>>> print create_table_sql
['CREATE TABLE `schema_evolution_person` (\\n `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,\\n `name` varchar(20) NOT NULL,\\n `gender` varchar(1) NOT NULL,\\n `gender2` varchar(1) NOT NULL\\n)\\n;']
# make sure we don't evolve an unedited table
>>> management.get_sql_evolution(app)
[]
# delete a column, so it looks like we've recently added a field
>>> sql = backend.get_drop_column_sql( 'schema_evolution_person', 'gender' )
>>> print sql
ALTER TABLE `schema_evolution_person` DROP COLUMN `gender`;
>>> cursor.execute(sql)
0L
>>> management.get_sql_evolution(app)
['ALTER TABLE `schema_evolution_person` ADD COLUMN `gender` varchar(1) NOT NULL;']
# reset the db
>>> cursor.execute('DROP TABLE schema_evolution_person;'); cursor.execute(create_table_sql[0])
0L\n0L
# add a column, so it looks like we've recently deleted a field
>>> cursor.execute('ALTER TABLE `schema_evolution_person` ADD COLUMN `gender_nothere` varchar(1) NOT NULL;')
0L
>>> management.get_sql_evolution(app)
['-- warning: as the following may cause data loss, it/they must be run manually', u'-- ALTER TABLE `schema_evolution_person` DROP COLUMN `gender_nothere`;', '-- end warning']
# reset the db
>>> cursor.execute('DROP TABLE schema_evolution_person;'); cursor.execute(create_table_sql[0])
0L\n0L
# rename column, so it looks like we've recently renamed a field
>>> cursor.execute('ALTER TABLE `schema_evolution_person` CHANGE COLUMN `gender2` `gender_old` varchar(1) NOT NULL;')
0L
>>> management.get_sql_evolution(app)
['ALTER TABLE `schema_evolution_person` CHANGE COLUMN `gender_old` `gender2` varchar(1) NOT NULL;']
# reset the db
>>> cursor.execute('DROP TABLE schema_evolution_person;'); cursor.execute(create_table_sql[0])
0L\n0L
# rename table, so it looks like we've recently renamed a model
>>> cursor.execute('ALTER TABLE `schema_evolution_person` RENAME TO `schema_evolution_personold`')
0L
>>> management.get_sql_evolution(app)
['ALTER TABLE `schema_evolution_personold` RENAME TO `schema_evolution_person`;']
# reset the db
>>> cursor.execute(create_table_sql[0])
0L
# change column flags, so it looks like we've recently changed a column flag
>>> cursor.execute('ALTER TABLE `schema_evolution_person` MODIFY COLUMN `name` varchar(10) NULL;')
0L
>>> management.get_sql_evolution(app)
['ALTER TABLE `schema_evolution_person` MODIFY COLUMN `name` varchar(20) NOT NULL;']
"""}