Django

Code

Changeset 4937

Show
Ignore:
Timestamp:
04/05/07 21:25:58 (2 years ago)
Author:
russellm
Message:

Fixed #3790 -- Fixed a problem with sequence resetting during fixture loads when using Postgres. Thanks to Jon Ballard and scott@staplefish.com for the report, and to Zach Thompson for suggesting a solution.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/AUTHORS

    r4920 r4937  
    193193    Tyson Tate <tyson@fallingbullets.com> 
    194194    thebjorn <bp@datakortet.no> 
     195    Zach Thompson <zthompson47@gmail.com> 
    195196    Tom Tobin 
    196197    Joe Topjian <http://joe.terrarum.net/geek/code/python/django/> 
  • django/trunk/django/core/management.py

    r4835 r4937  
    411411 
    412412def get_sql_sequence_reset(app): 
    413     "Returns a list of the SQL statements to reset PostgreSQL sequences for the given app." 
     413    "Returns a list of the SQL statements to reset sequences for the given app." 
    414414    from django.db import backend, models 
    415     output = [] 
    416     for model in models.get_models(app): 
    417         for f in model._meta.fields: 
    418             if isinstance(f, models.AutoField): 
    419                 output.append("%s setval('%s', (%s max(%s) %s %s));" % \ 
    420                     (style.SQL_KEYWORD('SELECT'), 
    421                     style.SQL_FIELD('%s_%s_seq' % (model._meta.db_table, f.column)), 
    422                     style.SQL_KEYWORD('SELECT'), 
    423                     style.SQL_FIELD(backend.quote_name(f.column)), 
    424                     style.SQL_KEYWORD('FROM'), 
    425                     style.SQL_TABLE(backend.quote_name(model._meta.db_table)))) 
    426                 break # Only one AutoField is allowed per model, so don't bother continuing. 
    427         for f in model._meta.many_to_many: 
    428             output.append("%s setval('%s', (%s max(%s) %s %s));" % \ 
    429                 (style.SQL_KEYWORD('SELECT'), 
    430                 style.SQL_FIELD('%s_id_seq' % f.m2m_db_table()), 
    431                 style.SQL_KEYWORD('SELECT'), 
    432                 style.SQL_FIELD(backend.quote_name('id')), 
    433                 style.SQL_KEYWORD('FROM'), 
    434                 style.SQL_TABLE(f.m2m_db_table()))) 
    435     return output 
    436 get_sql_sequence_reset.help_doc = "Prints the SQL statements for resetting PostgreSQL sequences for the given app name(s)." 
     415    return backend.get_sql_sequence_reset(style, models.get_models(app)) 
     416get_sql_sequence_reset.help_doc = "Prints the SQL statements for resetting sequences for the given app name(s)." 
    437417get_sql_sequence_reset.args = APP_ARGS 
    438418 
     
    13341314    from django.db.models import get_apps 
    13351315    from django.core import serializers 
    1336     from django.db import connection, transaction 
     1316    from django.db import connection, transaction, backend 
    13371317    from django.conf import settings 
    13381318    import sys 
     
    13401320    # Keep a count of the installed objects and fixtures 
    13411321    count = [0,0] 
    1342  
     1322    models = set() 
     1323     
    13431324    humanize = lambda dirname: dirname and "'%s'" % dirname or 'absolute path' 
    13441325 
     
    14021383                            for obj in objects: 
    14031384                                count[0] += 1 
     1385                                models.add(obj.object.__class__) 
    14041386                                obj.save() 
    14051387                            label_found = True 
     
    14231405        if verbosity > 0: 
    14241406            print "Installed %d object(s) from %d fixture(s)" % tuple(count) 
     1407        sequence_sql = backend.get_sql_sequence_reset(style, models) 
     1408        if sequence_sql: 
     1409            if verbosity > 1: 
     1410                print "Resetting sequences" 
     1411            for line in sequence_sql: 
     1412                cursor.execute(line) 
    14251413    transaction.commit() 
    14261414    transaction.leave_transaction_management() 
  • django/trunk/django/db/backends/ado_mssql/base.py

    r4691 r4937  
    152152                 )  for table in full_table_list] 
    153153 
     154def get_sql_sequence_reset(style, model_list): 
     155    "Returns a list of the SQL statements to reset sequences for the given models." 
     156    # No sequence reset required 
     157    return [] 
     158 
    154159OPERATOR_MAPPING = { 
    155160    'exact': '= %s', 
  • django/trunk/django/db/backends/dummy/base.py

    r4659 r4937  
    4141get_drop_foreignkey_sql = complain 
    4242get_sql_flush = complain 
     43get_sql_sequence_reset = complain 
    4344 
    4445OPERATOR_MAPPING = {} 
  • django/trunk/django/db/backends/mysql/base.py

    r4760 r4937  
    216216        return [] 
    217217 
     218def get_sql_sequence_reset(style, model_list): 
     219    "Returns a list of the SQL statements to reset sequences for the given models." 
     220    # No sequence reset required 
     221    return [] 
     222 
    218223OPERATOR_MAPPING = { 
    219224    'exact': '= %s', 
  • django/trunk/django/db/backends/mysql_old/base.py

    r4767 r4937  
    218218        return [] 
    219219 
     220def get_sql_sequence_reset(style, model_list): 
     221    "Returns a list of the SQL statements to reset sequences for the given models." 
     222    # No sequence reset required 
     223    return [] 
     224 
    220225OPERATOR_MAPPING = { 
    221226    'exact': '= %s', 
  • django/trunk/django/db/backends/oracle/base.py

    r4691 r4937  
    135135             )  for table in tables] 
    136136 
     137def get_sql_sequence_reset(style, model_list): 
     138    "Returns a list of the SQL statements to reset sequences for the given models." 
     139    # No sequence reset required 
     140    return [] 
    137141 
    138142OPERATOR_MAPPING = { 
  • django/trunk/django/db/backends/postgresql/base.py

    r4691 r4937  
    214214        return [] 
    215215 
     216def get_sql_sequence_reset(style, model_list): 
     217    "Returns a list of the SQL statements to reset sequences for the given models." 
     218    from django.db import models 
     219    output = [] 
     220    for model in model_list: 
     221        for f in model._meta.fields: 
     222            if isinstance(f, models.AutoField): 
     223                output.append("%s setval('%s', (%s max(%s) %s %s));" % \ 
     224                    (style.SQL_KEYWORD('SELECT'), 
     225                    style.SQL_FIELD('%s_%s_seq' % (model._meta.db_table, f.column)), 
     226                    style.SQL_KEYWORD('SELECT'), 
     227                    style.SQL_FIELD(quote_name(f.column)), 
     228                    style.SQL_KEYWORD('FROM'), 
     229                    style.SQL_TABLE(quote_name(model._meta.db_table)))) 
     230                break # Only one AutoField is allowed per model, so don't bother continuing. 
     231        for f in model._meta.many_to_many: 
     232            output.append("%s setval('%s', (%s max(%s) %s %s));" % \ 
     233                (style.SQL_KEYWORD('SELECT'), 
     234                style.SQL_FIELD('%s_id_seq' % f.m2m_db_table()), 
     235                style.SQL_KEYWORD('SELECT'), 
     236                style.SQL_FIELD(quote_name('id')), 
     237                style.SQL_KEYWORD('FROM'), 
     238                style.SQL_TABLE(f.m2m_db_table()))) 
     239    return output 
    216240         
    217241# Register these custom typecasts, because Django expects dates/times to be 
  • django/trunk/django/db/backends/postgresql_psycopg2/base.py

    r4691 r4937  
    170170    else: 
    171171        return [] 
     172 
     173def get_sql_sequence_reset(style, model_list): 
     174    "Returns a list of the SQL statements to reset sequences for the given models." 
     175    from django.db import models 
     176    output = [] 
     177    for model in model_list: 
     178        for f in model._meta.fields: 
     179            if isinstance(f, models.AutoField): 
     180                output.append("%s setval('%s', (%s max(%s) %s %s));" % \ 
     181                    (style.SQL_KEYWORD('SELECT'), 
     182                    style.SQL_FIELD('%s_%s_seq' % (model._meta.db_table, f.column)), 
     183                    style.SQL_KEYWORD('SELECT'), 
     184                    style.SQL_FIELD(quote_name(f.column)), 
     185                    style.SQL_KEYWORD('FROM'), 
     186                    style.SQL_TABLE(quote_name(model._meta.db_table)))) 
     187                break # Only one AutoField is allowed per model, so don't bother continuing. 
     188        for f in model._meta.many_to_many: 
     189            output.append("%s setval('%s', (%s max(%s) %s %s));" % \ 
     190                (style.SQL_KEYWORD('SELECT'), 
     191                style.SQL_FIELD('%s_id_seq' % f.m2m_db_table()), 
     192                style.SQL_KEYWORD('SELECT'), 
     193                style.SQL_FIELD(quote_name('id')), 
     194                style.SQL_KEYWORD('FROM'), 
     195                style.SQL_TABLE(f.m2m_db_table()))) 
     196    return output 
    172197         
    173198OPERATOR_MAPPING = { 
  • django/trunk/django/db/backends/sqlite3/base.py

    r4691 r4937  
    171171    return sql 
    172172 
     173def get_sql_sequence_reset(style, model_list): 
     174    "Returns a list of the SQL statements to reset sequences for the given models." 
     175    # No sequence reset required 
     176    return [] 
     177     
    173178def _sqlite_date_trunc(lookup_type, dt): 
    174179    try: 
  • django/trunk/docs/django-admin.txt

    r4818 r4937  
    367367---------------------------------------------- 
    368368 
    369 Prints the SQL statements for resetting PostgreSQL sequences for the given 
     369Prints the SQL statements for resetting sequences for the given 
    370370appnames. 
    371371