diff options
| author | Derek Anderson <public@kered.org> | 2007-08-03 22:38:44 +0000 |
|---|---|---|
| committer | Derek Anderson <public@kered.org> | 2007-08-03 22:38:44 +0000 |
| commit | db79faa3285361e6f6778bfc003edd8844196f7e (patch) | |
| tree | 8a9392f9b2a3079357c44c1b47ae833bc18e376a /tests/modeltests/schema_evolution | |
| parent | 6aad6a8a48b38f32ca77f758ea5bdc9eb187d96a (diff) | |
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
git-svn-id: http://code.djangoproject.com/svn/django/branches/schema-evolution@5794 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests/modeltests/schema_evolution')
| -rw-r--r-- | tests/modeltests/schema_evolution/models.py | 40 |
1 files changed, 36 insertions, 4 deletions
diff --git a/tests/modeltests/schema_evolution/models.py b/tests/modeltests/schema_evolution/models.py index 4118f94625..c0809055e6 100644 --- a/tests/modeltests/schema_evolution/models.py +++ b/tests/modeltests/schema_evolution/models.py @@ -22,7 +22,7 @@ class Person(models.Model): aka = ('PersonOld', 'OtherBadName') class Muebles(models.Model): - tipo = models.CharField(maxlength=40) + tipo = models.CharField(maxlength=40, default="woot") # new fields fecha_publicacion = models.DateTimeField('date published') @@ -97,13 +97,24 @@ if settings.DATABASE_ENGINE == 'mysql': >>> cursor.execute('DROP TABLE schema_evolution_person;'); cursor.execute(create_table_sql[0]) 0L\n0L -# delete a datetime column pair, so it looks like we've recently added a datetime field +# delete a datetime column, so it looks like we've recently added a datetime field >>> for sql in backend.get_drop_column_sql( 'schema_evolution_muebles', 'fecha_publicacion' ): print sql; cursor.execute(sql) ALTER TABLE `schema_evolution_muebles` DROP COLUMN `fecha_publicacion`; 0L >>> management.get_sql_evolution(app) ['ALTER TABLE `schema_evolution_muebles` ADD COLUMN `fecha_publicacion` datetime NOT NULL;'] +# reset the db +>>> cursor.execute('DROP TABLE schema_evolution_muebles;'); cursor.execute(create_table_sql[1]) +0L\n0L + +# delete a column with a default value, so it looks like we've recently added a column +>>> for sql in backend.get_drop_column_sql( 'schema_evolution_muebles', 'tipo' ): print sql; cursor.execute(sql) +ALTER TABLE `schema_evolution_muebles` DROP COLUMN `tipo`; +0L +>>> management.get_sql_evolution(app) +['ALTER TABLE `schema_evolution_muebles` ADD COLUMN `tipo` varchar(40) NOT NULL DEFAULT `woot`;'] + """ if settings.DATABASE_ENGINE == 'postgresql' or settings.DATABASE_ENGINE == 'postgresql_psycopg2' : @@ -124,7 +135,7 @@ if settings.DATABASE_ENGINE == 'postgresql' or settings.DATABASE_ENGINE == 'post >>> cursor.execute('DROP TABLE schema_evolution_person;'); cursor.execute(create_table_sql[0]) # add a column, so it looks like we've recently deleted a field ->>> for sql in backend.get_add_column_sql( 'schema_evolution_person', 'gender_nothere', 'varchar(1)', True, False, False ): cursor.execute(sql) +>>> for sql in backend.get_add_column_sql( 'schema_evolution_person', 'gender_nothere', 'varchar(1)', True, False, False, None ): cursor.execute(sql) >>> management.get_sql_evolution(app) ['-- warning: the following may cause data loss', u'ALTER TABLE "schema_evolution_person" DROP COLUMN "gender_nothere";', '-- end warning'] @@ -148,7 +159,7 @@ if settings.DATABASE_ENGINE == 'postgresql' or settings.DATABASE_ENGINE == 'post >>> cursor.execute(create_table_sql[0]) # change column flags, so it looks like we've recently changed a column flag ->>> for sql in backend.get_change_column_def_sql( 'schema_evolution_person', 'name', 'varchar(10)', True, False, False ): cursor.execute(sql) +>>> for sql in backend.get_change_column_def_sql( 'schema_evolution_person', 'name', 'varchar(10)', True, False, False, None ): cursor.execute(sql) >>> management.get_sql_evolution(app) ['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;'] @@ -160,6 +171,15 @@ if settings.DATABASE_ENGINE == 'postgresql' or settings.DATABASE_ENGINE == 'post ALTER TABLE "schema_evolution_muebles" DROP COLUMN "fecha_publicacion"; >>> management.get_sql_evolution(app) ['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;'] + +# reset the db +>>> cursor.execute('DROP TABLE schema_evolution_muebles;'); cursor.execute(create_table_sql[1]) + +# delete a column with a default value, so it looks like we've recently added a column +>>> for sql in backend.get_drop_column_sql( 'schema_evolution_muebles', 'tipo' ): print sql; cursor.execute(sql) +ALTER TABLE "schema_evolution_muebles" DROP COLUMN "tipo"; +>>> management.get_sql_evolution(app) +['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;'] """ if settings.DATABASE_ENGINE == 'sqlite3': @@ -276,6 +296,18 @@ if settings.DATABASE_ENGINE == 'sqlite3': >>> management.get_sql_evolution(app) ['ALTER TABLE "schema_evolution_muebles" ADD COLUMN "fecha_publicacion" datetime NOT NULL;'] +# reset the db +>>> cursor.execute('DROP TABLE schema_evolution_muebles;').__class__ +<class 'django.db.backends.sqlite3.base.SQLiteCursorWrapper'> +>>> cursor.execute(create_table_sql[1]).__class__ +<class 'django.db.backends.sqlite3.base.SQLiteCursorWrapper'> + +# delete a column with a default value, so it looks like we've recently added a column +>>> 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__ +<class 'django.db.backends.sqlite3.base.SQLiteCursorWrapper'> +<class 'django.db.backends.sqlite3.base.SQLiteCursorWrapper'> +>>> management.get_sql_evolution(app) +['ALTER TABLE "schema_evolution_muebles" ADD COLUMN "tipo" varchar(40) NOT NULL DEFAULT "woot";'] """ |
