Skip to content

Commit 1966761

Browse files
committed
feat: add clear_all_tables SQLAlchemy helper
1 parent 4474812 commit 1966761

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
http_request_recorder @ git+https://github.com/sipgate/http-request-recorder.git@main
2+
SQLAlchemy~=2.0.43
23
pre-commit

sipgate_e2e_test_utils/db.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from sqlalchemy import text
2+
from sqlalchemy.engine import Engine
3+
from sqlalchemy.orm import Session
4+
5+
6+
def clear_all_tables(db_engine: Engine, model):
7+
with Session(db_engine) as db_session:
8+
tables = list(model.metadata.sorted_tables)
9+
tables.reverse()
10+
db_session.execute(text('SET FOREIGN_KEY_CHECKS = 0;'))
11+
for table in tables:
12+
db_session.execute(table.delete())
13+
db_session.execute(text('SET FOREIGN_KEY_CHECKS = 1;'))
14+
db_session.commit()

0 commit comments

Comments
 (0)