aiida.backends.sqlalchemy package

Module with implementation of the database backend using SqlAlchemy.

aiida.backends.sqlalchemy.get_scoped_session(**kwargs)[source]

Return a scoped session

According to SQLAlchemy docs, this returns always the same object within a thread, and a different object in a different thread. Moreover, since we update the session class upon forking, different session objects will be used.

Parameters

kwargs – keyword argument that will be passed on to aiida.backends.utils.create_sqlalchemy_engine(), opening the possibility to change QueuePool time outs and more. See https://docs.sqlalchemy.org/en/13/core/engines.html?highlight=create_engine#sqlalchemy.create_engine for more info.

aiida.backends.sqlalchemy.reset_session()[source]

Reset the session which means setting the global engine and session factory instances to None.

Submodules

Utilities and configuration of the SqlAlchemy database schema.

class aiida.backends.sqlalchemy.manager.SqlaBackendManager[source]

Bases: aiida.backends.manager.BackendManager

Class to manage the database schema.

__module__ = 'aiida.backends.sqlalchemy.manager'
_load_backend_environment(**kwargs)[source]

Load the backend environment.

Parameters

kwargs – keyword arguments that will be passed on to aiida.backends.sqlalchemy.get_scoped_session().

_migrate_database_version()[source]

Migrate the database to the current schema version.

static alembic_config()[source]

Context manager to return an instance of an Alembic configuration with the current connection inserted.

Returns

instance of alembic.config.Config

get_schema_version_code()[source]

Return the code schema version.

get_schema_version_database()[source]

Return the database schema version.

Returns

distutils.version.StrictVersion with schema version of the database

get_schema_version_reset(schema_generation_code)[source]

Return schema version the database should have to be able to automatically reset to code schema generation.

Parameters

schema_generation_code – the schema generation of the code.

Returns

schema version

get_settings_manager()[source]

Return an instance of the SettingsManager.

Returns

SettingsManager

is_database_schema_ahead()[source]

Determine whether the database schema version is ahead of the code schema version.

Warning

this will not check whether the schema generations are equal

Returns

boolean, True if the database schema version is ahead of the code schema version.

reset_backend_environment()[source]

Reset the backend environment.

set_schema_version_database(version)[source]

Set the database schema version.

Parameters

version – string with schema version to set

class aiida.backends.sqlalchemy.manager.SqlaSettingsManager[source]

Bases: aiida.backends.manager.SettingsManager

Class to get, set and delete settings from the DbSettings table.

__module__ = 'aiida.backends.sqlalchemy.manager'
delete(key)[source]

Delete the setting with the given key.

Parameters

key – the key identifying the setting

Raises

~aiida.common.exceptions.NotExistent if the settings does not exist

get(key)[source]

Return the setting with the given key.

Parameters

key – the key identifying the setting

Returns

Setting

Raises

~aiida.common.exceptions.NotExistent if the settings does not exist

set(key, value, description=None)[source]

Return the settings with the given key.

Parameters
  • key – the key identifying the setting

  • value – the value for the setting

  • description – optional setting description

table_name = 'db_dbsetting'
validate_table_existence()[source]

Verify that the DbSetting table actually exists.

Raises

~aiida.common.exceptions.NotExistent if the settings table does not exist

Module to manage custom queries under SQLA backend.

class aiida.backends.sqlalchemy.queries.SqlaQueryManager(backend)[source]

Bases: aiida.backends.general.abstractqueries.AbstractQueryManager

SQLAlchemy implementation of custom queries, for efficiency reasons.

__abstractmethods__ = frozenset({})
__module__ = 'aiida.backends.sqlalchemy.queries'
_abc_impl = <_abc_data object>
get_creation_statistics(user_pk=None)[source]

Return a dictionary with the statistics of node creation, summarized by day, optimized for the Django backend.

Note

Days when no nodes were created are not present in the returned ctime_by_day dictionary.

Parameters

user_pk – If None (default), return statistics for all users. If user pk is specified, return only the statistics for the given user.

Returns

a dictionary as follows:

{
   "total": TOTAL_NUM_OF_NODES,
   "types": {TYPESTRING1: count, TYPESTRING2: count, ...},
   "ctime_by_day": {'YYYY-MMM-DD': count, ...}

where in ctime_by_day the key is a string in the format ‘YYYY-MM-DD’ and the value is an integer with the number of nodes created that day.

This module contains the codebase for the setUpClass and tearDown methods used internally by the AiidaTestCase. This inherits only from ‘object’ to avoid that it is picked up by the automatic discovery of tests (It shouldn’t, as it risks to destroy the DB if there are not the checks in place, and these are implemented in the AiidaTestCase.

class aiida.backends.sqlalchemy.testbase.SqlAlchemyTests[source]

Bases: aiida.backends.testimplbase.AiidaTestImplementation

Base class to test SQLA-related functionalities.

__abstractmethods__ = frozenset({})
__module__ = 'aiida.backends.sqlalchemy.testbase'
_abc_impl = <_abc_data object>
_backend = None
property backend

Get the backend.

clean_db()[source]

This method implements the logic to fully clean the DB.

connection = None
setUpClass_method()[source]

This class prepares the database (cleans it up and installs some basic entries). You have also to set a self.computer and a self.user_email as explained in the docstring of the AiidaTestImplemention docstring.

tearDownClass_method()[source]

Backend-specific tasks for tearing down the test environment.

Utility functions specific to the SqlAlchemy backend.

aiida.backends.sqlalchemy.utils.delete_nodes_and_connections_sqla(pks_to_delete)[source]

Delete all nodes corresponding to pks in the input. :param pks_to_delete: A list, tuple or set of pks that should be deleted.

aiida.backends.sqlalchemy.utils.flag_modified(instance, key)[source]

Wrapper around sqlalchemy.orm.attributes.flag_modified to correctly dereference utils.ModelWrapper

Since SqlAlchemy 1.2.12 (and maybe earlier but not in 1.0.19) the flag_modified function will check that the key is actually present in the instance or it will except. If we pass a model instance, wrapped in the ModelWrapper the call will raise an InvalidRequestError. In this function that wraps the flag_modified of SqlAlchemy, we derefence the model instance if the passed instance is actually wrapped in the ModelWrapper.

aiida.backends.sqlalchemy.utils.get_pg_tc(links_table_name, links_table_input_field, links_table_output_field, closure_table_name, closure_table_parent_field, closure_table_child_field)[source]

Return the transitive closure table template

aiida.backends.sqlalchemy.utils.install_tc(session)[source]

Install the transitive closure table with SqlAlchemy.