aiida.backends.sqlalchemy package

aiida.backends.sqlalchemy.get_scoped_session()[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.

aiida.backends.sqlalchemy.recreate_after_fork(engine)[source]

Callback called after a fork.

Not only disposes the engine, but also recreates a new scoped session to use independent sessions in the fork.

Parameters:engine – the engine that will be used by the sessionmaker
aiida.backends.sqlalchemy.reset_session(profile=None)[source]

Resets (global) engine and sessionmaker classes, to create a new one (or creates a new one from scratch if not already available)

Parameters:profile – the profile whose configuration to use to connect to the database

Submodules

Simple wrapper around the alembic command line tool that first loads an AiiDA profile.

aiida.backends.sqlalchemy.manage.execute_alembic_command(command_name, **kwargs)[source]

Execute an Alembic CLI command.

Parameters:
  • command_name – the sub command name
  • kwargs – parameters to pass to the command
class aiida.backends.sqlalchemy.queries.SqlaQueryManager(backend)[source]

Bases: aiida.backends.general.abstractqueries.AbstractQueryManager

SQLAlchemy implementation of custom queries, for efficiency reasons

__abstractmethods__ = frozenset()
__init__(backend)[source]
Parameters:backend (aiida.orm.implementation.sql.SqlBackend) – The AiiDA backend
__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.

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

Bases: aiida.backends.utils.SettingsManager

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

__module__ = 'aiida.backends.sqlalchemy.utils'
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
aiida.backends.sqlalchemy.utils._load_dbenv_noschemacheck(profile)[source]

Load the database environment without checking that code and database schema versions are compatible.

This should ONLY be used internally, inside load_dbenv, and for schema migrations. DO NOT USE OTHERWISE!

Parameters:profile – instance of Profile whose database to load
aiida.backends.sqlalchemy.utils.check_schema_version(profile_name)[source]

Check if the version stored in the database is the same of the version of the code.

Raises:aiida.common.ConfigurationError – if the two schema versions do not match
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_alembic_conf()[source]

This function returns the alembic configuration file contents by doing the necessary updates in the ‘script_location’ name. :return: The alembic configuration.

aiida.backends.sqlalchemy.utils.get_db_schema_version(config)[source]

This function returns the current version of the database. :param config: The alembic configuration. :return: The version of the database.

aiida.backends.sqlalchemy.utils.get_migration_head(config)[source]

This function returns the head of the migration scripts. :param config: The alembic configuration. :return: The version of the head.

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.

aiida.backends.sqlalchemy.utils.load_dbenv(profile)[source]

Load the database environment and ensure that the code and database schema versions are compatible.

Parameters:profile – the string with the profile to use
aiida.backends.sqlalchemy.utils.migrate_database(alembic_cfg=None)[source]

Migrate the database to the latest schema version.

Parameters:config – alembic configuration to use, will use default if not provided
aiida.backends.sqlalchemy.utils.unload_dbenv()[source]

Unload the database environment, which boils down to destroying the current engine and session.