def get_sql_fingerprint(app, style, notify=True): "Returns the fingerprint of the current schema, used in schema evolution." from django.db import connection # This should work even if a connecton isn't available try: cursor = connection.cursor() except: cursor = None ops, introspection = get_operations_and_introspection_classes(style) app_name = app.__name__.split('.')[-2] schema_fingerprint = introspection.get_schema_fingerprint(cursor, app_name, get_installed_tables(app)) try: fingerprints, evolutions = get_fingerprints_evolutions_from_app(app, style, notify) # is this a schema we recognize? schema_recognized = schema_fingerprint in fingerprints if schema_recognized: if notify: sys.stderr.write(style.NOTICE("deseb: Current schema fingerprint for '%s' is '%s' (recognized)\n" % (app_name, schema_fingerprint))) else: if notify: sys.stderr.write(style.NOTICE("deseb: Current schema fingerprint for '%s' is '%s' (unrecognized)\n" % (app_name, schema_fingerprint))) except: traceback.print_exc() if notify: sys.stderr.write(style.NOTICE("deseb: Current schema fingerprint for '%s' is '%s' (no schema_evolution module found)\n" % (app_name, schema_fingerprint))) return
def get_sql_evolution_detailed(app, style, notify): "Returns SQL to update an existing schema to match the existing models." from django.db import connection cursor = connection.cursor() #show_evolution_plan(cursor, app, style) ops, introspection = get_operations_and_introspection_classes(style) app_name = app.__name__.split('.')[-2] final_output = [] db_schema, model_schema = get_schemas(cursor, app, style) cursor, app_name, get_installed_tables(app) schema_fingerprint = introspection.get_schema_fingerprint(model_schema) schema_recognized, all_upgrade_paths, available_upgrades, best_upgrade \ = get_managed_evolution_options(app, schema_fingerprint, style, notify) if schema_recognized: if notify: sys.stderr.write(style.NOTICE("deseb: Current schema fingerprint for '%s' is '%s' (recognized)\n" % (app_name, schema_fingerprint))) final_output.extend(best_upgrade[2]) return schema_fingerprint, False, final_output else: if notify: sys.stderr.write(style.NOTICE("deseb: Current schema fingerprint for '%s' is '%s' (unrecognized)\n" % (app_name, schema_fingerprint))) final_output.extend(get_introspected_evolution_options(app, style)) return schema_fingerprint, True, final_output
def drop_all_tables(): from django.core.management.color import no_style from django.db import connection from deseb.meta import DBTable from deseb.common import get_operations_and_introspection_classes _ops, introspection = get_operations_and_introspection_classes(no_style()) all_tables = introspection.get_table_names(connection.cursor()) for table in all_tables: run_sql(_ops.get_drop_table_sql(DBTable(table)).actions)
def get_schemas(cursor, app, style, db_schema=None, model_schema=None): _ops, introspection = get_operations_and_introspection_classes(style) app_name = app.__name__.split('.')[-2] if model_schema is None: model_schema = build_model_schema(app) add_tables = get_installed_tables(app, model_schema) if db_schema is None: db_schema = introspection.get_schema(cursor, app_name, add_tables) db_schema.name = 'Current DB' return db_schema, model_schema
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
def __init__(self, table, style): super(IndexActions, self).__init__(table, style) self.ops, _introspection = get_operations_and_introspection_classes(style)
def __init__(self, table, style): super(FieldActions, self).__init__(table, style) self.ops, _introspection = get_operations_and_introspection_classes(style) self.side_effects = []
def __init__(self, schema, style): super(TableActions, self).__init__(schema, style) self.ops, _introspection = get_operations_and_introspection_classes(style)