-
Notifications
You must be signed in to change notification settings - Fork 144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add NodeGroup.grouping_node
#11613
#11614
Changes from 3 commits
a569d6b
3557fac
82d5194
8a3292e
679010d
383bfb2
e959d17
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# Generated by Django 5.1.3 on 2024-11-19 09:29 | ||
|
||
import django.db.models.deletion | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("models", "11408_loadstaging_sortorder"), | ||
] | ||
|
||
def add_grouping_node(apps, schema_editor): | ||
NodeGroup = apps.get_model("models", "NodeGroup") | ||
nodegroups_with_nodes = NodeGroup.objects.filter(node__gt=0).distinct() | ||
for nodegroup in nodegroups_with_nodes: | ||
nodegroup.grouping_node_id = nodegroup.pk | ||
NodeGroup.objects.bulk_update(nodegroups_with_nodes, ["grouping_node_id"]) | ||
|
||
PublishedGraph = apps.get_model("models", "PublishedGraph") | ||
published_graphs = PublishedGraph.objects.all() | ||
for published_graph in published_graphs: | ||
for node_dict in published_graph.serialized_graph["nodes"]: | ||
node_dict["grouping_node_id"] = node_dict["nodegroup_id"] | ||
PublishedGraph.objects.bulk_update(published_graphs, ["serialized_graph"]) | ||
|
||
def remove_grouping_node(apps, schema_editor): | ||
PublishedGraph = apps.get_model("models", "PublishedGraph") | ||
published_graphs = PublishedGraph.objects.all() | ||
for published_graph in published_graphs: | ||
for node_dict in published_graph.serialized_graph["nodegroups"]: | ||
node_dict.pop("grouping_node_id", None) | ||
PublishedGraph.objects.bulk_update(published_graphs, ["serialized_graph"]) | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="nodegroup", | ||
name="grouping_node", | ||
field=models.OneToOneField( | ||
blank=True, | ||
db_column="groupingnodeid", | ||
null=True, | ||
on_delete=django.db.models.deletion.SET_NULL, | ||
related_name="grouping_node_nodegroup", | ||
to="models.node", | ||
), | ||
), | ||
migrations.AlterField( | ||
model_name="nodegroup", | ||
name="cardinality", | ||
field=models.CharField( | ||
blank=True, choices=[("1", "1"), ("n", "n")], default="1", max_length=1 | ||
), | ||
), | ||
migrations.AlterField( | ||
model_name="nodegroup", | ||
name="parentnodegroup", | ||
field=models.ForeignKey( | ||
blank=True, | ||
db_column="parentnodegroupid", | ||
null=True, | ||
on_delete=django.db.models.deletion.CASCADE, | ||
related_name="children", | ||
related_query_name="child", | ||
to="models.nodegroup", | ||
), | ||
), | ||
migrations.RunPython(add_grouping_node, remove_grouping_node), | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Generated by Django 5.1.3 on 2024-11-19 09:33 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("models", "11613_nodegroup_grouping_node"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddConstraint( | ||
model_name="node", | ||
constraint=models.CheckConstraint( | ||
condition=models.Q( | ||
("istopnode", True), ("nodegroup__isnull", False), _connector="OR" | ||
), | ||
name="has_nodegroup_or_istopnode", | ||
), | ||
), | ||
migrations.AddConstraint( | ||
model_name="nodegroup", | ||
constraint=models.CheckConstraint( | ||
condition=models.Q( | ||
("grouping_node", models.F("pk")), | ||
("grouping_node__isnull", True), | ||
_connector="OR", | ||
), | ||
name="grouping_node_matches_pk_or_null", | ||
), | ||
), | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -709,14 +709,27 @@ class Meta: | |
class NodeGroup(models.Model): | ||
nodegroupid = models.UUIDField(primary_key=True) | ||
legacygroupid = models.TextField(blank=True, null=True) | ||
cardinality = models.TextField(blank=True, default="1") | ||
cardinality = models.CharField( | ||
max_length=1, blank=True, default="1", choices={"1": "1", "n": "n"} | ||
) | ||
parentnodegroup = models.ForeignKey( | ||
"self", | ||
db_column="parentnodegroupid", | ||
blank=True, | ||
null=True, | ||
on_delete=models.CASCADE, | ||
related_name="children", | ||
related_query_name="child", | ||
) # Allows nodegroups within nodegroups | ||
grouping_node = models.OneToOneField( | ||
"Node", | ||
db_column="groupingnodeid", | ||
blank=True, | ||
null=True, | ||
# models.RESTRICT might be better, but revisit after future graph refactor. | ||
on_delete=models.SET_NULL, | ||
related_name="grouping_node_nodegroup", | ||
) | ||
|
||
def __init__(self, *args, **kwargs): | ||
super(NodeGroup, self).__init__(*args, **kwargs) | ||
|
@@ -726,6 +739,13 @@ def __init__(self, *args, **kwargs): | |
class Meta: | ||
managed = True | ||
db_table = "node_groups" | ||
constraints = [ | ||
models.CheckConstraint( | ||
condition=Q(grouping_node=models.F("pk")) | ||
| Q(grouping_node__isnull=True), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sorry to potentially rehash but it's been a bit since I've looked at this. What's the condition for a NodeGroup to exist without a Node? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Purely for convenience when creating them -- we have to create one (NodeGroup) and then the other (Node), unless we want to require that everybody create them in a single transaction and then run this as a deferred constraint, but that's a breaking change I didn't want to impose. There's a validation check in manage.py validate that checks for nodeless nodegroups. Apparently we're still creating some on setup_db:
|
||
name="grouping_node_matches_pk_or_null", | ||
) | ||
] | ||
|
||
default_permissions = () | ||
permissions = ( | ||
|
@@ -874,15 +894,18 @@ def __init__(self, *args, **kwargs): | |
def clean(self): | ||
if not self.alias: | ||
Graph.objects.get(pk=self.graph_id).create_node_alias(self) | ||
if self.pk == self.source_identifier_id: | ||
chrabyrd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.source_identifier_id = None | ||
|
||
def save(self, **kwargs): | ||
if not self.alias: | ||
self.clean() | ||
add_to_update_fields(kwargs, "alias") | ||
add_to_update_fields(kwargs, "hascustomalias") | ||
if self.pk == self.source_identifier_id: | ||
self.source_identifier_id = None | ||
chrabyrd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
add_to_update_fields(kwargs, "source_identifier_id") | ||
|
||
self.clean() | ||
|
||
super(Node, self).save() | ||
|
||
class Meta: | ||
|
@@ -895,6 +918,10 @@ class Meta: | |
models.UniqueConstraint( | ||
fields=["alias", "graph"], name="unique_alias_graph" | ||
), | ||
models.CheckConstraint( | ||
condition=Q(istopnode=True) | Q(nodegroup__isnull=False), | ||
name="has_nodegroup_or_istopnode", | ||
), | ||
] | ||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the error that I am seeing is caused here. The "grouping_node_id" is getting assigned back to the node. Shouldn't it be assigned to the nodegroup?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, should have retested this. Fixed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, that did the trick!
One other thing. I noticed that if I create a new graph and then create a new node, my new node does not have the groupingnodeid assigned. That may be fine because it's a nodegroup of 1, so there really isn't a group to be concerned about. However, if that first node is semantic, and I add another node, then the first semantic node still doesn't have a groupingnodeid. This seems like it could be an issue because it really does have a child node in its group.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ouch, taking a look.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
383bfb2