Django

Code

Ticket #1935: django.core.management.diff

File django.core.management.diff, 1.5 kB (added by jpellerin@gmail.com, 2 years ago)

patch to split sql initial data into individual sql statements

  • django/core/management.py

    old new  
    328328    app_dir = os.path.normpath(os.path.join(os.path.dirname(models.get_app(model._meta.app_label).__file__), 'sql')) 
    329329    output = [] 
    330330 
    331     # Find custom SQL, if it's available. 
     331    sql_expr = re.compile( 
     332        r"""(           # each statement is... 
     333        (?:             # one or more chunks of ... 
     334            (?:[^;'"]+) # not the end of a statement or start of a quote 
     335          | (?:'[^']+') # something in single quotes 
     336          | (?:"[^"]+") # something in double quotes 
     337        )+)""", re.VERBOSE) 
     338         
     339    # Find custom SQL, if it's available.           
    332340    sql_files = [os.path.join(app_dir, "%s.%s.sql" % (opts.object_name.lower(), settings.DATABASE_ENGINE)), 
    333341                 os.path.join(app_dir, "%s.sql" % opts.object_name.lower())] 
    334342    for sql_file in sql_files: 
    335343        if os.path.exists(sql_file): 
    336344            fp = open(sql_file) 
    337             output.append(fp.read()
     345            sql = fp.read(
    338346            fp.close() 
     347            if ';' in sql: 
     348                # some backends can't execute more than one statement at a time 
     349                statements = sql_expr.findall(sql) 
     350                if statements: 
     351                    output.extend(statements) 
     352                    continue 
     353            output.append(sql) 
    339354 
     355 
    340356    return output 
    341357 
    342358def get_sql_initial_data(app):