From 4800bf8257c825c85a11802e457d1e325bb0338a Mon Sep 17 00:00:00 2001 From: Sanyam Khurana Date: Wed, 27 Nov 2024 05:22:43 +0530 Subject: [PATCH] try db clone through shutil copy --- src/django_libsql/creation.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/django_libsql/creation.py b/src/django_libsql/creation.py index f2a796c..14b685c 100644 --- a/src/django_libsql/creation.py +++ b/src/django_libsql/creation.py @@ -227,28 +227,27 @@ def _clone_test_db(self, suffix, verbosity, keepdb=False): """ Internal implementation - duplicate the test db tables. """ + import os + import shutil + try: # Get the source database name from settings source_database = self.connection.settings_dict["NAME"] clone_database = f"{source_database}_{suffix}" - # Use the existing database connection to clone - conn = self.connection.get_new_connection(self.connection.connection_params()) - - # Attach the clone database - conn.execute(f"ATTACH DATABASE '{clone_database}' AS clone_db") + # If `keepdb` is False, ensure the cloned database doesn't already exist + if not keepdb and os.path.exists(clone_database): + os.remove(clone_database) - # Copy all tables to the clone database - tables = conn.execute("SELECT name FROM sqlite_master WHERE type='table'").fetchall() - for table in tables: - table_name = table[0] - conn.execute(f"CREATE TABLE clone_db.{table_name} AS SELECT * FROM main.{table_name}") + # Copy the source database file to create the clone + shutil.copy2(source_database, clone_database) - # Detach the clone database - conn.execute("DETACH DATABASE clone_db") + # Update the connection to point to the cloned database + self.connection.settings_dict["NAME"] = clone_database if verbosity >= 1: print(f"Cloned test database '{source_database}' to '{clone_database}'") + except Exception as e: raise NotImplementedError( "Cloning databases is not supported for this backend."