Django

Code

Show
Ignore:
Timestamp:
08/03/07 17:38:44 (1 year ago)
Author:
danderson
Message:

schema-evolution:
added "default" support so when you add a not null column to a non-empty table you don't get an sql exception

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/schema-evolution/tests/modeltests/schema_evolution/models.py

    r5792 r5794  
    2323 
    2424class Muebles(models.Model): 
    25     tipo = models.CharField(maxlength=40
     25    tipo = models.CharField(maxlength=40, default="woot"
    2626    # new fields 
    2727    fecha_publicacion = models.DateTimeField('date published') 
     
    98980L\n0L 
    9999 
    100 # delete a datetime column pair, so it looks like we've recently added a datetime field 
     100# delete a datetime column, so it looks like we've recently added a datetime field 
    101101>>> for sql in backend.get_drop_column_sql( 'schema_evolution_muebles', 'fecha_publicacion' ): print sql; cursor.execute(sql) 
    102102ALTER TABLE `schema_evolution_muebles` DROP COLUMN `fecha_publicacion`; 
     
    104104>>> management.get_sql_evolution(app) 
    105105['ALTER TABLE `schema_evolution_muebles` ADD COLUMN `fecha_publicacion` datetime NOT NULL;'] 
     106 
     107# reset the db 
     108>>> cursor.execute('DROP TABLE schema_evolution_muebles;'); cursor.execute(create_table_sql[1]) 
     1090L\n0L 
     110 
     111# delete a column with a default value, so it looks like we've recently added a column 
     112>>> for sql in backend.get_drop_column_sql( 'schema_evolution_muebles', 'tipo' ): print sql; cursor.execute(sql) 
     113ALTER TABLE `schema_evolution_muebles` DROP COLUMN `tipo`; 
     1140L 
     115>>> management.get_sql_evolution(app) 
     116['ALTER TABLE `schema_evolution_muebles` ADD COLUMN `tipo` varchar(40) NOT NULL DEFAULT `woot`;'] 
    106117 
    107118""" 
     
    125136 
    126137# add a column, so it looks like we've recently deleted a field 
    127 >>> for sql in backend.get_add_column_sql( 'schema_evolution_person', 'gender_nothere', 'varchar(1)', True, False, False ): cursor.execute(sql) 
     138>>> for sql in backend.get_add_column_sql( 'schema_evolution_person', 'gender_nothere', 'varchar(1)', True, False, False, None ): cursor.execute(sql) 
    128139>>> management.get_sql_evolution(app) 
    129140['-- warning: the following may cause data loss', u'ALTER TABLE "schema_evolution_person" DROP COLUMN "gender_nothere";', '-- end warning'] 
     
    149160 
    150161# change column flags, so it looks like we've recently changed a column flag 
    151 >>> for sql in backend.get_change_column_def_sql( 'schema_evolution_person', 'name', 'varchar(10)', True, False, False ): cursor.execute(sql) 
     162>>> for sql in backend.get_change_column_def_sql( 'schema_evolution_person', 'name', 'varchar(10)', True, False, False, None ): cursor.execute(sql) 
    152163>>> management.get_sql_evolution(app) 
    153164['ALTER TABLE "schema_evolution_person" ADD COLUMN "name_tmp" varchar(20);', 'UPDATE "schema_evolution_person" SET "name_tmp" = "name";', 'ALTER TABLE "schema_evolution_person" DROP COLUMN "name";', 'ALTER TABLE "schema_evolution_person" RENAME COLUMN "name_tmp" TO "name";', 'ALTER TABLE "schema_evolution_person" ALTER COLUMN "name" SET NOT NULL;'] 
     
    161172>>> management.get_sql_evolution(app) 
    162173['ALTER TABLE "schema_evolution_muebles" ADD COLUMN "fecha_publicacion" timestamp with time zone;', 'ALTER TABLE "schema_evolution_muebles" ALTER COLUMN "fecha_publicacion" SET NOT NULL;'] 
     174 
     175# reset the db 
     176>>> cursor.execute('DROP TABLE schema_evolution_muebles;'); cursor.execute(create_table_sql[1]) 
     177 
     178# delete a column with a default value, so it looks like we've recently added a column 
     179>>> for sql in backend.get_drop_column_sql( 'schema_evolution_muebles', 'tipo' ): print sql; cursor.execute(sql) 
     180ALTER TABLE "schema_evolution_muebles" DROP COLUMN "tipo"; 
     181>>> management.get_sql_evolution(app) 
     182['ALTER TABLE "schema_evolution_muebles" ADD COLUMN "tipo" varchar(40);', 'ALTER TABLE "schema_evolution_muebles" ALTER COLUMN "tipo" SET DEFAULT "woot";', 'ALTER TABLE "schema_evolution_muebles" ALTER COLUMN "tipo" SET NOT NULL;'] 
    163183""" 
    164184 
     
    277297['ALTER TABLE "schema_evolution_muebles" ADD COLUMN "fecha_publicacion" datetime NOT NULL;'] 
    278298 
    279  
    280 """ 
    281  
     299# reset the db 
     300>>> cursor.execute('DROP TABLE schema_evolution_muebles;').__class__ 
     301<class 'django.db.backends.sqlite3.base.SQLiteCursorWrapper'> 
     302>>> cursor.execute(create_table_sql[1]).__class__ 
     303<class 'django.db.backends.sqlite3.base.SQLiteCursorWrapper'> 
     304 
     305# delete a column with a default value, so it looks like we've recently added a column 
     306>>> for sql in ['DROP TABLE schema_evolution_muebles;','CREATE TABLE "schema_evolution_muebles" ("id" integer NOT NULL UNIQUE PRIMARY KEY,"fecha_publicacion" datetime NOT NULL);']: cursor.execute(sql).__class__ 
     307<class 'django.db.backends.sqlite3.base.SQLiteCursorWrapper'> 
     308<class 'django.db.backends.sqlite3.base.SQLiteCursorWrapper'> 
     309>>> management.get_sql_evolution(app) 
     310['ALTER TABLE "schema_evolution_muebles" ADD COLUMN "tipo" varchar(40) NOT NULL DEFAULT "woot";'] 
     311 
     312""" 
     313