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 ALTER ROLE edgedb refer to admin if edgedb doesn't exist #8048

Merged
merged 1 commit into from
Nov 28, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 56 additions & 1 deletion edb/schema/roles.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@


from __future__ import annotations
from typing import Optional, Type, List, TYPE_CHECKING
from typing import Optional, Type, List, Union, overload, TYPE_CHECKING

from edgedb import scram

Expand All @@ -30,6 +30,7 @@
from . import annos as s_anno
from . import delta as sd
from . import inheriting
from . import name as sn
from . import objects as so
from . import utils

Expand Down Expand Up @@ -195,6 +196,60 @@ class RenameRole(RoleCommand, sd.RenameObject[Role]):
class AlterRole(RoleCommand, inheriting.AlterInheritingObject[Role]):
astnode = qlast.AlterRole

@overload
def get_object(
self,
schema: s_schema.Schema,
context: sd.CommandContext,
*,
name: Optional[sn.Name] = None,
default: Union[Role, so.NoDefaultT] = so.NoDefault,
sourcectx: Optional[qlast.Span] = None,
) -> Role:
...

@overload
def get_object(
self,
schema: s_schema.Schema,
context: sd.CommandContext,
*,
name: Optional[sn.Name] = None,
default: None = None,
sourcectx: Optional[qlast.Span] = None,
) -> Optional[Role]:
...

def get_object(
self,
schema: s_schema.Schema,
context: sd.CommandContext,
*,
name: Optional[sn.Name] = None,
default: Union[Role, so.NoDefaultT, None] = so.NoDefault,
sourcectx: Optional[qlast.Span] = None,
) -> Optional[Role]:
# On an ALTER ROLE edgedb, if 'edgedb' doesn't exist, fall
# back to 'admin'. This mirrors what we do for login and
# avoids breaking setup scripts.
if name is None and str(self.classname) == 'edgedb':
try:
return super().get_object(
schema,
context,
sourcectx=sourcectx,
)
except errors.InvalidReferenceError:
name = sn.UnqualName('admin')

return super().get_object(
schema,
context,
name=name,
default=default,
sourcectx=sourcectx,
)

@classmethod
def _cmd_tree_from_ast(
cls,
Expand Down
13 changes: 13 additions & 0 deletions tests/test_server_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,19 @@ async def test_server_only_bootstraps_once(self):
finally:
await con.aclose()

async def test_server_alter_role_fallback(self):
with tempfile.TemporaryDirectory() as temp_dir:
async with tb.start_edgedb_server(
data_dir=temp_dir,
default_auth_method=args.ServerAuthMethod.Scram,
bootstrap_command='ALTER ROLE edgedb SET password := "first";'
) as sd:
con = await sd.connect(password='first')
try:
await con.query_single('SELECT 1')
finally:
await con.aclose()

async def test_server_ops_bogus_bind_addr_in_mix(self):
async with tb.start_edgedb_server(
bind_addrs=('host.invalid', '127.0.0.1',),
Expand Down