Skip to content
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

Make using globals as defaults work #6153

Merged
merged 1 commit into from
Sep 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 13 additions & 3 deletions edb/schema/pointers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1766,13 +1766,23 @@ def validate_object(
schema, context, default_expr.irast.expr)

source_context = self.get_attribute_source_context('default')
default_schema = default_expr.irast.schema
default_type = default_expr.irast.stype
ir = default_expr.irast
default_schema = ir.schema
default_type = ir.stype
assert default_type is not None
ptr_target = scls.get_target(schema)
assert ptr_target is not None

if default_type.is_view(default_schema):
if (
default_type.is_view(default_schema)
# Using an alias/global always creates a new subtype view,
# but we want to allow those here, so check whether there
# is a shape more directly.
and not (
len(shape := ir.view_shapes.get(default_type, [])) == 1
and shape[0].is_id_pointer(default_schema)
)
):
raise errors.SchemaDefinitionError(
f'default expression may not include a shape',
context=source_context,
Expand Down
62 changes: 62 additions & 0 deletions tests/test_edgeql_ddl.py
Original file line number Diff line number Diff line change
Expand Up @@ -6946,6 +6946,68 @@ async def test_edgeql_ddl_global_08(self):
alter global foo set type str using ('lol');
""")

async def test_edgeql_ddl_global_09(self):
await self.con.execute('''
create type Org;

create global current_org_id -> uuid;
create global current_org := (
select Org filter .id = global current_org_id
);

create type Widget {
create required link org -> Org {
set default := global current_org;
}
};
''')

obj = await self.con.query_single('insert Org')
await self.con.execute(
'''
set global current_org_id := <uuid>$0
''',
obj.id,
)
await self.con.execute('insert Widget')
await self.assert_query_result(
'''
select Widget { org }
''',
[{'org': {'id': obj.id}}],
)

async def test_edgeql_ddl_global_10(self):
await self.con.execute('''
create type Org;

create global current_org_id -> uuid;
create global current_org := (
select Org filter .id = global current_org_id
);

create type Widget {
create required link org -> Org {
create rewrite insert using (global current_org)
}
};
''')

obj = await self.con.query_single('insert Org')
await self.con.execute(
'''
set global current_org_id := <uuid>$0
''',
obj.id,
)
await self.con.execute('insert Widget')
await self.assert_query_result(
'''
select Widget { org }
''',
[{'org': {'id': obj.id}}],
)

async def test_edgeql_ddl_global_default(self):
await self.con.execute('''
create global foo -> str;
Expand Down