-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreport.py
More file actions
66 lines (58 loc) · 1.82 KB
/
Copy pathreport.py
File metadata and controls
66 lines (58 loc) · 1.82 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from pathlib import Path
from loguru import logger
from sqlalchemy import func
from sqlmodel import Session, select
from src.db_schema import (
Author,
Dataset,
DataSource,
File,
FileType,
ParameterFile,
TopologyFile,
TrajectoryFile,
engine,
)
# Log file format
# Log file will be erased at each run
# Remove mode="w" to keep log file between runs
logger.add(
f"{Path(__file__).stem}.log",
mode="w",
format="{time:YYYY-MM-DDTHH:mm:ss} | <lvl>{level:<8} | {message}</lvl>",
level="DEBUG",
)
def main():
"""
This script reports the number of rows and columns in each table of the database.
To run this script, use the command:
uv run python report.py
"""
# List of all the models you want to report on.
models = [
Dataset,
DataSource,
Author,
File,
FileType,
TopologyFile,
ParameterFile,
TrajectoryFile,
]
with Session(engine) as session:
for model in models:
# Get the table name from the model's __tablename__ attribute.
table_name = model.__tablename__
# Count the number of columns using the model's table metadata.
n_columns = len(model.__table__.columns)
# Build a SQL query to count the rows in the table.
# select(func.count()) creates a query that returns the count of rows.
# .select_from(model) specifies the table (model) to count rows from.
statement = select(func.count()).select_from(model)
n_rows = session.exec(statement).first()
# Log the table name, number of rows, and number of columns.
logger.info(
f"Table: {table_name:>16} - Columns: {n_columns:5} - Rows: {n_rows:10,}"
)
if __name__ == "__main__":
main()