Changes between Version 3 and Version 4 of DbDumpScript


Ignore:
Timestamp:
01/19/07 22:52:36 (8 years ago)
Author:
limodou@…
Comment:

update to 1.2

Legend:

Unmodified
Added
Removed
Modified
  • DbDumpScript

    v3 v4  
    22
    33 * Author: limodou <limodou AT gmail.com>
    4  * Version: 1.0
     4 * Current Version: 1.2 2007-01-20
    55
    66== Description ==
     
    9090# be saved as python source code, and you can import it.
    9191#
    92 # Version 1.0 2007-01-18
     92# Version 1.2 2007-01-20
     93# Update 1.0 2007-01-18
    9394# Update 1.1 2007-01-19
    9495#    * if no arguments after db_dump.py, then it'll show help infomation
     96# Update 1.2 2007-01-20
     97#    * change dumpdb to use model info but not cursor.description,
     98#      because some database backend does not support cursor.description
    9599#
    96100
     
    102106    from django.db.models import get_app, get_apps, get_models
    103107    from django.db import connection, transaction
    104     from django.db.models import GenericRel
    105108   
    106109    if options.verbose:
     
    123126            try:
    124127                load_model(cursor, model, format, options)
    125                 opts = model._meta
    126                 for field in opts.many_to_many:
    127                     if not isinstance(field.rel, GenericRel):
    128                         table = field.m2m_db_table()
    129                         load_model(cursor, table, format, options)
     128                for table, fields in get_model_many2many_stru(model):
     129                    load_model(cursor, (table, fields), format, options)
    130130            except Exception, e:
    131131                success = False
     
    133133        if success:
    134134            transaction.commit_unless_managed()
     135        else:
     136            transaction.rollback_unless_managed()
    135137   
    136138    if errornum:
     
    145147    sql = 'INSERT INTO %s (%s) values (%s);'
    146148
    147     if isinstance(model, str):
    148         filename = os.path.join(datadir, model + '.%s' % format)
    149         fields, default = get_table_stru(model), {}
     149    if isinstance(model, (tuple, list)):
     150        filename = os.path.join(datadir, model[0] + '.%s' % format)
     151        fields, default = model[1], {}
    150152    else:
    151153        opts = model._meta
     
    251253    return fields, default
    252254
    253 def get_table_stru(table):
    254     from django.db import connection, get_introspection_module
    255     cursor = connection.cursor()
    256     introspection_module = get_introspection_module()
    257     return [x[0] for x in introspection_module.get_table_description(cursor, table)]
     255def get_model_many2many_stru(model):
     256    from django.db.models import GenericRel
     257   
     258    opts = model._meta
     259    for f in opts.many_to_many:
     260        fields = []
     261        if not isinstance(f.rel, GenericRel):
     262            fields.append('id')
     263            fields.append(f.m2m_column_name())
     264            fields.append(f.m2m_reverse_name())
     265            yield f.m2m_db_table(), fields
     266   
    258267   
    259268def dumpdb(app_labels, format, options):
     
    292301
    293302def dump_model(model):
    294     from django.db import connection
     303    from django.db import connection, backend
    295304
    296305    opts = model._meta
    297306    cursor = connection.cursor()
    298     x = cursor.execute('select * from %s' % opts.db_table)
    299     fields = []
    300     for f in x.description:
    301         fields.append(f[0])
     307    fields, default = get_model_stru(model)
     308    x = cursor.execute('select %s from %s' %
     309        (','.join(map(backend.quote_name, fields)), backend.quote_name(opts.db_table)))
    302310    return {'table':opts.db_table, 'fields':fields, 'records':_pre_data(x.fetchall())}
    303311
     
    312320
    313321def dump_many2many(model):
    314     from django.db.models import GenericRel
    315     from django.db import connection
    316     opts = model._meta
     322    from django.db import connection, backend
     323   
    317324    cursor = connection.cursor()
    318     for field in opts.many_to_many:
    319         if not isinstance(field.rel, GenericRel):
    320             table = field.m2m_db_table()
    321             x = cursor.execute('select * from %s' % table)
    322             fields = [f[0] for f in x.description]
    323             yield {'table':table, 'fields':fields, 'records':_pre_data(x.fetchall())}
     325
     326    for table, fields in get_model_many2many_stru(model):
     327        x = cursor.execute('select %s from %s' %
     328            (','.join(map(backend.quote_name, fields)), backend.quote_name(table)))
     329        yield {'table':table, 'fields':fields, 'records':_pre_data(x.fetchall())}
    324330
    325331def write_result(result, format, options):
Back to Top