Ejemplo n.º 1
0
def evolvedb(app, interactive=True, do_save=False, verbose=False, managed_upgrade_only=False):
    from django.db import connection
    cursor = connection.cursor()

    style = color.no_style()
    ops, introspection = get_operations_and_introspection_classes(style)
    app_name = app.__name__.split('.')[-2]
    
    seen_schema_fingerprints = set()
    
    fingerprints, evolutions = get_fingerprints_evolutions_from_app(app, style, verbose)
    if fingerprints and evolutions:
        if verbose:
            print 'deseb: %s.schema_evolution module found (%i fingerprints, %i evolutions)' % \
                    (app_name, len(fingerprints), len(evolutions))

    while True:
        commands = []
        commands_color = []
    
        schema_fingerprint = introspection.get_schema_fingerprint(cursor, app_name, get_installed_tables(app))
        schema_recognized, all_upgrade_paths, available_upgrades, best_upgrade = \
                    get_managed_evolution_options(app, schema_fingerprint, style, verbose)
        if fingerprints and evolutions:
            if schema_recognized:
                if verbose or interactive: 
                    print "deseb: fingerprint for '%s' is '%s' (recognized)" % (app_name, schema_fingerprint)
            else:
                if verbose or interactive: 
                    print "deseb: fingerprint for '%s' is '%s' (unrecognized)" % (app_name, schema_fingerprint)
        managed_upgrade = schema_recognized and available_upgrades and best_upgrade and best_upgrade[3]>0
        if managed_upgrade:
            if verbose or interactive: 
                print "\t and a managed schema upgrade to '%s' is available:" % best_upgrade[1], best_upgrade[3]
            commands_color = commands = best_upgrade[2]
        elif not managed_upgrade_only:
            commands = get_introspected_evolution_options(app, style)
            commands_color = get_introspected_evolution_options(app, color.color_style())
            if interactive:
                if commands:
                    print '%s: the following schema upgrade is available:' % app_name
    #            else:
    #                print '%s: schema is up to date' % app_name
            
        if commands:
            if interactive or DEBUG:
                for cmd in commands_color:
                    print cmd
        else:
                break
    
        if interactive:
            confirm = raw_input("do you want to run the preceeding commands?\ntype 'yes' to continue, or 'no' to cancel: ")
        else:
            confirm = 'yes'
        
        if confirm == 'yes':
            connection._commit() # clean previous commands run state
            for cmd in commands:
                if cmd[:3] != '-- ':
                    cursor.execute(cmd)
            connection._commit() # commit changes
            if interactive: print 'schema upgrade executed'
            new_schema_fingerprint = introspection.get_schema_fingerprint(cursor, app_name, get_installed_tables(app))
            
            if schema_fingerprint==new_schema_fingerprint:
                print "schema fingerprint was unchanged - this really shouldn't happen"
            else:
                if commands and not managed_upgrade and (schema_fingerprint,new_schema_fingerprint) not in all_upgrade_paths:
                    if interactive and do_save:
                        confirm = raw_input("do you want to save these commands in %s.schema_evolution?\n"
                                            "type 'yes' to continue, or 'no' to cancel: " % app_name)
                    else:
                        confirm = 'yes'
                    if do_save and confirm == 'yes':
                        save_managed_evolution(app, commands, schema_fingerprint, new_schema_fingerprint)
            
            if not managed_upgrade: break
        else:
            if interactive: print 'schema not saved'
            break
                
        seen_schema_fingerprints.add(schema_fingerprint)
        schema_fingerprint = new_schema_fingerprint
            
        if managed_upgrade:
            if schema_fingerprint==best_upgrade[1]:
                if verbose: 
                    print '\tfingerprint verification successful'
            else:
                if verbose: 
                    print "\tfingerprint verification failed (is '%s'; was expecting '%s')" % \
                            (schema_fingerprint, best_upgrade[1])
                break
        
        print
    
        if schema_fingerprint in seen_schema_fingerprints:
            break
Ejemplo n.º 2
0
def evolvediff(app, interactive=True, do_save=False, verbose=False, managed_upgrade_only=False):
    from django.db import connection
    cursor = connection.cursor()
    style = color.no_style()
    return show_evolution_plan(cursor, app, style)