|
| 1 | +from .utils import Db |
| 2 | +from .experiment import get_ppls, PipeLine |
| 3 | + |
| 4 | +def corrupt_ppl(pplid: str): |
| 5 | + """ |
| 6 | + Deletes a record from the 'ppls' table in the SQLite database if the provided |
| 7 | + `pplid` exists and the user confirms the deletion. |
| 8 | +
|
| 9 | + Args: |
| 10 | + pplid (str): The ID of the person or record to be deleted. |
| 11 | +
|
| 12 | + Raises: |
| 13 | + FileNotFoundError: If the database directory doesn't exist. |
| 14 | + sqlite3.Error: If there is an issue executing the SQL query. |
| 15 | +
|
| 16 | + This function will: |
| 17 | + - Check if the provided `pplid` exists in the database. |
| 18 | + - Ask the user to confirm the deletion by entering the same `pplid`. |
| 19 | + - If the `pplid` matches, the record is deleted from the database. |
| 20 | + - If the `pplid` does not match, the deletion is aborted. |
| 21 | + - If the `pplid` is not found in the list, a message is displayed. |
| 22 | + """ |
| 23 | + P = PipeLine() |
| 24 | + db_path = f"{P.settings['data_path']}/ppls.db" |
| 25 | + # Use the Db class context manager for automatic connection management |
| 26 | + with Db(db_path=db_path) as db: |
| 27 | + # Ensure the pplid exists in the database before attempting deletion |
| 28 | + if pplid in get_ppls(): |
| 29 | + print('Cross verify before deleting.') |
| 30 | + |
| 31 | + # Single attempt to verify the correct pplid |
| 32 | + pplid1 = input("Enter the same pplid: ") |
| 33 | + if pplid == pplid1: |
| 34 | + try: |
| 35 | + # Perform the deletion (no need for db.commit() since execute handles it) |
| 36 | + db.execute("DELETE FROM ppls WHERE pplid = ?", (pplid,)) |
| 37 | + print(f"Record with pplid {pplid} has been corupted.") |
| 38 | + except Exception as e: |
| 39 | + # In case there's an error, print the error message |
| 40 | + print(f"Error deleting record: {e}") |
| 41 | + else: |
| 42 | + print("pplid does not match. Deletion aborted.") |
| 43 | + else: |
| 44 | + print(f"pplid {pplid} not found in the list of available pplids.") |
0 commit comments