Repository: edgedb/edgedb

Issue:
`start migration to` ISEs if it references a pre-existing type without defining it
Have schema
```
type Foo;
```

```
start migration to {
    type Bar { link foo: Foo }
}
```

Receive

```
edgedb error: InternalServerError: reference to an undefined item default::Bar in default::Foo@bar
  Hint: This is most likely a bug in EdgeDB. Please consider opening an issue ticket at https://github.com/edgedb/edgedb/issues/new?template=bug_report.md
  Server traceback:
      Traceback (most recent call last):
        File "/Users/efaust/dev/edgedb/edb/server/compiler_pool/worker_proc.py", line 55, in worker
          res = meth(*args)
        File "/Users/efaust/dev/edgedb/edb/server/compiler_pool/worker.py", line 161, in compile
          units, cstate = COMPILER.compile(
        File "/Users/efaust/dev/edgedb/edb/server/compiler/compiler.py", line 751, in compile
          unit_group = compile(ctx=ctx, source=source)
        File "/Users/efaust/dev/edgedb/edb/server/compiler/compiler.py", line 1935, in compile
          return _try_compile(ctx=ctx, source=source)
        File "/Users/efaust/dev/edgedb/edb/server/compiler/compiler.py", line 2003, in _try_compile
          comp, capabilities = _compile_dispatch_ql(
        File "/Users/efaust/dev/edgedb/edb/server/compiler/compiler.py", line 1842, in _compile_dispatch_ql
          query = ddl.compile_dispatch_ql_migration(
        File "/Users/efaust/dev/edgedb/edb/server/compiler/ddl.py", line 381, in compile_dispatch_ql_migration
          return _start_migration(ctx, ql, in_script)
        File "/Users/efaust/dev/edgedb/edb/server/compiler/ddl.py", line 462, in _start_migration
          target_schema = s_ddl.apply_sdl(
        File "/Users/efaust/dev/edgedb/edb/schema/ddl.py", line 451, in apply_sdl
          ddl_stmts = s_decl.sdl_to_ddl(current_schema, documents)
        File "/Users/efaust/dev/edgedb/edb/edgeql/declarative.py", line 458, in sdl_to_ddl
          ordered = topological.sort(ddlgraph, allow_unresolved=False)
        File "/Users/efaust/dev/edgedb/edb/common/topological.py", line 197, in sort
          items = sort_ex(graph, allow_unresolved=allow_unresolved)
        File "/Users/efaust/dev/edgedb/edb/common/topological.py", line 121, in sort_ex
          raise UnresolvedReferenceError(
      edb.common.topological.UnresolvedReferenceError: reference to an undefined item default::Far in default::Bar@foo
```


Discussion:

Resolving patch:
diff --git a/edb/schema/ddl.py b/edb/schema/ddl.py
--- a/edb/schema/ddl.py
+++ b/edb/schema/ddl.py
@@ -448,7 +448,7 @@ def collect(
     for decl in sdl_document.declarations:
         collect(decl, None)
 
-    ddl_stmts = s_decl.sdl_to_ddl(current_schema, documents)
+    ddl_stmts = s_decl.sdl_to_ddl(base_schema, documents)
     context = sd.CommandContext(
         modaliases={},
         schema=base_schema,


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
@@ -12117,6 +12117,18 @@ async def test_edgeql_migration_rewrite_06(self):
             {'script': 'CREATE TYPE default::A;\nCREATE TYPE default::B;'},
         ])
 
+    async def test_edgeql_migration_preexisting_01(self):
+        await self.con.execute("create type Foo;")
+        with self.assertRaisesRegex(
+            edgedb.InvalidReferenceError,
+            r"type 'default::Foo' does not exist"
+        ):
+            await self.start_migration("""
+                type Bar {
+                    baz: Foo
+                }
+            """, module='default')
+
 
 class TestEdgeQLMigrationRewriteNonisolated(TestEdgeQLMigrationRewrite):
     TRANSACTION_ISOLATION = False