diff --git a/datalite/mass_actions.py b/datalite/mass_actions.py index d3f1d6e..09afe88 100644 --- a/datalite/mass_actions.py +++ b/datalite/mass_actions.py @@ -28,11 +28,13 @@ def is_homogeneous(objects: Union[List[T], Tuple[T]]) -> bool: return all([isinstance(obj, class_) for obj in objects]) -def create_many_entries(objects: Union[List[T], Tuple[T]]) -> None: +def create_many_entries(objects: Union[List[T], Tuple[T]], protect_memory: bool = True) -> None: """ Insert many records corresponding to objects in a tuple or a list. + :param protect_memory: If False, memory protections are turned off, + makes it faster. :param objects: A tuple or a list of objects decorated with datalite. :return: None. @@ -50,11 +52,14 @@ def create_many_entries(objects: Union[List[T], Tuple[T]]) -> None: with sql.connect(getattr(objects[0], "db_path")) as con: cur: sql.Cursor = con.cursor() try: + if not protect_memory: + cur.execute("PRAGMA synchronous = OFF") + cur.execute("PRAGMA journal_mode = MEMORY") cur.execute(f"SELECT obj_id FROM {table_name} ORDER BY obj_id DESC LIMIT 1") index_tuple = cur.fetchone() if index_tuple: first_index = index_tuple[0] - cur.executescript('\n'.join(sql_queries)) + cur.executescript("BEGIN TRANSACTION;\n" + '\n'.join(sql_queries) + '\nEND TRANSACTION;') except sql.IntegrityError: raise ConstraintFailedError con.commit() diff --git a/test/main_tests.py b/test/main_tests.py index 5ab28e2..9a1daa8 100644 --- a/test/main_tests.py +++ b/test/main_tests.py @@ -200,7 +200,7 @@ class DatabaseMassInsert(unittest.TestCase): self.objs = [MassCommit('cat') for _ in range(30)] def testMassCreate(self): - create_many_entries(self.objs) + create_many_entries(self.objs, protect_memory=False) def tearDown(self) -> None: [obj.remove_entry() for obj in self.objs]