def destroy_test_db(old_database_name, verbosity=1): # If the database wants to drop the test DB itself, let it creation_module = get_creation_module() if hasattr(creation_module, "destroy_test_db"): creation_module.destroy_test_db(settings, connection, old_database_name, verbosity) return if verbosity >= 1: print "Destroying test database..." connection.close() TEST_DATABASE_NAME = settings.DATABASE_NAME settings.DATABASE_NAME = old_database_name if settings.DATABASE_ENGINE == "sqlite3": if TEST_DATABASE_NAME and TEST_DATABASE_NAME != ":memory:": # Remove the SQLite database file os.remove(TEST_DATABASE_NAME) else: # Remove the test database to clean up after # ourselves. Connect to the previous database (not the test database) # to do so, because it's not allowed to delete a database while being # connected to it. cursor = connection.cursor() _set_autocommit(connection) time.sleep( 1) # To avoid "database is being accessed by other users" errors. cursor.execute("DROP DATABASE %s" % connection.ops.quote_name(TEST_DATABASE_NAME)) connection.close()
def db_type(self): """ Returns the database column data type for this field, taking into account the DATABASE_ENGINE setting. """ # The default implementation of this method looks at the # backend-specific DATA_TYPES dictionary, looking up the field by its # "internal type". # # A Field class can implement the get_internal_type() method to specify # which *preexisting* Django Field class it's most similar to -- i.e., # an XMLField is represented by a TEXT column type, which is the same # as the TextField Django field type, which means XMLField's # get_internal_type() returns 'TextField'. # # But the limitation of the get_internal_type() / DATA_TYPES approach # is that it cannot handle database column types that aren't already # mapped to one of the built-in Django field types. In this case, you # can implement db_type() instead of get_internal_type() to specify # exactly which wacky database column type you want to use. data_types = get_creation_module().DATA_TYPES internal_type = self.get_internal_type() if internal_type not in data_types: return None return data_types[internal_type] % self.__dict__
def destroy_test_db(old_database_name, verbosity=1): # If the database wants to drop the test DB itself, let it creation_module = get_creation_module() if hasattr(creation_module, "destroy_test_db"): creation_module.destroy_test_db(settings, connection, old_database_name, verbosity) return if verbosity >= 1: print "Destroying test database..." connection.close() TEST_DATABASE_NAME = settings.DATABASE_NAME settings.DATABASE_NAME = old_database_name if settings.DATABASE_ENGINE == "sqlite3": if TEST_DATABASE_NAME and TEST_DATABASE_NAME != ":memory:": # Remove the SQLite database file os.remove(TEST_DATABASE_NAME) else: # Remove the test database to clean up after # ourselves. Connect to the previous database (not the test database) # to do so, because it's not allowed to delete a database while being # connected to it. cursor = connection.cursor() _set_autocommit(connection) time.sleep(1) # To avoid "database is being accessed by other users" errors. cursor.execute("DROP DATABASE %s" % connection.ops.quote_name(TEST_DATABASE_NAME)) connection.close()
def db_type(self): """ Returns the database column data type for this field, taking into account the DATABASE_ENGINE setting. """ # The default implementation of this method looks at the # backend-specific DATA_TYPES dictionary, looking up the field by its # "internal type". # # A Field class can implement the get_internal_type() method to specify # which *preexisting* Django Field class it's most similar to -- i.e., # an XMLField is represented by a TEXT column type, which is the same # as the TextField Django field type, which means XMLField's # get_internal_type() returns 'TextField'. # # But the limitation of the get_internal_type() / DATA_TYPES approach # is that it cannot handle database column types that aren't already # mapped to one of the built-in Django field types. In this case, you # can implement db_type() instead of get_internal_type() to specify # exactly which wacky database column type you want to use. data = DictWrapper(self.__dict__, connection.ops.quote_name, "qn_") try: return get_creation_module().DATA_TYPES[self.get_internal_type()] % data except KeyError: return None
def get_sql_evolution_check_for_changed_field_flags(klass, old_table_name, style): ops, introspection = get_operations_and_introspection_classes(style) from django.db import get_creation_module, connection data_types = get_creation_module().DATA_TYPES cursor = connection.cursor() # introspection = ops = get_ops_class(connection) opts = klass._meta output = [] db_table = klass._meta.db_table if old_table_name: db_table = old_table_name for f in opts.fields: existing_fields = introspection.get_columns(cursor,db_table) #existing_relations = introspection.get_relations(cursor,db_table) cf = None # current field, ie what it is before any renames if f.column in existing_fields: cf = f.column f_col_type = f.db_type() elif f.aka and len(set(f.aka).intersection(set(existing_fields)))==1: cf = set(f.aka).intersection(set(existing_fields)).pop() #hack tempname = f.column f.column = cf f_col_type = f.db_type() f.column = tempname else: continue # no idea what column you're talking about - should be handled by get_sql_evolution_check_for_new_fields()) data_type = f.get_internal_type() if data_types.has_key(data_type): #print cf, data_type, f_col_type is_postgresql = settings.DATABASE_ENGINE in ['postgresql', 'postgresql_psycopg2'] column_flags = introspection.get_known_column_flags(cursor, db_table, cf) update_length = compare_field_length(f, column_flags) update_type = get_field_type(column_flags['coltype']) != get_field_type(f_col_type) if DEBUG and update_type: print get_field_type(column_flags['coltype']), '->', get_field_type(f_col_type) update_unique = ( column_flags['unique']!=f.unique and not (is_postgresql and f.primary_key) ) update_null = column_flags['allow_null'] != f.null and not f.primary_key update_primary = column_flags['primary_key']!=f.primary_key update_sequences = False if column_flags.has_key('sequence'): correct_seq_name = klass._meta.db_table+"_"+cf+'_seq' update_sequences = column_flags['sequence'] != correct_seq_name #if DEBUG and update_length: # print f_col_type, column_flags['coltype'], column_flags['max_length'] if update_type or update_null or update_length or update_unique or update_primary or update_sequences: #print "cf, f.default, column_flags['default']", cf, f.default, column_flags['default'], f.default.__class__ f_default = get_field_default(f) updates = { 'update_type': update_type, 'update_length': update_length, 'update_unique': update_unique, 'update_null': update_null, 'update_primary': update_primary, 'update_sequences': update_sequences, } output.extend( ops.get_change_column_def_sql( klass._meta.db_table, cf, f_col_type, f, column_flags, f_default, updates ) ) return output
def db_type(self): from django.db import get_creation_module #@UnresolvedImport from django.db.models.fields.related import ForeignKey data_types = get_creation_module().DATA_TYPES internal_type = self.get_internal_type() if internal_type in ['ForeignKey']: return db_type(self.rel.to._meta.pk) assert internal_type in data_types.keys(), "No such django type. Please send this error to maintainer." return data_types.get(internal_type,'') % self.__dict__
def create_test_db(verbosity=1, autoclobber=False): """ Creates a test database, prompting the user for confirmation if the database already exists. Returns the name of the test database created. """ # If the database backend wants to create the test DB itself, let it creation_module = get_creation_module() if hasattr(creation_module, "create_test_db"): creation_module.create_test_db(settings, connection, verbosity, autoclobber) return if verbosity >= 1: print "Creating test database..." # If we're using SQLite, it's more convenient to test against an # in-memory database. if settings.DATABASE_ENGINE == "sqlite3": TEST_DATABASE_NAME = ":memory:" else: suffix = { 'postgresql': get_postgresql_create_suffix, 'postgresql_psycopg2': get_postgresql_create_suffix, 'mysql': get_mysql_create_suffix, 'mysql_old': get_mysql_create_suffix, }.get(settings.DATABASE_ENGINE, lambda: '')() if settings.TEST_DATABASE_NAME: TEST_DATABASE_NAME = settings.TEST_DATABASE_NAME else: TEST_DATABASE_NAME = TEST_DATABASE_PREFIX + settings.DATABASE_NAME qn = connection.ops.quote_name # Create the test database and connect to it. We need to autocommit # if the database supports it because PostgreSQL doesn't allow # CREATE/DROP DATABASE statements within transactions. cursor = connection.cursor() _set_autocommit(connection) try: cursor.execute("CREATE DATABASE %s %s" % (qn(TEST_DATABASE_NAME), suffix)) except Exception, e: sys.stderr.write("Got an error creating the test database: %s\n" % e) if not autoclobber: confirm = raw_input("Type 'yes' if you would like to try deleting the test database '%s', or 'no' to cancel: " % TEST_DATABASE_NAME) if autoclobber or confirm == 'yes': try: if verbosity >= 1: print "Destroying old test database..." cursor.execute("DROP DATABASE %s" % qn(TEST_DATABASE_NAME)) if verbosity >= 1: print "Creating test database..." cursor.execute("CREATE DATABASE %s %s" % (qn(TEST_DATABASE_NAME), suffix)) except Exception, e: sys.stderr.write("Got an error recreating the test database: %s\n" % e) sys.exit(2) else:
def create_test_db(verbosity=1, autoclobber=False): """ Creates a test database, prompting the user for confirmation if the database already exists. Returns the name of the test database created. """ # If the database backend wants to create the test DB itself, let it creation_module = get_creation_module() if hasattr(creation_module, "create_test_db"): creation_module.create_test_db(settings, connection, verbosity, autoclobber) return if verbosity >= 1: print "Creating test database..." # If we're using SQLite, it's more convenient to test against an # in-memory database. Using the TEST_DATABASE_NAME setting you can still choose # to run on a physical database. if settings.DATABASE_ENGINE == "sqlite3": if settings.TEST_DATABASE_NAME and settings.TEST_DATABASE_NAME != ":memory:": TEST_DATABASE_NAME = settings.TEST_DATABASE_NAME # Erase the old test database if verbosity >= 1: print "Destroying old test database..." if os.access(TEST_DATABASE_NAME, os.F_OK): if not autoclobber: confirm = raw_input( "Type 'yes' if you would like to try deleting the test database '%s', or 'no' to cancel: " % TEST_DATABASE_NAME) if autoclobber or confirm == 'yes': try: if verbosity >= 1: print "Destroying old test database..." os.remove(TEST_DATABASE_NAME) except Exception, e: sys.stderr.write( "Got an error deleting the old test database: %s\n" % e) sys.exit(2) else: print "Tests cancelled." sys.exit(1) if verbosity >= 1: print "Creating test database..." else: TEST_DATABASE_NAME = ":memory:"
def create_test_db(verbosity=1, autoclobber=False): """ Creates a test database, prompting the user for confirmation if the database already exists. Returns the name of the test database created. """ # If the database backend wants to create the test DB itself, let it creation_module = get_creation_module() if hasattr(creation_module, "create_test_db"): creation_module.create_test_db(settings, connection, verbosity, autoclobber) return if verbosity >= 1: print "Creating test database..." # If we're using SQLite, it's more convenient to test against an # in-memory database. Using the TEST_DATABASE_NAME setting you can still choose # to run on a physical database. if settings.DATABASE_ENGINE == "sqlite3": if settings.TEST_DATABASE_NAME and settings.TEST_DATABASE_NAME != ":memory:": TEST_DATABASE_NAME = settings.TEST_DATABASE_NAME # Erase the old test database if verbosity >= 1: print "Destroying old test database..." if os.access(TEST_DATABASE_NAME, os.F_OK): if not autoclobber: confirm = raw_input("Type 'yes' if you would like to try deleting the test database '%s', or 'no' to cancel: " % TEST_DATABASE_NAME) if autoclobber or confirm == 'yes': try: if verbosity >= 1: print "Destroying old test database..." os.remove(TEST_DATABASE_NAME) except Exception, e: sys.stderr.write("Got an error deleting the old test database: %s\n" % e) sys.exit(2) else: print "Tests cancelled." sys.exit(1) if verbosity >= 1: print "Creating test database..." else: TEST_DATABASE_NAME = ":memory:"
def get_sql_evolution_check_for_new_fields(model, old_table_name, style): "checks for model fields that are not in the existing data structure" from django.db import get_creation_module, get_introspection_module, connection ops, introspection = get_operations_and_introspection_classes(style) data_types = get_creation_module().DATA_TYPES cursor = connection.cursor() # introspection = ops = get_ops_class(connection) opts = model._meta output = [] db_table = model._meta.db_table if old_table_name: db_table = old_table_name existing_fields = introspection.get_columns(cursor,db_table) for f in opts.fields: if f.column not in existing_fields and (not f.aka or f.aka not in existing_fields and len(set(f.aka) & set(existing_fields))==0): col_type = f.db_type() if col_type is not None: output.extend( ops.get_add_column_sql( model._meta.db_table, f.column, style.SQL_COLTYPE(col_type), f.null, f.unique, f.primary_key, f.default ) ) output.extend( get_sql_indexes_for_field(model, f, style) ) return output
def create_test_db(verbosity=1, autoclobber=False): """ Creates a test database, prompting the user for confirmation if the database already exists. Returns the name of the test database created. """ # If the database backend wants to create the test DB itself, let it creation_module = get_creation_module() if hasattr(creation_module, "create_test_db"): creation_module.create_test_db(settings, connection, verbosity, autoclobber) return if verbosity >= 1: print("Creating test database...") # If we're using SQLite, it's more convenient to test against an # in-memory database. Using the TEST_DATABASE_NAME setting you can still choose # to run on a physical database. if settings.DATABASE_ENGINE == "sqlite3": if settings.TEST_DATABASE_NAME and settings.TEST_DATABASE_NAME != ":memory:": TEST_DATABASE_NAME = settings.TEST_DATABASE_NAME # Erase the old test database if verbosity >= 1: print("Destroying old test database...") if os.access(TEST_DATABASE_NAME, os.F_OK): if not autoclobber: confirm = input("Type 'yes' if you would like to try deleting the test database '%s', or 'no' to cancel: " % TEST_DATABASE_NAME) if autoclobber or confirm == 'yes': try: if verbosity >= 1: print("Destroying old test database...") os.remove(TEST_DATABASE_NAME) except Exception as e: sys.stderr.write("Got an error deleting the old test database: %s\n" % e) sys.exit(2) else: print("Tests cancelled.") sys.exit(1) if verbosity >= 1: print("Creating test database...") else: TEST_DATABASE_NAME = ":memory:" else: suffix = { 'postgresql': get_postgresql_create_suffix, 'postgresql_psycopg2': get_postgresql_create_suffix, 'mysql': get_mysql_create_suffix, }.get(settings.DATABASE_ENGINE, lambda: '')() if settings.TEST_DATABASE_NAME: TEST_DATABASE_NAME = settings.TEST_DATABASE_NAME else: TEST_DATABASE_NAME = TEST_DATABASE_PREFIX + settings.DATABASE_NAME qn = connection.ops.quote_name # Create the test database and connect to it. We need to autocommit # if the database supports it because PostgreSQL doesn't allow # CREATE/DROP DATABASE statements within transactions. cursor = connection.cursor() _set_autocommit(connection) try: cursor.execute("CREATE DATABASE %s %s" % (qn(TEST_DATABASE_NAME), suffix)) except Exception as e: sys.stderr.write("Got an error creating the test database: %s\n" % e) if not autoclobber: confirm = input("Type 'yes' if you would like to try deleting the test database '%s', or 'no' to cancel: " % TEST_DATABASE_NAME) if autoclobber or confirm == 'yes': try: if verbosity >= 1: print("Destroying old test database...") cursor.execute("DROP DATABASE %s" % qn(TEST_DATABASE_NAME)) if verbosity >= 1: print("Creating test database...") cursor.execute("CREATE DATABASE %s %s" % (qn(TEST_DATABASE_NAME), suffix)) except Exception as e: sys.stderr.write("Got an error recreating the test database: %s\n" % e) sys.exit(2) else: print("Tests cancelled.") sys.exit(1) connection.close() settings.DATABASE_NAME = TEST_DATABASE_NAME call_command('syncdb', verbosity=verbosity, interactive=False) if settings.CACHE_BACKEND.startswith('db://'): cache_name = settings.CACHE_BACKEND[len('db://'):] call_command('createcachetable', cache_name) # Get a cursor (even though we don't need one yet). This has # the side effect of initializing the test database. cursor = connection.cursor() return TEST_DATABASE_NAME
def create_test_db(verbosity=1, autoclobber=False): """ Creates a test database, prompting the user for confirmation if the database already exists. Returns the name of the test database created. """ # If the database backend wants to create the test DB itself, let it creation_module = get_creation_module() if hasattr(creation_module, "create_test_db"): creation_module.create_test_db(settings, connection, verbosity, autoclobber) return if verbosity >= 1: print("Creating test database...") # If we're using SQLite, it's more convenient to test against an # in-memory database. Using the TEST_DATABASE_NAME setting you can still choose # to run on a physical database. if settings.DATABASE_ENGINE == "sqlite3": if settings.TEST_DATABASE_NAME and settings.TEST_DATABASE_NAME != ":memory:": TEST_DATABASE_NAME = settings.TEST_DATABASE_NAME # Erase the old test database if verbosity >= 1: print("Destroying old test database...") if os.access(TEST_DATABASE_NAME, os.F_OK): if not autoclobber: confirm = input( "Type 'yes' if you would like to try deleting the test database '%s', or 'no' to cancel: " % TEST_DATABASE_NAME) if autoclobber or confirm == 'yes': try: if verbosity >= 1: print("Destroying old test database...") os.remove(TEST_DATABASE_NAME) except Exception as e: sys.stderr.write( "Got an error deleting the old test database: %s\n" % e) sys.exit(2) else: print("Tests cancelled.") sys.exit(1) if verbosity >= 1: print("Creating test database...") else: TEST_DATABASE_NAME = ":memory:" else: suffix = { 'postgresql': get_postgresql_create_suffix, 'postgresql_psycopg2': get_postgresql_create_suffix, 'mysql': get_mysql_create_suffix, }.get(settings.DATABASE_ENGINE, lambda: '')() if settings.TEST_DATABASE_NAME: TEST_DATABASE_NAME = settings.TEST_DATABASE_NAME else: TEST_DATABASE_NAME = TEST_DATABASE_PREFIX + settings.DATABASE_NAME qn = connection.ops.quote_name # Create the test database and connect to it. We need to autocommit # if the database supports it because PostgreSQL doesn't allow # CREATE/DROP DATABASE statements within transactions. cursor = connection.cursor() _set_autocommit(connection) try: cursor.execute("CREATE DATABASE %s %s" % (qn(TEST_DATABASE_NAME), suffix)) except Exception as e: sys.stderr.write("Got an error creating the test database: %s\n" % e) if not autoclobber: confirm = input( "Type 'yes' if you would like to try deleting the test database '%s', or 'no' to cancel: " % TEST_DATABASE_NAME) if autoclobber or confirm == 'yes': try: if verbosity >= 1: print("Destroying old test database...") cursor.execute("DROP DATABASE %s" % qn(TEST_DATABASE_NAME)) if verbosity >= 1: print("Creating test database...") cursor.execute("CREATE DATABASE %s %s" % (qn(TEST_DATABASE_NAME), suffix)) except Exception as e: sys.stderr.write( "Got an error recreating the test database: %s\n" % e) sys.exit(2) else: print("Tests cancelled.") sys.exit(1) connection.close() settings.DATABASE_NAME = TEST_DATABASE_NAME call_command('syncdb', verbosity=verbosity, interactive=False) if settings.CACHE_BACKEND.startswith('db://'): cache_name = settings.CACHE_BACKEND[len('db://'):] call_command('createcachetable', cache_name) # Get a cursor (even though we don't need one yet). This has # the side effect of initializing the test database. cursor = connection.cursor() return TEST_DATABASE_NAME