Skip to content

Commit 83454c7

Browse files
authored
[QueryBuilder] Implement get_creation_statistics for SQLite backend (#6763)
This commit improves backend parity between two database backends by implementing `get_creation_statistics` for the SQLite backend. It achieves this by using an alternative method to implement `date_trunc`, which is available in PostgreSQL but not in SQLite.
1 parent ae49af6 commit 83454c7

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

src/aiida/storage/psql_dos/orm/querybuilder/main.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -798,8 +798,27 @@ def get_creation_statistics(self, user_pk: Optional[int] = None) -> Dict[str, An
798798

799799
total_query = session.query(self.Node)
800800
types_query = session.query(self.Node.node_type.label('typestring'), sa_func.count(self.Node.id))
801+
802+
dialect = session.bind.dialect.name
803+
date_format = '%Y-%m-%d'
804+
805+
if dialect == 'sqlite':
806+
cday = sa_func.strftime(date_format, sa_func.datetime(self.Node.ctime, 'localtime'))
807+
808+
def date_to_str(d):
809+
return d
810+
811+
elif dialect == 'postgresql':
812+
cday = sa_func.date_trunc('day', self.Node.ctime)
813+
814+
def date_to_str(d):
815+
return d.strftime(date_format)
816+
817+
else:
818+
raise NotImplementedError(f'unsupported dialect: {dialect}')
819+
801820
stat_query = session.query(
802-
sa_func.date_trunc('day', self.Node.ctime).label('cday'),
821+
cday.label('cday'),
803822
sa_func.count(self.Node.id),
804823
)
805824

@@ -817,7 +836,7 @@ def get_creation_statistics(self, user_pk: Optional[int] = None) -> Dict[str, An
817836
# Nodes created per day
818837
stat = stat_query.group_by('cday').order_by('cday').all()
819838

820-
ctime_by_day = {_[0].strftime('%Y-%m-%d'): _[1] for _ in stat}
839+
ctime_by_day = {date_to_str(entry[0]): entry[1] for entry in stat}
821840
retdict['ctime_by_day'] = ctime_by_day
822841

823842
return retdict

tests/orm/test_querybuilder.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,9 +1581,6 @@ class TestManager:
15811581
def init_db(self, backend):
15821582
self.backend = backend
15831583

1584-
# This fails with sqlite with:
1585-
# sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such function: date_trunc
1586-
@pytest.mark.requires_psql
15871584
def test_statistics(self):
15881585
"""Test if the statistics query works properly.
15891586
@@ -1620,9 +1617,6 @@ def store_and_add(n, statistics):
16201617

16211618
assert new_db_statistics == expected_db_statistics
16221619

1623-
# This fails with sqlite with:
1624-
# sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such function: date_trunc
1625-
@pytest.mark.requires_psql
16261620
def test_statistics_default_class(self):
16271621
"""Test if the statistics query works properly.
16281622

tests/restapi/test_statistics.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,7 @@ def linearize_namespace(tree_namespace, linear_namespace=None):
3333
return linear_namespace
3434

3535

36-
# This test does not work with SQLite since it uses the `statistics` endpoint,
37-
# which uses `date_trunc` under the hood, which is not implemented in SQLite.
3836
@pytest.mark.usefixtures('populate_restapi_database')
39-
@pytest.mark.requires_psql
4037
def test_count_consistency(restapi_server, server_url):
4138
"""Test the consistency in values between full_type_count and statistics"""
4239
server = restapi_server()

0 commit comments

Comments
 (0)