diff --git a/hydra_base/db/alembic/versions/9b9f7d7a4f21_hybrid_attr_phase1.py b/hydra_base/db/alembic/versions/9b9f7d7a4f21_hybrid_attr_phase1.py new file mode 100644 index 00000000..9e071a4e --- /dev/null +++ b/hydra_base/db/alembic/versions/9b9f7d7a4f21_hybrid_attr_phase1.py @@ -0,0 +1,68 @@ +"""hybrid_attr_phase1 + +Revision ID: 9b9f7d7a4f21 +Revises: c1a2b3d4e5f7 +Create Date: 2026-07-01 00:00:00.000000 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '9b9f7d7a4f21' +down_revision = 'c1a2b3d4e5f7' +branch_labels = None +depends_on = None + + +def upgrade(): + op.add_column('tResourceAttr', sa.Column('attr_name', sa.String(length=200), nullable=True)) + op.add_column('tResourceAttr', sa.Column('dimension_id', sa.Integer(), nullable=True)) + op.create_foreign_key('fk_tresourceattr_dimension_id', 'tResourceAttr', 'tDimension', ['dimension_id'], ['id']) + + op.add_column('tTypeAttr', sa.Column('attr_name', sa.String(length=200), nullable=True)) + op.add_column('tTypeAttr', sa.Column('dimension_id', sa.Integer(), nullable=True)) + op.create_foreign_key('fk_ttypeattr_dimension_id', 'tTypeAttr', 'tDimension', ['dimension_id'], ['id']) + + op.execute( + """ + UPDATE tResourceAttr + SET attr_name = ( + SELECT name FROM tAttr WHERE tAttr.id = tResourceAttr.attr_id + ), + dimension_id = ( + SELECT dimension_id FROM tAttr WHERE tAttr.id = tResourceAttr.attr_id + ) + WHERE attr_id IS NOT NULL + """ + ) + + op.execute( + """ + UPDATE tTypeAttr + SET attr_name = ( + SELECT name FROM tAttr WHERE tAttr.id = tTypeAttr.attr_id + ), + dimension_id = ( + SELECT dimension_id FROM tAttr WHERE tAttr.id = tTypeAttr.attr_id + ) + WHERE attr_id IS NOT NULL + """ + ) + + op.alter_column('tResourceAttr', 'attr_id', existing_type=sa.Integer(), nullable=True) + op.alter_column('tTypeAttr', 'attr_id', existing_type=sa.Integer(), nullable=True) + + +def downgrade(): + op.alter_column('tTypeAttr', 'attr_id', existing_type=sa.Integer(), nullable=False) + op.alter_column('tResourceAttr', 'attr_id', existing_type=sa.Integer(), nullable=False) + + op.drop_constraint('fk_ttypeattr_dimension_id', 'tTypeAttr', type_='foreignkey') + op.drop_column('tTypeAttr', 'dimension_id') + op.drop_column('tTypeAttr', 'attr_name') + + op.drop_constraint('fk_tresourceattr_dimension_id', 'tResourceAttr', type_='foreignkey') + op.drop_column('tResourceAttr', 'dimension_id') + op.drop_column('tResourceAttr', 'attr_name') diff --git a/hydra_base/db/model/network/link.py b/hydra_base/db/model/network/link.py index f6edf58e..40203959 100644 --- a/hydra_base/db/model/network/link.py +++ b/hydra_base/db/model/network/link.py @@ -75,9 +75,11 @@ def link_description(self): def link_description_setter(self): self.description = self.link_description - def add_attribute(self, attr_id, attr_is_var='N'): + def add_attribute(self, attr_id, attr_is_var='N', attr_name=None, dimension_id=None): res_attr = ResourceAttr() res_attr.attr_id = attr_id + res_attr.attr_name = attr_name + res_attr.dimension_id = dimension_id res_attr.attr_is_var = attr_is_var res_attr.ref_key = self.ref_key res_attr.link_id = self.id diff --git a/hydra_base/db/model/network/network.py b/hydra_base/db/model/network/network.py index 98114bcc..c50d025d 100644 --- a/hydra_base/db/model/network/network.py +++ b/hydra_base/db/model/network/network.py @@ -78,9 +78,11 @@ def is_owner(self, user_id): def get_name(self): return self.name - def add_attribute(self, attr_id, attr_is_var='N'): + def add_attribute(self, attr_id, attr_is_var='N', attr_name=None, dimension_id=None): res_attr = ResourceAttr() res_attr.attr_id = attr_id + res_attr.attr_name = attr_name + res_attr.dimension_id = dimension_id res_attr.attr_is_var = attr_is_var res_attr.ref_key = self.ref_key res_attr.network_id = self.id diff --git a/hydra_base/db/model/network/node.py b/hydra_base/db/model/network/node.py index 40012bac..530fc948 100644 --- a/hydra_base/db/model/network/node.py +++ b/hydra_base/db/model/network/node.py @@ -73,9 +73,11 @@ def node_description(self): def node_description_setter(self): self.description = self.node_description - def add_attribute(self, attr_id, attr_is_var='N'): + def add_attribute(self, attr_id, attr_is_var='N', attr_name=None, dimension_id=None): res_attr = ResourceAttr() res_attr.attr_id = attr_id + res_attr.attr_name = attr_name + res_attr.dimension_id = dimension_id res_attr.attr_is_var = attr_is_var res_attr.ref_key = self.ref_key res_attr.node_id = self.id diff --git a/hydra_base/db/model/network/resourceattr.py b/hydra_base/db/model/network/resourceattr.py index f7511c78..b04652c3 100644 --- a/hydra_base/db/model/network/resourceattr.py +++ b/hydra_base/db/model/network/resourceattr.py @@ -35,7 +35,9 @@ class ResourceAttr(Base, Inspect): ) id = Column(Integer(), primary_key=True, nullable=False) - attr_id = Column(Integer(), ForeignKey('tAttr.id'), nullable=False) + attr_id = Column(Integer(), ForeignKey('tAttr.id'), nullable=True) + attr_name = Column(String(200), nullable=True) + dimension_id = Column(Integer(), ForeignKey('tDimension.id'), nullable=True) ref_key = Column(String(60), nullable=False, index=True) network_id = Column(Integer(), ForeignKey('tNetwork.id'), index=True, nullable=True) project_id = Column(Integer(), ForeignKey('tProject.id'), index=True, nullable=True) @@ -46,6 +48,7 @@ class ResourceAttr(Base, Inspect): cr_date = Column(TIMESTAMP(), nullable=False, server_default=text(u'CURRENT_TIMESTAMP')) attr = relationship('Attr') + dimension = relationship('Dimension', foreign_keys=[dimension_id]) project = relationship('Project', backref=backref('attributes', uselist=True, diff --git a/hydra_base/db/model/network/resourcegroup.py b/hydra_base/db/model/network/resourcegroup.py index 579cbb13..72a3ab8f 100644 --- a/hydra_base/db/model/network/resourcegroup.py +++ b/hydra_base/db/model/network/resourcegroup.py @@ -69,9 +69,11 @@ def group_description(self): def group_description_setter(self): self.description = self.group_description - def add_attribute(self, attr_id, attr_is_var='N'): + def add_attribute(self, attr_id, attr_is_var='N', attr_name=None, dimension_id=None): res_attr = ResourceAttr() res_attr.attr_id = attr_id + res_attr.attr_name = attr_name + res_attr.dimension_id = dimension_id res_attr.attr_is_var = attr_is_var res_attr.ref_key = self.ref_key res_attr.group_id = self.id diff --git a/hydra_base/db/model/project.py b/hydra_base/db/model/project.py index f1467328..5f9483a4 100644 --- a/hydra_base/db/model/project.py +++ b/hydra_base/db/model/project.py @@ -363,9 +363,11 @@ def get_attribute_data(self): self.attribute_data = attribute_data_rs return attribute_data_rs - def add_attribute(self, attr_id, attr_is_var='N'): + def add_attribute(self, attr_id, attr_is_var='N', attr_name=None, dimension_id=None): res_attr = ResourceAttr() res_attr.attr_id = attr_id + res_attr.attr_name = attr_name + res_attr.dimension_id = dimension_id res_attr.attr_is_var = attr_is_var res_attr.ref_key = self.ref_key res_attr.project_id = self.id diff --git a/hydra_base/db/model/template.py b/hydra_base/db/model/template.py index aab8563a..faa270b5 100644 --- a/hydra_base/db/model/template.py +++ b/hydra_base/db/model/template.py @@ -490,7 +490,9 @@ class TypeAttr(Base, Inspect): id = Column(Integer(), primary_key=True, nullable=False) parent_id = Column(Integer(), ForeignKey('tTypeAttr.id')) - attr_id = Column(Integer(), ForeignKey('tAttr.id'), nullable=False) + attr_id = Column(Integer(), ForeignKey('tAttr.id'), nullable=True) + attr_name = Column(String(200), nullable=True) + dimension_id = Column(Integer(), ForeignKey('tDimension.id'), nullable=True) type_id = Column(Integer(), ForeignKey('tTemplateType.id', ondelete='CASCADE'), nullable=False) default_dataset_id = Column(Integer(), ForeignKey('tDataset.id')) @@ -507,6 +509,7 @@ class TypeAttr(Base, Inspect): parent = relationship('TypeAttr', remote_side=[id], backref=backref("children", order_by=id)) attr = relationship('Attr') + dimension = relationship('Dimension', foreign_keys=[dimension_id]) #Don't use a cascade delete all here. Instead force the code to delete the typeattrs #manually, to avoid accidentally deleting them templatetype = relationship('TemplateType', diff --git a/hydra_base/lib/attributes.py b/hydra_base/lib/attributes.py index 0339aece..a4cb26ef 100644 --- a/hydra_base/lib/attributes.py +++ b/hydra_base/lib/attributes.py @@ -924,6 +924,12 @@ def add_resource_attributes(resource_attributes, **kwargs): field_to_key = {v: k for k, v in key_to_field.items()} + attr_ids = [ra.attr_id for ra in resource_attributes if ra.get('attr_id') is not None] + attr_lookup = {} + if attr_ids: + attrs = db.DBSession.query(Attr).filter(Attr.id.in_(attr_ids)).all() + attr_lookup = {a.id: a for a in attrs} + for ra in resource_attributes: if ra.get('is_var') is not None: ra['attr_is_var'] = ra['is_var'] @@ -956,6 +962,12 @@ def add_resource_attributes(resource_attributes, **kwargs): if target_field: ra[target_field] = ra['ref_id'] + attr_id = ra.get('attr_id') + attr_i = attr_lookup.get(attr_id) + if attr_i is not None: + ra['attr_name'] = ra.get('attr_name', attr_i.name) + ra['dimension_id'] = ra.get('dimension_id', attr_i.dimension_id) + #2. Build DB-column-filtered dicts for all incoming RAs cols = set(c.name for c in ResourceAttr.__table__.columns) - {'id', 'cr_date', 'updated_at'} rows = [{k: v for k, v in ra.items() if k in cols} for ra in resource_attributes] @@ -1056,7 +1068,12 @@ def add_resource_attribute(resource_type, resource_id, attr_id, is_var, error_on attr_is_var = 'Y' if is_var in (True, 'Y') else 'N' - new_ra = resource_i.add_attribute(attr_id, attr_is_var) + new_ra = resource_i.add_attribute( + attr_id, + attr_is_var, + attr_name=attr.name, + dimension_id=attr.dimension_id, + ) db.DBSession.flush() return new_ra @@ -1091,7 +1108,11 @@ def add_resource_attrs_from_type(type_id, resource_type, resource_id, **kwargs): new_resource_attrs = [] for item in type_i.typeattrs: if attrs.get(item.attr_id) is None: - ra = resource_i.add_attribute(item.attr_id) + ra = resource_i.add_attribute( + item.attr_id, + attr_name=item.attr.name if item.attr is not None else getattr(item, 'attr_name', None), + dimension_id=item.attr.dimension_id if item.attr is not None else getattr(item, 'dimension_id', None), + ) new_resource_attrs.append(ra) db.DBSession.flush() diff --git a/hydra_base/util/hdb.py b/hydra_base/util/hdb.py index a36bdd87..346a6d9d 100644 --- a/hydra_base/util/hdb.py +++ b/hydra_base/util/hdb.py @@ -19,7 +19,7 @@ import os import json -from ..db.model import Network, Scenario, Project, User, Role, Perm, RolePerm, RoleUser, ResourceAttr, ResourceType, Dimension, Unit +from ..db.model import Network, Scenario, Project, User, Role, Perm, RolePerm, RoleUser, ResourceAttr, ResourceType, Dimension, Unit, Attr from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound from .. import db import datetime @@ -76,11 +76,22 @@ def add_resource_attributes(resource_i, attributes): if attributes is None: return {} resource_attrs = {} + attr_ids = [ra.attr_id for ra in attributes if getattr(ra, 'attr_id', None) is not None] + attr_lookup = {} + if attr_ids: + all_attrs = db.DBSession.query(Attr).filter(Attr.id.in_(attr_ids)).all() + attr_lookup = {a.id: a for a in all_attrs} #ra is for ResourceAttr for ra in attributes: if ra.id < 0: - ra_i = resource_i.add_attribute(ra.attr_id, ra.attr_is_var) + attr_i = attr_lookup.get(ra.attr_id) + ra_i = resource_i.add_attribute( + ra.attr_id, + ra.attr_is_var, + attr_name=attr_i.name if attr_i is not None else None, + dimension_id=attr_i.dimension_id if attr_i is not None else None, + ) db.DBSession.add(ra_i) else: ra_i = db.DBSession.query(ResourceAttr).filter(ResourceAttr.id==ra.id).one()