Changes between Version 5 and Version 6 of DbDumpScript


Ignore:
Timestamp:
01/21/2007 12:27:18 AM (8 years ago)
Author:
limodou@…
Comment:

update to 1.4

Legend:

Unmodified
Added
Removed
Modified
  • DbDumpScript

    v5 v6  
    22
    33 * Author: limodou <limodou AT gmail.com>
    4  * Current Version: 1.3 2007-01-20
     4 * Current Version: 1.4 2007-01-20
    55
    66== Description ==
     
    9090# be saved as python source code, and you can import it.
    9191#
    92 # Version 1.3 2007-01-20
     92# Version 1.4 2007-01-20
    9393#
    9494# Update 1.0 2007-01-18
     95#
    9596# Update 1.1 2007-01-19
    9697#    * if no arguments after db_dump.py, then it'll show help infomation
     98#
    9799# Update 1.2 2007-01-20
    98100#    * change dumpdb to use model info but not cursor.description,
     
    103105#      effective of dumpping and loading
    104106#
     107# Update 1.4 2007-01-21
     108#    * support mysql
     109#
    105110
    106111import os, sys
     
    108113import datetime
    109114
     115def _get_table_order(app_labels):
     116    from django.db.models import get_app, get_apps, get_models
     117    from django.db.models import ForeignKey, OneToOneField
     118
     119    if not app_labels:
     120        app_list = get_apps()
     121    else:
     122        app_list = [get_app(app_label) for app_label in app_labels]
     123   
     124    models = {}
     125    for app in app_list:
     126        for model in get_models(app):
     127            models[model._meta.db_table] = model
     128    s = []     
     129    rules = []
     130   
     131    def order(s, rule):
     132        a, b = rule
     133        try:
     134            i = s.index(a)
     135            try:
     136                j = s.index(b)
     137                if j<i:
     138                    del s[i]
     139                    s.insert(j, a)
     140            except:
     141                s.append(b)
     142        except:
     143            s.append(a)
     144            try:
     145                j = s.index(b)
     146                del s[j]
     147                s.append(b)
     148            except:
     149                s.append(b)
     150           
     151    for i, table in enumerate(models.keys()[:]):
     152        for field in models[table]._meta.fields:
     153            if isinstance(field, (ForeignKey, OneToOneField)):
     154                tname = field.rel.to._meta.db_table
     155                if not models.has_key(tname) or tname == table:
     156                    continue
     157                rules.append((tname, table))
     158                order(s, (tname, table))
     159
     160    n = []
     161    for k, v in models.items():
     162        if s.count(k) == 0:
     163            n.append(k)
     164           
     165    return [models[k] for k in s+n]
     166
     167def _find_key(d, key):
     168    if not d:
     169        return None
     170    for k, v in d.items()[:]:
     171        if k == key:
     172            return d
     173        else:
     174            result = _find_key(v, key)
     175            if result is not None:
     176                return result
     177
    110178def loaddb(app_labels, format, options):
    111     from django.db.models import get_app, get_apps, get_models
    112     from django.db import connection, transaction
    113    
     179    from django.db import connection, transaction, backend
     180   
     181
    114182    if options.verbose:
    115183        print "Begin to load data for %s format...\n" % format
    116184   
    117     if len(app_labels) == 0:
    118         app_list = get_apps()
    119     else:
    120         app_list = [get_app(app_label) for app_label in app_labels]
    121    
     185    models = _get_table_order(app_labels)
     186
    122187    cursor = connection.cursor()
    123188
    124189    errornum = 0
    125     for app in app_list:
    126         if options.verbose:
    127             print "..Checking '%s' app for [%s] format...\n" % (os.path.basename(os.path.dirname(app.__file__)), format)
     190
     191    if not options.remain and not options.stdout:
     192        m = models[:]
     193        m.reverse()
     194        for model in m:
     195            cursor.execute('DELETE FROM %s WHERE 1=1;' % backend.quote_name(model._meta.db_table))
     196            for table, fields in get_model_many2many_stru(model):
     197                cursor.execute('DELETE FROM %s WHERE 1=1;' % backend.quote_name(table))
     198   
     199    success = True
     200    for model in models:
     201        try:
     202            load_model(cursor, model, format, options)
     203            for table, fields in get_model_many2many_stru(model):
     204                load_model(cursor, (table, fields), format, options)
     205        except Exception, e:
     206            success = False
     207            errornum += 1
    128208       
    129         success = True
    130         for model in get_models(app):
    131             try:
    132                 load_model(cursor, model, format, options)
    133                 for table, fields in get_model_many2many_stru(model):
    134                     load_model(cursor, (table, fields), format, options)
    135             except Exception, e:
    136                 success = False
    137                 errornum += 1
    138         if success:
    139             transaction.commit_unless_managed()
    140         else:
    141             transaction.rollback_unless_managed()
     209    if success:
     210        transaction.commit_unless_managed()
     211    else:
     212        transaction.rollback_unless_managed()
    142213   
    143214    if errornum:
     
    149220    from django.db import backend
    150221
    151     datadir, verbose, stdout, remain = options.datadir, options.verbose, options.stdout, options.remain
     222    datadir, verbose, stdout = options.datadir, options.verbose, options.stdout
    152223    sql = 'INSERT INTO %s (%s) VALUES (%s);'
    153224
     
    189260        table = objs['table']
    190261       
    191         if not remain and not stdout:
    192             cursor.execute('DELETE FROM %s WHERE 1=1;' % backend.quote_name(table))
    193            
    194262        default.update(objs.get('default', {}))
    195263        count = 0
     
    253321
    254322def get_model_stru(model):
    255     from django.db.models.fields import AutoField, DateField, DateTimeField, TimeField
     323    from django.db.models.fields import DateField, DateTimeField, TimeField
    256324   
    257325    fields = []
    258326    default = {}
    259327    for f in model._meta.fields:
    260         if not isinstance(f, AutoField):
    261             fields.append(f.column)
    262             v = f.get_default()
    263             if v is not None:
    264                 default[f.column] = ('value', v)
    265             if isinstance(f, (DateTimeField, DateField, TimeField)):
    266                 if f.auto_now or f.auto_now_add:
    267                     v = datetime.datetime.now()
    268                     default[f.column] = ('value', f.get_db_prep_save(v))
     328        fields.append(f.column)
     329        v = f.get_default()
     330        if v is not None:
     331            default[f.column] = ('value', v)
     332        if isinstance(f, (DateTimeField, DateField, TimeField)):
     333            if f.auto_now or f.auto_now_add:
     334                v = datetime.datetime.now()
     335                default[f.column] = ('value', f.get_db_prep_save(v))
    269336    return fields, default
    270337
     
    323390    cursor = connection.cursor()
    324391    fields, default = get_model_stru(model)
    325     x = cursor.execute('select %s from %s' %
     392    cursor.execute('select %s from %s' %
    326393        (','.join(map(backend.quote_name, fields)), backend.quote_name(opts.db_table)))
    327     return call_cursor(opts.db_table, fields, x)
     394    return call_cursor(opts.db_table, fields, cursor)
    328395
    329396def call_cursor(table, fields, cursor):
     
    339406
    340407def _pre_data(row):
     408    row = list(row)
    341409    for i, fd in enumerate(row):
    342410        if isinstance(fd, datetime.datetime):
     
    352420
    353421    for table, fields in get_model_many2many_stru(model):
    354         x = cursor.execute('select %s from %s' %
     422        cursor.execute('select %s from %s' %
    355423            (','.join(map(backend.quote_name, fields)), backend.quote_name(table)))
    356         yield call_cursor(table, fields, x)
     424        yield call_cursor(table, fields, cursor)
    357425
    358426def write_result(result, format, options):
Back to Top