From 9dbff5f575a7eb1bafe70b02bd714b14c93468bd Mon Sep 17 00:00:00 2001 From: dnwpark Date: Fri, 23 Aug 2024 17:18:54 -0400 Subject: [PATCH] Add check for deletion policies in properties. (#7675) --- edb/schema/properties.py | 24 ++++++++++++++++++------ tests/test_schema.py | 24 +++++++++++++++++++++++- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/edb/schema/properties.py b/edb/schema/properties.py index b104707ad12..38a7b47ca01 100644 --- a/edb/schema/properties.py +++ b/edb/schema/properties.py @@ -277,15 +277,27 @@ def validate_object( span=span, ) - def _check_linkprop_errors(self, node: qlast.DDLOperation) -> None: + def _check_field_errors(self, node: qlast.DDLOperation) -> None: for sub in node.commands: # do not allow link property on properties if isinstance(sub, qlast.CreateConcretePointer): raise errors.InvalidDefinitionError( - f'cannot create a link property on a property', + f'cannot place a link property on a property', span=node.span, - hint='Link properties can only be created on links, whose ' - 'target types are object types.', + hint=( + 'Link properties can only be placed on links, whose ' + 'target types are object types.' + ), + ) + # do not allow on source/target delete on properties + if isinstance(sub, (qlast.OnSourceDelete, qlast.OnTargetDelete)): + raise errors.InvalidDefinitionError( + f'cannot place a deletion policy on a property', + span=node.span, + hint=( + 'Deletion policies can only be placed on links, whose ' + 'target types are object types.' + ), ) @@ -310,7 +322,7 @@ def _cmd_tree_from_ast( if isinstance(astnode, qlast.CreateConcreteProperty): assert isinstance(cmd, PropertyCommand) cmd._process_create_or_alter_ast(schema, astnode, context) - cmd._check_linkprop_errors(astnode) + cmd._check_field_errors(astnode) return cmd @@ -433,7 +445,7 @@ def _cmd_tree_from_ast( cmd._process_create_or_alter_ast(schema, astnode, context) else: cmd._process_alter_ast(schema, astnode, context) - cmd._check_linkprop_errors(astnode) + cmd._check_field_errors(astnode) return cmd def _apply_field_ast( diff --git a/tests/test_schema.py b/tests/test_schema.py index 07bd864bba0..8f1735f27e6 100644 --- a/tests/test_schema.py +++ b/tests/test_schema.py @@ -422,7 +422,7 @@ def test_schema_bad_link_06(self): """ @tb.must_fail(errors.InvalidDefinitionError, - "cannot create a link property on a property") + "cannot place a link property on a property") def test_schema_link_prop_on_prop_01(self): """ type Test1 { @@ -432,6 +432,28 @@ def test_schema_link_prop_on_prop_01(self): }; """ + @tb.must_fail(errors.InvalidDefinitionError, + "cannot place a deletion policy on a property") + def test_schema_deletion_policy_on_prop_01(self): + """ + type Test1 { + title : str { + on source delete allow; + } + }; + """ + + @tb.must_fail(errors.InvalidDefinitionError, + "cannot place a deletion policy on a property") + def test_schema_deletion_policy_on_prop_02(self): + """ + type Test1 { + title : str { + on target delete restrict; + } + }; + """ + @tb.must_fail(errors.QueryError, "could not resolve partial path") def test_schema_partial_path_in_default_of_link_prop_01(self):