diff --git a/src/aiida/storage/psql_dos/orm/querybuilder/main.py b/src/aiida/storage/psql_dos/orm/querybuilder/main.py index 490114f5f5..0a677218e3 100644 --- a/src/aiida/storage/psql_dos/orm/querybuilder/main.py +++ b/src/aiida/storage/psql_dos/orm/querybuilder/main.py @@ -798,8 +798,27 @@ def get_creation_statistics(self, user_pk: Optional[int] = None) -> Dict[str, An total_query = session.query(self.Node) types_query = session.query(self.Node.node_type.label('typestring'), sa_func.count(self.Node.id)) + + dialect = session.bind.dialect.name + date_format = '%Y-%m-%d' + + if dialect == 'sqlite': + cday = sa_func.strftime(date_format, sa_func.datetime(self.Node.ctime, 'localtime')) + + def date_to_str(d): + return d + + elif dialect == 'postgresql': + cday = sa_func.date_trunc('day', self.Node.ctime) + + def date_to_str(d): + return d.strftime(date_format) + + else: + raise NotImplementedError(f'unsupported dialect: {dialect}') + stat_query = session.query( - sa_func.date_trunc('day', self.Node.ctime).label('cday'), + cday.label('cday'), sa_func.count(self.Node.id), ) @@ -817,7 +836,7 @@ def get_creation_statistics(self, user_pk: Optional[int] = None) -> Dict[str, An # Nodes created per day stat = stat_query.group_by('cday').order_by('cday').all() - ctime_by_day = {_[0].strftime('%Y-%m-%d'): _[1] for _ in stat} + ctime_by_day = {date_to_str(entry[0]): entry[1] for entry in stat} retdict['ctime_by_day'] = ctime_by_day return retdict diff --git a/tests/orm/test_querybuilder.py b/tests/orm/test_querybuilder.py index 5dcf2c2e58..83f156c168 100644 --- a/tests/orm/test_querybuilder.py +++ b/tests/orm/test_querybuilder.py @@ -1581,9 +1581,6 @@ class TestManager: def init_db(self, backend): self.backend = backend - # This fails with sqlite with: - # sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such function: date_trunc - @pytest.mark.requires_psql def test_statistics(self): """Test if the statistics query works properly. @@ -1620,9 +1617,6 @@ def store_and_add(n, statistics): assert new_db_statistics == expected_db_statistics - # This fails with sqlite with: - # sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such function: date_trunc - @pytest.mark.requires_psql def test_statistics_default_class(self): """Test if the statistics query works properly. diff --git a/tests/restapi/test_statistics.py b/tests/restapi/test_statistics.py index f125ccff1d..cfc025b608 100644 --- a/tests/restapi/test_statistics.py +++ b/tests/restapi/test_statistics.py @@ -33,10 +33,7 @@ def linearize_namespace(tree_namespace, linear_namespace=None): return linear_namespace -# This test does not work with SQLite since it uses the `statistics` endpoint, -# which uses `date_trunc` under the hood, which is not implemented in SQLite. @pytest.mark.usefixtures('populate_restapi_database') -@pytest.mark.requires_psql def test_count_consistency(restapi_server, server_url): """Test the consistency in values between full_type_count and statistics""" server = restapi_server()