-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathcleanup.py
28 lines (25 loc) · 1013 Bytes
/
cleanup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
"""Purge all data from database."""
from sqlalchemy import text
from sqlalchemy.exc import IntegrityError, SQLAlchemyError
from database import session
from logger import LOGGER
def cleanup_data():
"""Purge test database of all data."""
try:
session.execute(text("SET FOREIGN_KEY_CHECKS=0;"))
session.commit()
session.execute(text("TRUNCATE TABLE comment;"))
session.commit()
session.execute(text("TRUNCATE TABLE post;"))
session.commit()
session.execute(text("TRUNCATE TABLE user;"))
session.commit()
session.execute(text("SET FOREIGN_KEY_CHECKS=1;"))
session.commit()
LOGGER.success("Successfully reset all data.")
except IntegrityError as e:
LOGGER.error(f"Integrity error when resetting data: {e}")
except SQLAlchemyError as e:
LOGGER.error(f"SQLAlchemyError error when resetting data: {e}")
except Exception as e:
LOGGER.error(f"Unexpected error when resetting data: {e}")