-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make
Group
sub classable through entry points (#3882)
We add the `aiida.groups` entry point group where sub classes of the `aiida.orm.groups.Group` class can be registered. A new metaclass is used to automatically set the `type_string` based on the entry point of the `Group` sub class. This will make it possible to reload the correct sub class when reloading from the database. If the `GroupMeta` metaclass cannot retrieve the corresponding entry point of the subclass, a warning is issued that any instances of this class will not be storable and the `_type_string` attribute is set to `None`. This can be checked by the `store` method which will make it fail. We choose to only except in the `store` method such that it is still possible to define and instantiate subclasses of `Group` that have not yet been registered. This is useful for testing and experimenting. Since the group type strings are now based on the entry point names, the existing group type strings in the database have to be migrated: * `user` -> `core` * `data.upf.family` -> `core.upf` * `auto.import` -> `core.import` * `auto.run` -> `core.auto` When loading a `Group` instance from the database, the loader will try to resolve the type string to the corresponding subclass through the entry points. If this fails, a warning is issued and we fallback on the base `Group` class.
- Loading branch information
Showing
30 changed files
with
696 additions
and
364 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
aiida/backends/djsite/db/migrations/0044_dbgroup_type_string.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# -*- coding: utf-8 -*- | ||
########################################################################### | ||
# Copyright (c), The AiiDA team. All rights reserved. # | ||
# This file is part of the AiiDA code. # | ||
# # | ||
# The code is hosted on GitHub at https://github.com/aiidateam/aiida-core # | ||
# For further information on the license, see the LICENSE.txt file # | ||
# For further information please visit http://www.aiida.net # | ||
########################################################################### | ||
# pylint: disable=invalid-name,too-few-public-methods | ||
"""Migration after the `Group` class became pluginnable and so the group `type_string` changed.""" | ||
|
||
# pylint: disable=no-name-in-module,import-error | ||
from django.db import migrations | ||
from aiida.backends.djsite.db.migrations import upgrade_schema_version | ||
|
||
REVISION = '1.0.44' | ||
DOWN_REVISION = '1.0.43' | ||
|
||
forward_sql = [ | ||
"""UPDATE db_dbgroup SET type_string = 'core' WHERE type_string = 'user';""", | ||
"""UPDATE db_dbgroup SET type_string = 'core.upf' WHERE type_string = 'data.upf';""", | ||
"""UPDATE db_dbgroup SET type_string = 'core.import' WHERE type_string = 'auto.import';""", | ||
"""UPDATE db_dbgroup SET type_string = 'core.auto' WHERE type_string = 'auto.run';""", | ||
] | ||
|
||
reverse_sql = [ | ||
"""UPDATE db_dbgroup SET type_string = 'user' WHERE type_string = 'core';""", | ||
"""UPDATE db_dbgroup SET type_string = 'data.upf' WHERE type_string = 'core.upf';""", | ||
"""UPDATE db_dbgroup SET type_string = 'auto.import' WHERE type_string = 'core.import';""", | ||
"""UPDATE db_dbgroup SET type_string = 'auto.run' WHERE type_string = 'core.auto';""", | ||
] | ||
|
||
|
||
class Migration(migrations.Migration): | ||
"""Migration after the update of group `type_string`""" | ||
dependencies = [ | ||
('db', '0043_default_link_label'), | ||
] | ||
|
||
operations = [ | ||
migrations.RunSQL(sql='\n'.join(forward_sql), reverse_sql='\n'.join(reverse_sql)), | ||
upgrade_schema_version(REVISION, DOWN_REVISION), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
aiida/backends/sqlalchemy/migrations/versions/bf591f31dd12_dbgroup_type_string.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Migration after the `Group` class became pluginnable and so the group `type_string` changed. | ||
Revision ID: bf591f31dd12 | ||
Revises: 118349c10896 | ||
Create Date: 2020-03-31 10:00:52.609146 | ||
""" | ||
# pylint: disable=no-name-in-module,import-error,invalid-name,no-member | ||
from alembic import op | ||
from sqlalchemy.sql import text | ||
|
||
forward_sql = [ | ||
"""UPDATE db_dbgroup SET type_string = 'core' WHERE type_string = 'user';""", | ||
"""UPDATE db_dbgroup SET type_string = 'core.upf' WHERE type_string = 'data.upf';""", | ||
"""UPDATE db_dbgroup SET type_string = 'core.import' WHERE type_string = 'auto.import';""", | ||
"""UPDATE db_dbgroup SET type_string = 'core.auto' WHERE type_string = 'auto.run';""", | ||
] | ||
|
||
reverse_sql = [ | ||
"""UPDATE db_dbgroup SET type_string = 'user' WHERE type_string = 'core';""", | ||
"""UPDATE db_dbgroup SET type_string = 'data.upf' WHERE type_string = 'core.upf';""", | ||
"""UPDATE db_dbgroup SET type_string = 'auto.import' WHERE type_string = 'core.import';""", | ||
"""UPDATE db_dbgroup SET type_string = 'auto.run' WHERE type_string = 'core.auto';""", | ||
] | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = 'bf591f31dd12' | ||
down_revision = '118349c10896' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
"""Migrations for the upgrade.""" | ||
conn = op.get_bind() | ||
statement = text('\n'.join(forward_sql)) | ||
conn.execute(statement) | ||
|
||
|
||
def downgrade(): | ||
"""Migrations for the downgrade.""" | ||
conn = op.get_bind() | ||
statement = text('\n'.join(reverse_sql)) | ||
conn.execute(statement) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.