@@ -220,7 +220,10 @@ def get_DB_debug() -> bool:
220220 return bool (qcodes .config ["core" ]["db_debug" ])
221221
222222
223- def initialise_database (journal_mode : JournalMode | None = "WAL" ) -> None :
223+ def initialise_database (
224+ journal_mode : JournalMode | None = "WAL" ,
225+ db_path : str | Path | None = None ,
226+ ) -> None :
224227 """
225228 Initialise a database in the location specified by the config object
226229 and set ``atomic commit and rollback mode`` of the db. The db is created
@@ -232,45 +235,28 @@ def initialise_database(journal_mode: JournalMode | None = "WAL") -> None:
232235 journal_mode: Which `journal_mode` should be used for atomic commit and rollback.
233236 Options are DELETE, TRUNCATE, PERSIST, MEMORY, WAL and OFF. If set to None
234237 no changes are made.
238+ db_path: Path to database file. If None, the path specified in the config object is used.
235239
236240 """
237241 # calling connect performs all the needed actions to create and upgrade
238242 # the db to the latest version.
239- conn = connect (get_DB_location (), get_DB_debug ())
243+ if db_path is None :
244+ db_path = get_DB_location ()
245+ conn = connect (db_path , get_DB_debug ())
240246 reset_default_experiment_id (conn )
241247 if journal_mode is not None :
242248 set_journal_mode (conn , journal_mode )
243249 conn .close ()
244250 del conn
245251
246252
247- def set_journal_mode (conn : AtomicConnection , journal_mode : JournalMode ) -> None :
248- """
249- Set the ``atomic commit and rollback mode`` of the sqlite database.
250- See https://www.sqlite.org/pragma.html#pragma_journal_mode for details.
251-
252- Args:
253- conn: Connection to the database.
254- journal_mode: Which `journal_mode` should be used for atomic commit and rollback.
255- Options are DELETE, TRUNCATE, PERSIST, MEMORY, WAL and OFF.
256-
257- """
258- valid_journal_modes = ["DELETE" , "TRUNCATE" , "PERSIST" , "MEMORY" , "WAL" , "OFF" ]
259- if journal_mode not in valid_journal_modes :
260- raise RuntimeError (
261- f"Invalid journal_mode { journal_mode } Valid modes are { valid_journal_modes } "
262- )
263- query = f"PRAGMA journal_mode={ journal_mode } ;"
264- cursor = conn .cursor ()
265- cursor .execute (query )
266-
267-
268253def initialise_or_create_database_at (
269254 db_file_with_abs_path : str | Path , journal_mode : JournalMode | None = "WAL"
270255) -> None :
271256 """
272- This function sets up QCoDeS to refer to the given database file. If the
273- database file does not exist, it will be initiated.
257+ Initialises or creates a database at the specified location and configures QCoDeS to use
258+ this as the default database for the duration of the session.
259+
274260
275261 Args:
276262 db_file_with_abs_path: Database file name with absolute path, for example
@@ -285,23 +271,52 @@ def initialise_or_create_database_at(
285271
286272
287273@contextmanager
288- def initialised_database_at (db_file_with_abs_path : str | Path ) -> Iterator [None ]:
274+ def initialised_database_at (
275+ db_file_with_abs_path : str | Path , * , journal_mode : JournalMode | None = "WAL"
276+ ) -> Iterator [None ]:
289277 """
290- Initializes or creates a database and restores the 'db_location' afterwards.
278+ Initialises or creates a database at the specified location, configures QCoDeS to use this as the
279+ default database for the duration of the context, and restores the 'db_location' afterwards.
291280
292281 Args:
293282 db_file_with_abs_path: Database file name with absolute path, for example
294283 ``C:\\ mydata\\ majorana_experiments.db``
284+ journal_mode: Which `journal_mode` should be used for atomic commit and rollback.
285+ Options are DELETE, TRUNCATE, PERSIST, MEMORY, WAL and OFF. If set to None
286+ no changes are made.
295287
296288 """
297289 db_location = qcodes .config ["core" ]["db_location" ]
298290 try :
299- initialise_or_create_database_at (db_file_with_abs_path )
291+ initialise_or_create_database_at (
292+ db_file_with_abs_path , journal_mode = journal_mode
293+ )
300294 yield
301295 finally :
302296 qcodes .config ["core" ]["db_location" ] = db_location
303297
304298
299+ def set_journal_mode (conn : AtomicConnection , journal_mode : JournalMode ) -> None :
300+ """
301+ Set the ``atomic commit and rollback mode`` of the sqlite database.
302+ See https://www.sqlite.org/pragma.html#pragma_journal_mode for details.
303+
304+ Args:
305+ conn: Connection to the database.
306+ journal_mode: Which `journal_mode` should be used for atomic commit and rollback.
307+ Options are DELETE, TRUNCATE, PERSIST, MEMORY, WAL and OFF.
308+
309+ """
310+ valid_journal_modes = ["DELETE" , "TRUNCATE" , "PERSIST" , "MEMORY" , "WAL" , "OFF" ]
311+ if journal_mode not in valid_journal_modes :
312+ raise RuntimeError (
313+ f"Invalid journal_mode { journal_mode } Valid modes are { valid_journal_modes } "
314+ )
315+ query = f"PRAGMA journal_mode={ journal_mode } ;"
316+ cursor = conn .cursor ()
317+ cursor .execute (query )
318+
319+
305320def conn_from_dbpath_or_conn (
306321 conn : AtomicConnection | None ,
307322 path_to_db : str | Path | None ,
0 commit comments