Code

Changes between Version 10 and Version 11 of DbDumpScript


Ignore:
Timestamp:
09/12/07 22:28:00 (7 years ago)
Author:
limodou
Comment:

update version

Legend:

Unmodified
Added
Removed
Modified
  • DbDumpScript

    v10 v11  
    22 
    33 * Author: limodou <limodou AT gmail.com> 
    4  * Current Version: 1.4 2007-01-20 
     4 * Current Version: 2.0 2007-09-11 
    55 
    66== Description == 
     
    9191# be saved as python source code, and you can import it. 
    9292# 
     93# Version 2.0 2007-09-11 
     94#    * refact, and add aoto reset postgres sequence 
     95# 
     96# Version 1.9 2007-09-02 (Merge from RichardH) 
     97#    * Adds try-except to catch the changes in db.backend refactoring in 
     98#      svn version. So db_dump.py can support old version except trunk. 
     99# 
    93100# Version 1.8 2007-08-30 
    94101#    * Fix backend.quote_name to backend.DatabaseOperations().quote_name 
     
    100107# 
    101108# Version 1.6 2007-04-09 
    102 # 
    103 # Update 1.0 2007-01-18 
    104 # 
    105 # Update 1.1 2007-01-19 
    106 #    * if no arguments after db_dump.py, then it'll show help infomation 
    107 # 
    108 # Update 1.2 2007-01-20 
     109#    * Add float support 
     110# 
     111# Version 1.5 2007-02-08 
     112#    * If the filename is not exists, then skip it 
     113# 
     114# Version 1.4 2007-01-21 
     115#    * support mysql 
     116# 
     117# Version 1.3 2007-01-20 
     118#    * change the output format of data file, and improve the process 
     119#      effective of dumpping and loading 
     120# 
     121# Version 1.2 2007-01-20 
    109122#    * change dumpdb to use model info but not cursor.description, 
    110123#      because some database backend does not support cursor.description 
    111124# 
    112 # Update 1.3 2007-01-20 
    113 #    * change the output format of data file, and improve the process 
    114 #      effective of dumpping and loading 
    115 # 
    116 # Update 1.4 2007-01-21 
    117 #    * support mysql 
    118 # 
    119 # Update 1.5 2007-02-08 
    120 #    * If the filename is not exists, then skip it 
    121 # 
    122 # Update 1.6 2007-04-09 
    123 #    * Add float support 
     125# Version 1.1 2007-01-19 
     126#    * if no arguments after db_dump.py, then it'll show help infomation 
     127# 
     128# Version 1.0 2007-01-18 
     129# 
    124130 
    125131import os, sys 
     
    127133import datetime 
    128134import decimal 
     135quote_flag = None 
    129136 
    130137def _get_table_order(app_labels): 
     
    192199 
    193200def loaddb(app_labels, format, options): 
    194     from django.db import connection, transaction, backend 
    195      
     201    from django.db import connection, transaction 
    196202 
    197203    if options.verbose:  
     
    208214        m.reverse() 
    209215        for model in m: 
    210             cursor.execute('DELETE FROM %s WHERE 1=1;' % backend.DatabaseOperations().quote_name(model._meta.db_table)) 
     216            cursor.execute('DELETE FROM %s WHERE 1=1;' % quote_name(model._meta.db_table)) 
    211217            for table, fields in get_model_many2many_stru(model): 
    212                 cursor.execute('DELETE FROM %s WHERE 1=1;' % backend.DatabaseOperations().quote_name(table)) 
     218                cursor.execute('DELETE FROM %s WHERE 1=1;' % quote_name(table)) 
    213219     
    214220    success = True 
     
    216222        try: 
    217223            load_model(cursor, model, format, options) 
     224            setSequence(cursor, model) 
    218225            for table, fields in get_model_many2many_stru(model): 
    219226                load_model(cursor, (table, fields), format, options) 
     227                setSequence(cursor, model) 
    220228        except Exception, e:  
    221229            success = False 
     
    233241     
    234242def load_model(cursor, model, format, options):  
    235     from django.db import backend 
    236  
    237243    datadir, verbose, stdout = options.datadir, options.verbose, options.stdout 
    238244    sql = 'INSERT INTO %s (%s) VALUES (%s);' 
     
    319325                    sql_fields.append(fd) 
    320326                    sql_values.append(v) 
    321             e_sql = sql % (backend.DatabaseOperations().quote_name(table),  
    322                 ','.join(map(backend.DatabaseOperations().quote_name, sql_fields)), ','.join(['%s'] * len(sql_fields))) 
     327            e_sql = sql % (quote_name(table),  
     328                ','.join(map(quote_name, sql_fields)), ','.join(['%s'] * len(sql_fields))) 
    323329            if stdout: 
    324330                print e_sql, sql_values, '\n' 
     
    340346 
    341347def get_model_stru(model): 
    342     from django.db.models.fields import DateField, DateTimeField, TimeField 
     348    from django.db.models.fields import DateField, DateTimeField, TimeField, IntegerField 
    343349     
    344350    fields = [] 
     
    353359                v = datetime.datetime.now() 
    354360                default[f.column] = ('value', f.get_db_prep_save(v)) 
     361        # Need to fix sqlite defaulting None values to '' 
     362        if isinstance(f, IntegerField): 
     363            default[f.column] = ('value', None) 
    355364    return fields, default 
    356365 
     
    407416 
    408417def dump_model(model): 
    409     from django.db import connection, backend 
     418    from django.db import connection 
    410419 
    411420    opts = model._meta 
     
    413422    fields, default = get_model_stru(model) 
    414423    cursor.execute('select %s from %s' %  
    415         (','.join(map(backend.DatabaseOperations().quote_name, fields)), backend.DatabaseOperations().quote_name(opts.db_table))) 
     424        (','.join(map(quote_name, fields)), quote_name(opts.db_table)))         
    416425    return call_cursor(opts.db_table, fields, cursor) 
    417426 
     
    440449 
    441450def dump_many2many(model): 
    442     from django.db import connection, backend 
     451    from django.db import connection 
    443452     
    444453    cursor = connection.cursor() 
     
    446455    for table, fields in get_model_many2many_stru(model): 
    447456        cursor.execute('select %s from %s' %  
    448             (','.join(map(backend.DatabaseOperations().quote_name, fields)), backend.DatabaseOperations().quote_name(table))) 
     457            (','.join(map(quote_name, fields)), quote_name(table))) 
    449458        yield call_cursor(table, fields, cursor) 
    450459 
     
    473482    if not options.stdout: 
    474483        f.close() 
    475      
     484 
     485def quote_name(s): 
     486    from django.db import backend 
     487    if quote_flag == 'old': 
     488        return backend.quote_name(s) 
     489    else: 
     490        return backend.DatabaseOperations().quote_name(s) 
     491         
     492def setSequence(cursor, model): 
     493    from django.conf import settings 
     494 
     495    # postgresql: reset sequence 
     496    if settings.DATABASE_ENGINE in ('postgresql_psycopg2', 'postgresql'): 
     497        cursor.execute('SELECT count(*) + 1 FROM %s;' % quote_name(model._meta.db_table)) 
     498        nb = cursor.fetchall()[0][0] 
     499         
     500        seq = quote_name(model._meta.db_table + '_id_seq') 
     501        cursor.execute('ALTER SEQUENCE %s RESTART WITH %d;' % (seq, nb)) 
     502 
    476503def get_usage(): 
    477504    usage = """ 
     
    517544             
    518545        setup_environ(settings) 
    519          
     546     
     547    global quote_flag 
     548    import django.db 
     549    try: 
     550        # Earlier Django versions. 
     551        django.db.backend.quote_name 
     552        quote_flag = 'old' 
     553    except AttributeError: 
     554        # Django after backend refactoring. 
     555        quote_flag = 'new' 
     556     
    520557    if action == 'dump': 
    521558        dumpdb(apps, 'py', options) 
     
    527564if __name__ == '__main__': 
    528565    execute_from_command_line() 
     566 
    529567}}}