Ticket #54: alter_column_null.diff

File alter_column_null.diff, 4.7 KB (added by anonymous, 6 years ago)
  • tests/logic.py

     
    117117        app = self.create_test_app() 
    118118         
    119119        self.assertEqual( 
    120             ["0001_spam", "0002_eggs"], 
     120            ["0001_spam", "0002_eggs", "0003_alter_spam"], 
    121121            migration.get_migration_names(app), 
    122122        ) 
    123123     
     
    129129        # Can't use vanilla import, modules beginning with numbers aren't in grammar 
    130130        M1 = __import__("fakeapp.migrations.0001_spam", {}, {}, ['Migration']).Migration 
    131131        M2 = __import__("fakeapp.migrations.0002_eggs", {}, {}, ['Migration']).Migration 
     132        M3 = __import__("fakeapp.migrations.0003_alter_spam", {}, {}, ['Migration']).Migration 
    132133         
    133134        self.assertEqual( 
    134             [M1, M2], 
     135            [M1, M2, M3], 
    135136            list(migration.get_migration_classes(app)), 
    136137        ) 
    137138     
     
    158159            {app: { 
    159160                "0001_spam": migration.get_migration(app, "0001_spam"), 
    160161                "0002_eggs": migration.get_migration(app, "0002_eggs"), 
     162                "0003_alter_spam": migration.get_migration(app, "0003_alter_spam"), 
    161163            }}, 
    162164            migration.all_migrations(), 
    163165        ) 
     
    186188            ( 
    187189                (u"fakeapp", u"0001_spam"), 
    188190                (u"fakeapp", u"0002_eggs"), 
     191                (u"fakeapp", u"0003_alter_spam"), 
    189192            ), 
    190193            migration.MigrationHistory.objects.values_list("app_name", "migration"), 
    191194        ) 
     
    241244            ( 
    242245                (u"fakeapp", u"0001_spam"), 
    243246                (u"fakeapp", u"0002_eggs"), 
     247                (u"fakeapp", u"0003_alter_spam"), 
    244248            ), 
    245249            migration.MigrationHistory.objects.values_list("app_name", "migration"), 
    246250        ) 
    247251         
    248252        # Now roll them backwards 
     253        migration.migrate_app(app, target_name="0002", resolve_mode=None, fake=False, silent=True) 
    249254        migration.migrate_app(app, target_name="0001", resolve_mode=None, fake=True, silent=True) 
    250255        migration.migrate_app(app, target_name="zero", resolve_mode=None, fake=False, silent=True) 
    251256         
    252257        # Finish with none 
    253258        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

     
    130130                self.alter_column(table_name, name, field) 
    131131     
    132132    alter_string_set_type = 'ALTER COLUMN %(column)s TYPE %(type)s' 
    133     alter_string_set_null = 'ALTER COLUMN %(column)s SET NOT NULL' 
    134     alter_string_drop_null = 'ALTER COLUMN %(column)s DROP NOT 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' 
    135135    allows_combined_alters = True 
    136136     
    137137    def alter_column(self, table_name, name, field): 
     
    173173            "type": field.db_type(), 
    174174        } 
    175175        if field.null: 
     176            sqls.append((self.alter_string_set_null % params, [])) 
     177        else: 
    176178            sqls.append((self.alter_string_drop_null % params, [])) 
    177         else: 
    178             sqls.append((self.alter_string_set_null % params, [])) 
    179179         
    180180         
    181181        # TODO: Unique