diff --git a/django/db/backends/sqlite3/base.py b/django/db/backends/sqlite3/base.py index 68452e1363..a3e8f0d584 100644 --- a/django/db/backends/sqlite3/base.py +++ b/django/db/backends/sqlite3/base.py @@ -62,7 +62,10 @@ class DatabaseWrapper(local): self.connection.rollback() def close(self): - if self.connection is not None: + from django.conf import settings + # If database is in memory, closing the connection destroys the database. + # To prevent accidental data loss, ignore close requests on an in-memory db. + if self.connection is not None and settings.DATABASE_NAME != ":memory:": self.connection.close() self.connection = None diff --git a/django/test/utils.py b/django/test/utils.py index 1bb448f41b..039a6dd7a2 100644 --- a/django/test/utils.py +++ b/django/test/utils.py @@ -95,10 +95,11 @@ def destroy_test_db(old_database_name, verbosity=1): # connected to it. 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": - connection.close() - TEST_DATABASE_NAME = settings.DATABASE_NAME - settings.DATABASE_NAME = old_database_name cursor = connection.cursor() _set_autocommit(connection) time.sleep(1) # To avoid "database is being accessed by other users" errors.