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 scopedsessionclass upon forking, also forks have different session objects.

Submodules

Functions to manage the global settings stored in the DB (in the DbSettings table.

aiida.backends.sqlalchemy.globalsettings.del_global_setting(key)[source]

Return the value of the given setting, or raise a KeyError if the setting is not present in the DB.

Raises:KeyError – if the setting does not exist in the DB
aiida.backends.sqlalchemy.globalsettings.get_global_setting(key)[source]

Return the value of the given setting, or raise a KeyError if the setting is not present in the DB.

Raises:KeyError – if the setting does not exist in the DB
aiida.backends.sqlalchemy.globalsettings.get_global_setting_description(key)[source]

Return the description for the given setting variable, as stored in the DB, or raise a KeyError if the setting is not present in the DB or the table doesn’t exist.

aiida.backends.sqlalchemy.globalsettings.set_global_setting(key, value, description=None)[source]

Set a global setting in the DbSetting table (therefore, stored at the DB level).

aiida.backends.sqlalchemy.globalsettings.table_check_test()[source]

Checks if the db_setting table exists in the database. If it doesn’t exist it rainses a KeyError.

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_cache = <_weakrefset.WeakSet object>
_abc_negative_cache = <_weakrefset.WeakSet object>
_abc_negative_cache_version = 102
_abc_registry = <_weakrefset.WeakSet 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.

aiida.backends.sqlalchemy.transition_06dj_to_07sqla.aiidadb_backend_value_sqla = 'sqlalchemy'

This scipt transitions an Django database 0.6.0 to an SQLAlchemy database 0.7.0. It also updates the needed config files.

It is supposed to be executed via ipython in the following way:

from aiida.backends.sqlalchemy.transition import transition transition(profile=”your_profile”,group_size=10000)

aiida.backends.sqlalchemy.transition_06dj_to_07sqla.attributes_to_dict(attr_list)[source]

Transform the attributes of a node into a dictionary. It assumes the key are ordered alphabetically, and that they all belong to the same node.

aiida.backends.sqlalchemy.transition_06dj_to_07sqla.change_backend_to_sqla(profile=None)[source]

Gets the main AiiDA configuration and searches if there is a backend defined. If there isn’t any then Django is added.

aiida.backends.sqlalchemy.transition_06dj_to_07sqla.create_gin_index()[source]

Create the GIN index for the attributes column of db_dbnode.

aiida.backends.sqlalchemy.transition_06dj_to_07sqla.select_from_key(key, d)[source]

Return element of the dict to do the insertion on. If it is foo.1.bar, it will return d[“foo”][1]. If it is only foo, it will return d directly.

aiida.backends.sqlalchemy.transition_06dj_to_07sqla.set_correct_schema_version_and_backend()[source]
aiida.backends.sqlalchemy.transition_06dj_to_07sqla.transition(profile=None, group_size=1000, delete_table=False)[source]
aiida.backends.sqlalchemy.transition_06dj_to_07sqla.transition_attributes(profile=None, group_size=1000, debug=False, delete_table=False)[source]

Migrate the DbAttribute table into the attributes column of db_dbnode.

aiida.backends.sqlalchemy.transition_06dj_to_07sqla.transition_config_files(profile=None)[source]
aiida.backends.sqlalchemy.transition_06dj_to_07sqla.transition_db(profile=None, group_size=1000, delete_table=False)[source]

Migrate the attributes, extra, and some other columns to use JSONMigrate the attributes, extra, and some other columns to use JSONB column type.

aiida.backends.sqlalchemy.transition_06dj_to_07sqla.transition_extras(profile=None, group_size=1000, delete_table=False)[source]

Migrate the DbExtra table into the extras column for db_dbnode.

aiida.backends.sqlalchemy.transition_06dj_to_07sqla.transition_json_column(profile=None)[source]

Migrate the TEXT column containing JSON into JSON columns

aiida.backends.sqlalchemy.transition_06dj_to_07sqla.transition_load_db_env(profile=None, *args, **kwargs)[source]
aiida.backends.sqlalchemy.transition_06dj_to_07sqla.transition_settings(profile=None)[source]

Migrate the DbAttribute table into the attributes column of db_dbnode.

aiida.backends.sqlalchemy.utils._load_dbenv_noschemacheck(profile=None, connection=None)[source]

Load the SQLAlchemy database.

aiida.backends.sqlalchemy.utils.check_schema_version(profile_name=None)[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.dumps_json(d)[source]

Transforms all datetime object into isoformat and then returns the JSON

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=None, connection=None)[source]

Load the database environment (SQLAlchemy) and perform some checks.

Parameters:profile – the string with the profile to use. If not specified, use the default one specified in the AiiDA configuration file.
aiida.backends.sqlalchemy.utils.loads_json(s)[source]

Loads the json and try to parse each basestring as a datetime object

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.recreate_after_fork(engine)[source]
Parameters:engine – the engine that will be used by the sessionmaker

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

aiida.backends.sqlalchemy.utils.reset_session(config)[source]
Parameters:config – the configuration of the profile from the configuration file

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