Ticket #54: alter_column_null.diff
| File alter_column_null.diff, 4.7 KB (added by anonymous, 4 years ago) |
|---|
-
tests/logic.py
117 117 app = self.create_test_app() 118 118 119 119 self.assertEqual( 120 ["0001_spam", "0002_eggs" ],120 ["0001_spam", "0002_eggs", "0003_alter_spam"], 121 121 migration.get_migration_names(app), 122 122 ) 123 123 … … 129 129 # Can't use vanilla import, modules beginning with numbers aren't in grammar 130 130 M1 = __import__("fakeapp.migrations.0001_spam", {}, {}, ['Migration']).Migration 131 131 M2 = __import__("fakeapp.migrations.0002_eggs", {}, {}, ['Migration']).Migration 132 M3 = __import__("fakeapp.migrations.0003_alter_spam", {}, {}, ['Migration']).Migration 132 133 133 134 self.assertEqual( 134 [M1, M2 ],135 [M1, M2, M3], 135 136 list(migration.get_migration_classes(app)), 136 137 ) 137 138 … … 158 159 {app: { 159 160 "0001_spam": migration.get_migration(app, "0001_spam"), 160 161 "0002_eggs": migration.get_migration(app, "0002_eggs"), 162 "0003_alter_spam": migration.get_migration(app, "0003_alter_spam"), 161 163 }}, 162 164 migration.all_migrations(), 163 165 ) … … 186 188 ( 187 189 (u"fakeapp", u"0001_spam"), 188 190 (u"fakeapp", u"0002_eggs"), 191 (u"fakeapp", u"0003_alter_spam"), 189 192 ), 190 193 migration.MigrationHistory.objects.values_list("app_name", "migration"), 191 194 ) … … 241 244 ( 242 245 (u"fakeapp", u"0001_spam"), 243 246 (u"fakeapp", u"0002_eggs"), 247 (u"fakeapp", u"0003_alter_spam"), 244 248 ), 245 249 migration.MigrationHistory.objects.values_list("app_name", "migration"), 246 250 ) 247 251 248 252 # Now roll them backwards 253 migration.migrate_app(app, target_name="0002", resolve_mode=None, fake=False, silent=True) 249 254 migration.migrate_app(app, target_name="0001", resolve_mode=None, fake=True, silent=True) 250 255 migration.migrate_app(app, target_name="zero", resolve_mode=None, fake=False, silent=True) 251 256 252 257 # Finish with none 253 258 self.assertEqual(list(migration.MigrationHistory.objects.all()), []) 259 260 def test_alter_column_null(self): 261 def null_ok(table_name, column_name): 262 from django.db import connection 263 cursor = connection.cursor() 264 for col in connection.introspection.get_table_description(cursor, table_name): 265 # elements as described in DB-API http://www.python.org/dev/peps/pep-0249/ 266 if col[0] == column_name: 267 return col[6] 268 raise Exception, 'column %s does not exist' % column_name 269 270 app = migration.get_app("fakeapp") 271 272 # by default name is NOT NULL 273 migration.migrate_app(app, target_name="0002", resolve_mode=None, fake=False, silent=True) 274 self.failIf(null_ok("southtest_spam", 'name')) 275 276 # after 0003, it should be NULL 277 migration.migrate_app(app, target_name="0003", resolve_mode=None, fake=False, silent=True) 278 self.assert_(null_ok("southtest_spam", 'name')) 279 280 # make sure it is NOT NULL again 281 migration.migrate_app(app, target_name="0002", resolve_mode=None, fake=False, silent=True) 282 self.failIf(null_ok("southtest_spam", 'name'), 'name not null after migration') 283 284 # finish with no migrations, otherwise other tests fail... 285 migration.migrate_app(app, target_name="zero", resolve_mode=None, fake=False, silent=True) -
db/generic.py
130 130 self.alter_column(table_name, name, field) 131 131 132 132 alter_string_set_type = 'ALTER COLUMN %(column)s TYPE %(type)s' 133 alter_string_set_null = 'ALTER COLUMN %(column)s SETNOT NULL'134 alter_string_drop_null = 'ALTER COLUMN %(column)s DROPNOT NULL'133 alter_string_set_null = 'ALTER COLUMN %(column)s DROP NOT NULL' 134 alter_string_drop_null = 'ALTER COLUMN %(column)s SET NOT NULL' 135 135 allows_combined_alters = True 136 136 137 137 def alter_column(self, table_name, name, field): … … 173 173 "type": field.db_type(), 174 174 } 175 175 if field.null: 176 sqls.append((self.alter_string_set_null % params, [])) 177 else: 176 178 sqls.append((self.alter_string_drop_null % params, [])) 177 else:178 sqls.append((self.alter_string_set_null % params, []))179 179 180 180 181 181 # TODO: Unique
