mirror of
https://github.com/chatmail/relay.git
synced 2026-05-19 12:28:06 +00:00
use with db.write_transaction()
This commit is contained in:
@@ -17,22 +17,18 @@ from .database import Database
|
|||||||
|
|
||||||
|
|
||||||
def remove_users(db: Database, cutoff_date: int):
|
def remove_users(db: Database, cutoff_date: int):
|
||||||
db.connect()
|
with db.write_transaction() as conn:
|
||||||
try:
|
|
||||||
delete_query = "DELETE FROM users WHERE last_login <?"
|
delete_query = "DELETE FROM users WHERE last_login <?"
|
||||||
db.execute_query(delete_query, (cutoff_date))
|
conn.execute(delete_query, (cutoff_date))
|
||||||
db.commit_changes()
|
|
||||||
finally:
|
|
||||||
db.close()
|
|
||||||
|
|
||||||
|
|
||||||
def remove_user_data(db: Database, cutoff_date: int, dir: Path):
|
def remove_user_data(db: Database, cutoff_date: int, dir: Path):
|
||||||
"""Collects all users where last_login < cutoff_date and deletes corresponding directories."""
|
"""Collects all users where last_login < cutoff_date and deletes corresponding directories."""
|
||||||
db.connect()
|
db.connect()
|
||||||
|
|
||||||
try:
|
with db.write_transaction() as conn:
|
||||||
select_query = "SELECT user FROM users WHERE last_login <?"
|
select_query = "SELECT user FROM users WHERE last_login <?"
|
||||||
cursor = db.execute_query(select_query, (cutoff_date,))
|
cursor = conn.execute(select_query, (cutoff_date,))
|
||||||
usernames = cursor.fetchall()
|
usernames = cursor.fetchall()
|
||||||
|
|
||||||
for username in usernames:
|
for username in usernames:
|
||||||
@@ -41,9 +37,6 @@ def remove_user_data(db: Database, cutoff_date: int, dir: Path):
|
|||||||
shutil.rmtree(user_dir)
|
shutil.rmtree(user_dir)
|
||||||
print(f"Deleted directory: {user_dir}")
|
print(f"Deleted directory: {user_dir}")
|
||||||
|
|
||||||
finally:
|
|
||||||
db.close()
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
db = Database(sys.argv[2])
|
db = Database(sys.argv[2])
|
||||||
|
|||||||
Reference in New Issue
Block a user