Repository: edgedb/edgedb

Issue:
Migration to drop a property fails if the property is used implicitly (via default) in a trigger.
<!-- Please search existing issues to avoid creating duplicates. -->

<!--
For the EdgeDB Version: run `edgedb query 'select sys::get_version_as_str()'` from your project directory (or run `select sys::get_version_as_str();` in the EdgeDB interactive shell).
For the EdgeDB CLI Version: Run `edgedb --version` from anywhere
-->

- EdgeDB Version: 3.0-dev.7435+bb3b78c
- EdgeDB CLI Version: 3.0.0-dev.968+8eef530
- OS Version: Ubuntu

Steps to Reproduce:

1. Apply the starting schema
   ```
   module default {
       type Log {
           body: str;
   
           timestamp: datetime {
               default := datetime_current();
           }
       }
   
       type Person {
           required name: str;
   
           trigger log_delete after insert for each do (
               insert Log {
                   body := __new__.name,
               }
           );
       }
   }
   ```
2. Remove `timestamp` from the `Log`.
   ```
   module default {
       type Log {
           body: str;
       }
   
       type Person {
           required name: str;
   
           trigger log_delete after insert for each do (
               insert Log {
                   body := __new__.name,
               }
           );
       }
   }
   ```
3. Try to `edgedb migration create` for the new schema.

<!-- If the issue is about a query error, please also provide your schema -->

```
$ edgedb migration create
did you drop property 'timestamp' of object type 'default::Log'? [y,n,l,c,b,s,q,?]
> y
edgedb error: SchemaError: cannot drop property 'timestamp' of object type 'default::Log' because other objects in the schema depend on it
  Detail: trigger '__::log_delete' of object type 'default::Person' depends on timestamp
Rolling back last operation...
did you drop property 'timestamp' of object type 'default::Log'? [y,n,l,c,b,s,q,?]
> n
edgedb error: EdgeDB could not resolve migration with the provided answers. Please retry with different answers.
```

Notice that the trigger expression doesn't mention the `timestamp` property, so it would not be obvious to a developer what the problem is. A workaround right now is to first drop the offending trigger together with the property, then re-create the dropped trigger in 2 migrations.


Discussion:

Resolving patch:
diff --git a/edb/schema/links.py b/edb/schema/links.py
--- a/edb/schema/links.py
+++ b/edb/schema/links.py
@@ -713,23 +713,6 @@ class DeleteLink(
     astnode = [qlast.DropConcreteLink, qlast.DropLink]
     referenced_astnode = qlast.DropConcreteLink
 
-    def _delete_begin(
-        self,
-        schema: s_schema.Schema,
-        context: sd.CommandContext,
-    ) -> s_schema.Schema:
-        schema = super()._delete_begin(schema, context)
-        if not context.canonical:
-            # We need to do a propagate here, too, since there could
-            # be backrefs to this link that technically reference
-            # us but will be fine if it is deleted.
-            schema = self._propagate_if_expr_refs(
-                schema,
-                context,
-                action=self.get_friendly_description(schema=schema),
-            )
-        return schema
-
     def _get_ast(
         self,
         schema: s_schema.Schema,
diff --git a/edb/schema/pointers.py b/edb/schema/pointers.py
--- a/edb/schema/pointers.py
+++ b/edb/schema/pointers.py
@@ -2219,6 +2219,16 @@ def _delete_begin(
         ):
             self.add_caused(del_cmd)
 
+        if not context.canonical:
+            # We need to do a propagate here, too, since there could
+            # be backrefs to this pointer that technically reference
+            # us but will be fine if it is deleted.
+            schema = self._propagate_if_expr_refs(
+                schema,
+                context,
+                action=self.get_friendly_description(schema=schema),
+            )
+
         return schema
 
     def _canonicalize(


Test patch:
diff --git a/tests/test_edgeql_data_migration.py b/tests/test_edgeql_data_migration.py
--- a/tests/test_edgeql_data_migration.py
+++ b/tests/test_edgeql_data_migration.py
@@ -11457,6 +11457,36 @@ async def test_edgeql_migration_backlink_overloaded(self):
             }
         ''')
 
+    async def test_edgeql_migration_property_ref(self):
+        await self.migrate(r'''
+            type Log {
+                body: str;
+                timestamp: datetime {
+                    default := datetime_current();
+                }
+            }
+
+            type Person {
+                required name: str;
+                trigger log_delete after insert for each do (
+                    insert Log { body := __new__.name }
+                );
+            }
+        ''')
+
+        await self.migrate(r'''
+            type Log {
+                body: str;
+            }
+
+            type Person {
+                required name: str;
+                trigger log_delete after insert for each do (
+                    insert Log { body := __new__.name }
+                );
+            }
+        ''')
+
     async def test_edgeql_migration_to_computed_drop_exclusive(self):
         await self.migrate(r'''
             type User {