-
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.
Merge pull request #1111 from sphuber/fix_1109_mutable_attributes
Restore proper mutability implementation of Node attributes
- Loading branch information
Showing
20 changed files
with
357 additions
and
363 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
aiida/backends/djsite/db/migrations/0008_code_hidden_to_extra.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,49 @@ | ||
# -*- 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 # | ||
########################################################################### | ||
from __future__ import unicode_literals | ||
|
||
from django.db import models, migrations | ||
from aiida.backends.djsite.db.migrations import update_schema_version | ||
|
||
|
||
SCHEMA_VERSION = "1.0.8" | ||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('db', '0007_update_linktypes'), | ||
] | ||
|
||
operations = [ | ||
# The 'hidden' property of AbstractCode has been changed from an attribute to an extra | ||
# Therefore we find all nodes of type Code and if they have an attribute with the key 'hidden' | ||
# we move that value to the extra table | ||
# | ||
# First we copy the 'hidden' attributes from code.Code. nodes to the db_extra table | ||
migrations.RunSQL(""" | ||
INSERT INTO db_dbextra (key, datatype, tval, fval, ival, bval, dval, dbnode_id) ( | ||
SELECT db_dbattribute.key, db_dbattribute.datatype, db_dbattribute.tval, db_dbattribute.fval, db_dbattribute.ival, db_dbattribute.bval, db_dbattribute.dval, db_dbattribute.dbnode_id | ||
FROM db_dbattribute JOIN db_dbnode ON db_dbnode.id = db_dbattribute.dbnode_id | ||
WHERE db_dbattribute.key = 'hidden' | ||
AND db_dbnode.type = 'code.Code.' | ||
); | ||
"""), | ||
# Secondly, we delete the original entries from the DbAttribute table | ||
migrations.RunSQL(""" | ||
DELETE FROM db_dbattribute | ||
WHERE id in ( | ||
SELECT db_dbattribute.id | ||
FROM db_dbattribute | ||
JOIN db_dbnode ON db_dbnode.id = db_dbattribute.dbnode_id | ||
WHERE db_dbattribute.key = 'hidden' AND db_dbnode.type = 'code.Code.' | ||
); | ||
"""), | ||
update_schema_version(SCHEMA_VERSION) | ||
] |
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
69 changes: 69 additions & 0 deletions
69
aiida/backends/sqlalchemy/migrations/versions/35d4ee9a1b0e_code_hidden_attr_to_extra.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,69 @@ | ||
# -*- 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 # | ||
########################################################################### | ||
"""Migrating 'hidden' properties from DbAttribute to DbExtra for code.Code. nodes | ||
Revision ID: 35d4ee9a1b0e | ||
Revises: 89176227b25 | ||
Create Date: 2018-02-21 22:00:43.460534 | ||
""" | ||
from alembic import op | ||
from sqlalchemy.orm.session import Session | ||
from sqlalchemy.orm.attributes import flag_modified | ||
from aiida.backends.sqlalchemy.models.node import DbNode | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = '35d4ee9a1b0e' | ||
down_revision = '89176227b25' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
conn = op.get_bind() | ||
session = Session(bind=conn) | ||
|
||
# The 'hidden' property of AbstractCode has been changed from an attribute to an extra | ||
# Therefore we find all nodes of type Code and if they have an attribute with the key 'hidden' | ||
# we move that value to the extra field | ||
codes = session.query(DbNode).filter(DbNode.type == 'code.Code.') | ||
for code in codes: | ||
if 'hidden' in code.attributes: | ||
session.add(code) | ||
|
||
hidden = code.attributes.pop('hidden') | ||
code.extras['hidden'] = hidden | ||
|
||
flag_modified(code, 'attributes') | ||
flag_modified(code, 'extras') | ||
|
||
session.flush() | ||
session.commit() | ||
|
||
|
||
def downgrade(): | ||
conn = op.get_bind() | ||
session = Session(bind=conn) | ||
|
||
# Reverse logic from the upgrade | ||
codes = session.query(DbNode).filter(DbNode.type == 'code.Code.') | ||
for code in codes: | ||
if 'hidden' in code.extras: | ||
session.add(code) | ||
|
||
hidden = code.extras.pop('hidden') | ||
code.attributes['hidden'] = hidden | ||
|
||
flag_modified(code, 'attributes') | ||
flag_modified(code, 'extras') | ||
|
||
session.flush() | ||
session.commit() |
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.