Skip to content
This repository has been archived by the owner on Dec 8, 2024. It is now read-only.

Commit

Permalink
feat: Added function for clearing the db
Browse files Browse the repository at this point in the history
faster than destroying it
  • Loading branch information
MitchellJC committed Oct 19, 2024
1 parent dd6b852 commit 957ceda
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 3 deletions.
11 changes: 11 additions & 0 deletions client/data/routines.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ def init_database() -> None:
connection.commit()


def clear_database() -> None:
"""Clear all rows from the database"""
with _connect() as connection:
cursor = connection.cursor()
cursor.execute("DELETE FROM user;")
cursor.execute("DELETE FROM posture;")
cursor.execute("DELETE FROM SQLITE_SEQUENCE WHERE name='user'")
cursor.execute("DELETE FROM SQLITE_SEQUENCE WHERE name='posture'")
connection.commit()


def destroy_database() -> None:
"""Delete the current database if it exists."""
with resources.as_file(DATABASE_RESOURCE) as database_file:
Expand Down
5 changes: 2 additions & 3 deletions client/drivers/pi_overlord.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
import RPi.GPIO as GPIO
from data.routines import (
init_database,
destroy_database,
reset_registered_face_embeddings,
get_user_postures,
Posture,
clear_database,
)
from drivers.data_structures import ControlledData, HardwareComponents
from drivers.login_system import RESET, handle_authentication
Expand Down Expand Up @@ -484,9 +484,8 @@ def _reset_garden() -> None:

global hardware

destroy_database()
logger.debug("\t<!> initialising database anew...")
init_database()
clear_database()
logger.debug("\t<!> resetting face embeddings...")
reset_registered_face_embeddings()
logger.debug("\t<!> initialising hardware...")
Expand Down
67 changes: 67 additions & 0 deletions demos/fast_data_reset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import time
import logging
import datetime
import pprint

from data.routines import (
destroy_database,
init_database,
create_user,
save_posture,
Posture,
get_users,
get_postures,
clear_database,
)

logger = logging.getLogger(__name__)


def main():
logging.basicConfig(level=logging.INFO)
logger.info("Destroying database")
destroy_database()

logger.info("Initialising database")
start_time = time.time()
init_database()
total_time = time.time() - start_time
logger.info("Done init database in %f seconds", total_time)

logger.info("Filling with some data")

for _ in range(10):
_fill_database()

logger.info(
"Example data\n%s\n%s",
pprint.pformat(get_users(), width=50),
pprint.pformat(get_postures(), width=50),
)

logger.info("Reseting data by dropping rows")
start_time = time.time()
clear_database()
total_time = time.time() - start_time
logger.info("Done clearing data in %f", total_time)

logger.info(
"Example data\n%s\n%s",
pprint.pformat(get_users(), width=50),
pprint.pformat(get_postures(), width=50),
)


def _fill_database() -> None:
create_user()
example_id = create_user()
create_user()

example_posture = Posture(
None, example_id, 0.8, 0.8, datetime.datetime.now(), datetime.datetime.now()
)
save_posture(example_posture)


if __name__ == "__main__":
main()

0 comments on commit 957ceda

Please sign in to comment.