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
6 changes: 6 additions & 0 deletions edgedb/base_client.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -118,6 +118,10 @@ def remove_log_listener(
def dbname(self) -> str:
return self._params.database

@property
def branch(self) -> str:
return self._params.branch

@abc.abstractmethod
def is_closed(self) -> bool:
...
Expand DownExpand Up@@ -679,6 +683,7 @@ def __init__(
password: str = None,
secret_key: str = None,
database: str = None,
branch: str = None,
tls_ca: str = None,
tls_ca_file: str = None,
tls_security: str = None,
Expand All@@ -697,6 +702,7 @@ def __init__(
"password": password,
"secret_key": secret_key,
"database": database,
"branch": branch,
"timeout": timeout,
"tls_ca": tls_ca,
"tls_ca_file": tls_ca_file,
Expand Down
133 changes: 125 additions & 8 deletions edgedb/con_utils.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -181,9 +181,15 @@ class ResolvedConnectConfig:
_port = None
_port_source = None

# We keep track of database and branch separately, because we want to make
# sure that all the configuration is consistent and uses one or the other
# exclusively.
_database = None
_database_source = None

_branch = None
_branch_source = None

_user = None
_user_source = None

Expand DownExpand Up@@ -226,6 +232,9 @@ def set_port(self, port, source):
def set_database(self, database, source):
self._set_param('database', database, source, _validate_database)

def set_branch(self, branch, source):
self._set_param('branch', branch, source, _validate_branch)

def set_user(self, user, source):
self._set_param('user', user, source, _validate_user)

Expand DownExpand Up@@ -268,9 +277,24 @@ def address(self):
self._port if self._port else 5656
)

# The properties actually merge database and branch, but "default" is
# different. If you need to know the underlying config use the _database
# and _branch.
@property
def database(self):
return self._database if self._database else 'edgedb'
return (
self._database if self._database else
self._branch if self._branch else
'edgedb'
)

@property
def branch(self):
return (
self._database if self._database else
self._branch if self._branch else
'__default__'
)

@property
def user(self):
Expand DownExpand Up@@ -391,6 +415,12 @@ def _validate_database(database):
return database


def _validate_branch(branch):
if branch == '':
raise ValueError(f'invalid branch name: {branch}')
return branch


def _validate_user(user):
if user == '':
raise ValueError(f'invalid user name: {user}')
Expand DownExpand Up@@ -521,6 +551,7 @@ def _parse_connect_dsn_and_args(
password,
secret_key,
database,
branch,
tls_ca,
tls_ca_file,
tls_security,
Expand DownExpand Up@@ -557,6 +588,10 @@ def _parse_connect_dsn_and_args(
(database, '"database" option')
if database is not None else None
),
branch=(
(branch, '"branch" option')
if branch is not None else None
),
user=(user, '"user" option') if user is not None else None,
password=(
(password, '"password" option')
Expand DownExpand Up@@ -604,6 +639,7 @@ def _parse_connect_dsn_and_args(
env_credentials_file = os.getenv('EDGEDB_CREDENTIALS_FILE')
env_host = os.getenv('EDGEDB_HOST')
env_database = os.getenv('EDGEDB_DATABASE')
env_branch = os.getenv('EDGEDB_BRANCH')
env_user = os.getenv('EDGEDB_USER')
env_password = os.getenv('EDGEDB_PASSWORD')
env_secret_key = os.getenv('EDGEDB_SECRET_KEY')
Expand DownExpand Up@@ -643,6 +679,10 @@ def _parse_connect_dsn_and_args(
(env_database, '"EDGEDB_DATABASE" environment variable')
if env_database is not None else None
),
branch=(
(env_branch, '"EDGEDB_BRANCH" environment variable')
if env_branch is not None else None
),
user=(
(env_user, '"EDGEDB_USER" environment variable')
if env_user is not None else None
Expand DownExpand Up@@ -818,11 +858,52 @@ def handle_dsn_part(
def strip_leading_slash(str):
return str[1:] if str.startswith('/') else str

handle_dsn_part(
'database', strip_leading_slash(database),
resolved_config._database, resolved_config.set_database,
strip_leading_slash
)
if (
'branch' in query or
'branch_env' in query or
'branch_file' in query
):
if (
'database' in query or
'database_env' in query or
'database_file' in query
):
raise ValueError(
f"invalid DSN: `database` and `branch` cannot be present "
f"at the same time"
)
if resolved_config._database is not None:
raise errors.ClientConnectionError(
f"`branch` in DSN and {resolved_config._database_source} "
f"are mutually exclusive"
)
handle_dsn_part(
'branch', strip_leading_slash(database),
resolved_config._branch, resolved_config.set_branch,
strip_leading_slash
)
else:
if resolved_config._branch is not None:
if (
'database' in query or
'database_env' in query or
'database_file' in query
):
raise errors.ClientConnectionError(
f"`database` in DSN and {resolved_config._branch_source} "
f"are mutually exclusive"
)
handle_dsn_part(
'branch', strip_leading_slash(database),
resolved_config._branch, resolved_config.set_branch,
strip_leading_slash
)
else:
handle_dsn_part(
'database', strip_leading_slash(database),
resolved_config._database, resolved_config.set_database,
strip_leading_slash
)

handle_dsn_part(
'user', user, resolved_config._user, resolved_config.set_user
Expand DownExpand Up@@ -929,6 +1010,7 @@ def _resolve_config_options(
host=None,
port=None,
database=None,
branch=None,
user=None,
password=None,
secret_key=None,
Expand All@@ -940,7 +1022,23 @@ def _resolve_config_options(
cloud_profile=None,
):
if database is not None:
if branch is not None:
raise errors.ClientConnectionError(
f"{database[1]} and {branch[1]} are mutually exclusive"
)
if resolved_config._branch is not None:
raise errors.ClientConnectionError(
f"{database[1]} and {resolved_config._branch_source} are "
f"mutually exclusive"
)
resolved_config.set_database(*database)
if branch is not None:
if resolved_config._database is not None:
raise errors.ClientConnectionError(
f"{resolved_config._database_source} and {branch[1]} are "
f"mutually exclusive"
)
resolved_config.set_branch(*branch)
if user is not None:
resolved_config.set_user(*user)
if password is not None:
Expand All@@ -950,7 +1048,8 @@ def _resolve_config_options(
if tls_ca_file is not None:
if tls_ca is not None:
raise errors.ClientConnectionError(
f"{tls_ca[1]} and {tls_ca_file[1]} are mutually exclusive")
f"{tls_ca[1]} and {tls_ca_file[1]} are mutually exclusive"
)
resolved_config.set_tls_ca_file(*tls_ca_file)
if tls_ca is not None:
resolved_config.set_tls_ca_data(*tls_ca)
Expand DownExpand Up@@ -1018,7 +1117,23 @@ def _resolve_config_options(

resolved_config.set_host(creds.get('host'), source)
resolved_config.set_port(creds.get('port'), source)
resolved_config.set_database(creds.get('database'), source)
# We know that credentials have been validated, but they might be
# inconsistent with other resolved config settings.
if 'database' in creds:
if resolved_config._branch is not None:
raise errors.ClientConnectionError(
f"`branch` in configuration and `database` "
f"in credentials are mutually exclusive"
)
resolved_config.set_database(creds.get('database'), source)

elif 'branch' in creds:
if resolved_config._database is not None:
raise errors.ClientConnectionError(
f"`database` in configuration and `branch` "
f"in credentials are mutually exclusive"
)
resolved_config.set_branch(creds.get('branch'), source)
resolved_config.set_user(creds.get('user'), source)
resolved_config.set_password(creds.get('password'), source)
resolved_config.set_tls_ca_data(creds.get('tls_ca'), source)
Expand DownExpand Up@@ -1068,6 +1183,7 @@ def parse_connect_arguments(
credentials,
credentials_file,
database,
branch,
user,
password,
secret_key,
Expand DownExpand Up@@ -1100,6 +1216,7 @@ def parse_connect_arguments(
credentials=credentials,
credentials_file=credentials_file,
database=database,
branch=branch,
user=user,
password=password,
secret_key=secret_key,
Expand Down
11 changes: 11 additions & 0 deletions edgedb/credentials.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,7 +14,9 @@ class RequiredCredentials(typing.TypedDict, total=True):
class Credentials(RequiredCredentials, total=False):
host: typing.Optional[str]
password: typing.Optional[str]
# Either database or branch may appear in credentials, but not both.
database: typing.Optional[str]
branch: typing.Optional[str]
tls_ca: typing.Optional[str]
tls_security: typing.Optional[str]

Expand DownExpand Up@@ -64,6 +66,15 @@ def validate_credentials(data: dict) -> Credentials:
raise ValueError("`database` must be a string")
result['database'] = database

branch = data.get('branch')
if branch is not None:
if not isinstance(branch, str):
raise ValueError("`branch` must be a string")
if database is not None:
raise ValueError(
f"`database` and `branch` cannot both be set")
result['branch'] = branch

password = data.get('password')
if password is not None:
if not isinstance(password, str):
Expand Down
2 changes: 1 addition & 1 deletion tests/shared-client-testcases
31 changes: 30 additions & 1 deletion tests/test_con_utils.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -120,6 +120,7 @@ def run_testcase(self, testcase):
host = opts.get('host')
port = opts.get('port')
database = opts.get('database')
branch = opts.get('branch')
user = opts.get('user')
password = opts.get('password')
secret_key = opts.get('secretKey')
Expand DownExpand Up@@ -233,6 +234,7 @@ def mocked_open(filepath, *args, **kwargs):
credentials=credentials,
credentials_file=credentials_file,
database=database,
branch=branch,
user=user,
password=password,
secret_key=secret_key,
Expand All@@ -250,6 +252,7 @@ def mocked_open(filepath, *args, **kwargs):
connect_config.address[0], connect_config.address[1]
],
'database': connect_config.database,
'branch': connect_config.branch,
'user': connect_config.user,
'password': connect_config.password,
'secretKey': connect_config.secret_key,
Expand DownExpand Up@@ -289,7 +292,7 @@ def test_test_connect_params_environ(self):
if key in os.environ:
del os.environ[key]

def test_test_connect_params_run_testcase(self):
def test_test_connect_params_run_testcase_01(self):
with self.environ(EDGEDB_PORT='777'):
self.run_testcase({
'env': {
Expand All@@ -301,6 +304,31 @@ def test_test_connect_params_run_testcase(self):
'result': {
'address': ['abc', 5656],
'database': 'edgedb',
'branch': '__default__',
'user': '__test__',
'password': None,
'secretKey': None,
'tlsCAData': None,
'tlsSecurity': 'strict',
'serverSettings': {},
'waitUntilAvailable': 30,
},
})

def test_test_connect_params_run_testcase_02(self):
with self.environ(EDGEDB_PORT='777'):
self.run_testcase({
'env': {
'EDGEDB_HOST': 'abc'
},
'opts': {
'user': '__test__',
'branch': 'new_branch',
},
'result': {
'address': ['abc', 5656],
'database': 'new_branch',
'branch': 'new_branch',
'user': '__test__',
'password': None,
'secretKey': None,
Expand DownExpand Up@@ -399,6 +427,7 @@ def test_project_config(self):
password=None,
secret_key=None,
database=None,
branch=None,
tls_ca=None,
tls_ca_file=None,
tls_security=None,
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content
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
6 changes: 6 additions & 0 deletions edgedb/base_client.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -118,6 +118,10 @@ def remove_log_listener(
def dbname(self) -> str:
return self._params.database

@property
def branch(self) -> str:
return self._params.branch

@abc.abstractmethod
def is_closed(self) -> bool:
...
Expand DownExpand Up@@ -679,6 +683,7 @@ def __init__(
password: str = None,
secret_key: str = None,
database: str = None,
branch: str = None,
tls_ca: str = None,
tls_ca_file: str = None,
tls_security: str = None,
Expand All@@ -697,6 +702,7 @@ def __init__(
"password": password,
"secret_key": secret_key,
"database": database,
"branch": branch,
"timeout": timeout,
"tls_ca": tls_ca,
"tls_ca_file": tls_ca_file,
Expand Down
133 changes: 125 additions & 8 deletions edgedb/con_utils.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -181,9 +181,15 @@ class ResolvedConnectConfig:
_port = None
_port_source = None

# We keep track of database and branch separately, because we want to make
# sure that all the configuration is consistent and uses one or the other
# exclusively.
_database = None
_database_source = None

_branch = None
_branch_source = None

_user = None
_user_source = None

Expand DownExpand Up@@ -226,6 +232,9 @@ def set_port(self, port, source):
def set_database(self, database, source):
self._set_param('database', database, source, _validate_database)

def set_branch(self, branch, source):
self._set_param('branch', branch, source, _validate_branch)

def set_user(self, user, source):
self._set_param('user', user, source, _validate_user)

Expand DownExpand Up@@ -268,9 +277,24 @@ def address(self):
self._port if self._port else 5656
)

# The properties actually merge database and branch, but "default" is
# different. If you need to know the underlying config use the _database
# and _branch.
@property
def database(self):
return self._database if self._database else 'edgedb'
return (
self._database if self._database else
self._branch if self._branch else
'edgedb'
)

@property
def branch(self):
return (
self._database if self._database else
self._branch if self._branch else
'__default__'
)

@property
def user(self):
Expand DownExpand Up@@ -391,6 +415,12 @@ def _validate_database(database):
return database


def _validate_branch(branch):
if branch == '':
raise ValueError(f'invalid branch name: {branch}')
return branch


def _validate_user(user):
if user == '':
raise ValueError(f'invalid user name: {user}')
Expand DownExpand Up@@ -521,6 +551,7 @@ def _parse_connect_dsn_and_args(
password,
secret_key,
database,
branch,
tls_ca,
tls_ca_file,
tls_security,
Expand DownExpand Up@@ -557,6 +588,10 @@ def _parse_connect_dsn_and_args(
(database, '"database" option')
if database is not None else None
),
branch=(
(branch, '"branch" option')
if branch is not None else None
),
user=(user, '"user" option') if user is not None else None,
password=(
(password, '"password" option')
Expand DownExpand Up@@ -604,6 +639,7 @@ def _parse_connect_dsn_and_args(
env_credentials_file = os.getenv('EDGEDB_CREDENTIALS_FILE')
env_host = os.getenv('EDGEDB_HOST')
env_database = os.getenv('EDGEDB_DATABASE')
env_branch = os.getenv('EDGEDB_BRANCH')
env_user = os.getenv('EDGEDB_USER')
env_password = os.getenv('EDGEDB_PASSWORD')
env_secret_key = os.getenv('EDGEDB_SECRET_KEY')
Expand DownExpand Up@@ -643,6 +679,10 @@ def _parse_connect_dsn_and_args(
(env_database, '"EDGEDB_DATABASE" environment variable')
if env_database is not None else None
),
branch=(
(env_branch, '"EDGEDB_BRANCH" environment variable')
if env_branch is not None else None
),
user=(
(env_user, '"EDGEDB_USER" environment variable')
if env_user is not None else None
Expand DownExpand Up@@ -818,11 +858,52 @@ def handle_dsn_part(
def strip_leading_slash(str):
return str[1:] if str.startswith('/') else str

handle_dsn_part(
'database', strip_leading_slash(database),
resolved_config._database, resolved_config.set_database,
strip_leading_slash
)
if (
'branch' in query or
'branch_env' in query or
'branch_file' in query
):
if (
'database' in query or
'database_env' in query or
'database_file' in query
):
raise ValueError(
f"invalid DSN: `database` and `branch` cannot be present "
f"at the same time"
)
if resolved_config._database is not None:
raise errors.ClientConnectionError(
f"`branch` in DSN and {resolved_config._database_source} "
f"are mutually exclusive"
)
handle_dsn_part(
'branch', strip_leading_slash(database),
resolved_config._branch, resolved_config.set_branch,
strip_leading_slash
)
else:
if resolved_config._branch is not None:
if (
'database' in query or
'database_env' in query or
'database_file' in query
):
raise errors.ClientConnectionError(
f"`database` in DSN and {resolved_config._branch_source} "
f"are mutually exclusive"
)
handle_dsn_part(
'branch', strip_leading_slash(database),
resolved_config._branch, resolved_config.set_branch,
strip_leading_slash
)
else:
handle_dsn_part(
'database', strip_leading_slash(database),
resolved_config._database, resolved_config.set_database,
strip_leading_slash
)

handle_dsn_part(
'user', user, resolved_config._user, resolved_config.set_user
Expand DownExpand Up@@ -929,6 +1010,7 @@ def _resolve_config_options(
host=None,
port=None,
database=None,
branch=None,
user=None,
password=None,
secret_key=None,
Expand All@@ -940,7 +1022,23 @@ def _resolve_config_options(
cloud_profile=None,
):
if database is not None:
if branch is not None:
raise errors.ClientConnectionError(
f"{database[1]} and {branch[1]} are mutually exclusive"
)
if resolved_config._branch is not None:
raise errors.ClientConnectionError(
f"{database[1]} and {resolved_config._branch_source} are "
f"mutually exclusive"
)
resolved_config.set_database(*database)
if branch is not None:
if resolved_config._database is not None:
raise errors.ClientConnectionError(
f"{resolved_config._database_source} and {branch[1]} are "
f"mutually exclusive"
)
resolved_config.set_branch(*branch)
if user is not None:
resolved_config.set_user(*user)
if password is not None:
Expand All@@ -950,7 +1048,8 @@ def _resolve_config_options(
if tls_ca_file is not None:
if tls_ca is not None:
raise errors.ClientConnectionError(
f"{tls_ca[1]} and {tls_ca_file[1]} are mutually exclusive")
f"{tls_ca[1]} and {tls_ca_file[1]} are mutually exclusive"
)
resolved_config.set_tls_ca_file(*tls_ca_file)
if tls_ca is not None:
resolved_config.set_tls_ca_data(*tls_ca)
Expand DownExpand Up@@ -1018,7 +1117,23 @@ def _resolve_config_options(

resolved_config.set_host(creds.get('host'), source)
resolved_config.set_port(creds.get('port'), source)
resolved_config.set_database(creds.get('database'), source)
# We know that credentials have been validated, but they might be
# inconsistent with other resolved config settings.
if 'database' in creds:
if resolved_config._branch is not None:
raise errors.ClientConnectionError(
f"`branch` in configuration and `database` "
f"in credentials are mutually exclusive"
)
resolved_config.set_database(creds.get('database'), source)

elif 'branch' in creds:
if resolved_config._database is not None:
raise errors.ClientConnectionError(
f"`database` in configuration and `branch` "
f"in credentials are mutually exclusive"
)
resolved_config.set_branch(creds.get('branch'), source)
resolved_config.set_user(creds.get('user'), source)
resolved_config.set_password(creds.get('password'), source)
resolved_config.set_tls_ca_data(creds.get('tls_ca'), source)
Expand DownExpand Up@@ -1068,6 +1183,7 @@ def parse_connect_arguments(
credentials,
credentials_file,
database,
branch,
user,
password,
secret_key,
Expand DownExpand Up@@ -1100,6 +1216,7 @@ def parse_connect_arguments(
credentials=credentials,
credentials_file=credentials_file,
database=database,
branch=branch,
user=user,
password=password,
secret_key=secret_key,
Expand Down
11 changes: 11 additions & 0 deletions edgedb/credentials.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,7 +14,9 @@ class RequiredCredentials(typing.TypedDict, total=True):
class Credentials(RequiredCredentials, total=False):
host: typing.Optional[str]
password: typing.Optional[str]
# Either database or branch may appear in credentials, but not both.
database: typing.Optional[str]
branch: typing.Optional[str]
tls_ca: typing.Optional[str]
tls_security: typing.Optional[str]

Expand DownExpand Up@@ -64,6 +66,15 @@ def validate_credentials(data: dict) -> Credentials:
raise ValueError("`database` must be a string")
result['database'] = database

branch = data.get('branch')
if branch is not None:
if not isinstance(branch, str):
raise ValueError("`branch` must be a string")
if database is not None:
raise ValueError(
f"`database` and `branch` cannot both be set")
result['branch'] = branch

password = data.get('password')
if password is not None:
if not isinstance(password, str):
Expand Down
2 changes: 1 addition & 1 deletion tests/shared-client-testcases
31 changes: 30 additions & 1 deletion tests/test_con_utils.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -120,6 +120,7 @@ def run_testcase(self, testcase):
host = opts.get('host')
port = opts.get('port')
database = opts.get('database')
branch = opts.get('branch')
user = opts.get('user')
password = opts.get('password')
secret_key = opts.get('secretKey')
Expand DownExpand Up@@ -233,6 +234,7 @@ def mocked_open(filepath, *args, **kwargs):
credentials=credentials,
credentials_file=credentials_file,
database=database,
branch=branch,
user=user,
password=password,
secret_key=secret_key,
Expand All@@ -250,6 +252,7 @@ def mocked_open(filepath, *args, **kwargs):
connect_config.address[0], connect_config.address[1]
],
'database': connect_config.database,
'branch': connect_config.branch,
'user': connect_config.user,
'password': connect_config.password,
'secretKey': connect_config.secret_key,
Expand DownExpand Up@@ -289,7 +292,7 @@ def test_test_connect_params_environ(self):
if key in os.environ:
del os.environ[key]

def test_test_connect_params_run_testcase(self):
def test_test_connect_params_run_testcase_01(self):
with self.environ(EDGEDB_PORT='777'):
self.run_testcase({
'env': {
Expand All@@ -301,6 +304,31 @@ def test_test_connect_params_run_testcase(self):
'result': {
'address': ['abc', 5656],
'database': 'edgedb',
'branch': '__default__',
'user': '__test__',
'password': None,
'secretKey': None,
'tlsCAData': None,
'tlsSecurity': 'strict',
'serverSettings': {},
'waitUntilAvailable': 30,
},
})

def test_test_connect_params_run_testcase_02(self):
with self.environ(EDGEDB_PORT='777'):
self.run_testcase({
'env': {
'EDGEDB_HOST': 'abc'
},
'opts': {
'user': '__test__',
'branch': 'new_branch',
},
'result': {
'address': ['abc', 5656],
'database': 'new_branch',
'branch': 'new_branch',
'user': '__test__',
'password': None,
'secretKey': None,
Expand DownExpand Up@@ -399,6 +427,7 @@ def test_project_config(self):
password=None,
secret_key=None,
database=None,
branch=None,
tls_ca=None,
tls_ca_file=None,
tls_security=None,
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
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
6 changes: 6 additions & 0 deletions edgedb/base_client.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -118,6 +118,10 @@ def remove_log_listener(
def dbname(self) -> str:
return self._params.database

@property
def branch(self) -> str:
return self._params.branch

@abc.abstractmethod
def is_closed(self) -> bool:
...
Expand DownExpand Up@@ -679,6 +683,7 @@ def __init__(
password: str = None,
secret_key: str = None,
database: str = None,
branch: str = None,
tls_ca: str = None,
tls_ca_file: str = None,
tls_security: str = None,
Expand All@@ -697,6 +702,7 @@ def __init__(
"password": password,
"secret_key": secret_key,
"database": database,
"branch": branch,
"timeout": timeout,
"tls_ca": tls_ca,
"tls_ca_file": tls_ca_file,
Expand Down
133 changes: 125 additions & 8 deletions edgedb/con_utils.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -181,9 +181,15 @@ class ResolvedConnectConfig:
_port = None
_port_source = None

# We keep track of database and branch separately, because we want to make
# sure that all the configuration is consistent and uses one or the other
# exclusively.
_database = None
_database_source = None

_branch = None
_branch_source = None

_user = None
_user_source = None

Expand DownExpand Up@@ -226,6 +232,9 @@ def set_port(self, port, source):
def set_database(self, database, source):
self._set_param('database', database, source, _validate_database)

def set_branch(self, branch, source):
self._set_param('branch', branch, source, _validate_branch)

def set_user(self, user, source):
self._set_param('user', user, source, _validate_user)

Expand DownExpand Up@@ -268,9 +277,24 @@ def address(self):
self._port if self._port else 5656
)

# The properties actually merge database and branch, but "default" is
# different. If you need to know the underlying config use the _database
# and _branch.
@property
def database(self):
return self._database if self._database else 'edgedb'
return (
self._database if self._database else
self._branch if self._branch else
'edgedb'
)

@property
def branch(self):
return (
self._database if self._database else
self._branch if self._branch else
'__default__'
)

@property
def user(self):
Expand DownExpand Up@@ -391,6 +415,12 @@ def _validate_database(database):
return database


def _validate_branch(branch):
if branch == '':
raise ValueError(f'invalid branch name: {branch}')
return branch


def _validate_user(user):
if user == '':
raise ValueError(f'invalid user name: {user}')
Expand DownExpand Up@@ -521,6 +551,7 @@ def _parse_connect_dsn_and_args(
password,
secret_key,
database,
branch,
tls_ca,
tls_ca_file,
tls_security,
Expand DownExpand Up@@ -557,6 +588,10 @@ def _parse_connect_dsn_and_args(
(database, '"database" option')
if database is not None else None
),
branch=(
(branch, '"branch" option')
if branch is not None else None
),
user=(user, '"user" option') if user is not None else None,
password=(
(password, '"password" option')
Expand DownExpand Up@@ -604,6 +639,7 @@ def _parse_connect_dsn_and_args(
env_credentials_file = os.getenv('EDGEDB_CREDENTIALS_FILE')
env_host = os.getenv('EDGEDB_HOST')
env_database = os.getenv('EDGEDB_DATABASE')
env_branch = os.getenv('EDGEDB_BRANCH')
env_user = os.getenv('EDGEDB_USER')
env_password = os.getenv('EDGEDB_PASSWORD')
env_secret_key = os.getenv('EDGEDB_SECRET_KEY')
Expand DownExpand Up@@ -643,6 +679,10 @@ def _parse_connect_dsn_and_args(
(env_database, '"EDGEDB_DATABASE" environment variable')
if env_database is not None else None
),
branch=(
(env_branch, '"EDGEDB_BRANCH" environment variable')
if env_branch is not None else None
),
user=(
(env_user, '"EDGEDB_USER" environment variable')
if env_user is not None else None
Expand DownExpand Up@@ -818,11 +858,52 @@ def handle_dsn_part(
def strip_leading_slash(str):
return str[1:] if str.startswith('/') else str

handle_dsn_part(
'database', strip_leading_slash(database),
resolved_config._database, resolved_config.set_database,
strip_leading_slash
)
if (
'branch' in query or
'branch_env' in query or
'branch_file' in query
):
if (
'database' in query or
'database_env' in query or
'database_file' in query
):
raise ValueError(
f"invalid DSN: `database` and `branch` cannot be present "
f"at the same time"
)
if resolved_config._database is not None:
raise errors.ClientConnectionError(
f"`branch` in DSN and {resolved_config._database_source} "
f"are mutually exclusive"
)
handle_dsn_part(
'branch', strip_leading_slash(database),
resolved_config._branch, resolved_config.set_branch,
strip_leading_slash
)
else:
if resolved_config._branch is not None:
if (
'database' in query or
'database_env' in query or
'database_file' in query
):
raise errors.ClientConnectionError(
f"`database` in DSN and {resolved_config._branch_source} "
f"are mutually exclusive"
)
handle_dsn_part(
'branch', strip_leading_slash(database),
resolved_config._branch, resolved_config.set_branch,
strip_leading_slash
)
else:
handle_dsn_part(
'database', strip_leading_slash(database),
resolved_config._database, resolved_config.set_database,
strip_leading_slash
)

handle_dsn_part(
'user', user, resolved_config._user, resolved_config.set_user
Expand DownExpand Up@@ -929,6 +1010,7 @@ def _resolve_config_options(
host=None,
port=None,
database=None,
branch=None,
user=None,
password=None,
secret_key=None,
Expand All@@ -940,7 +1022,23 @@ def _resolve_config_options(
cloud_profile=None,
):
if database is not None:
if branch is not None:
raise errors.ClientConnectionError(
f"{database[1]} and {branch[1]} are mutually exclusive"
)
if resolved_config._branch is not None:
raise errors.ClientConnectionError(
f"{database[1]} and {resolved_config._branch_source} are "
f"mutually exclusive"
)
resolved_config.set_database(*database)
if branch is not None:
if resolved_config._database is not None:
raise errors.ClientConnectionError(
f"{resolved_config._database_source} and {branch[1]} are "
f"mutually exclusive"
)
resolved_config.set_branch(*branch)
if user is not None:
resolved_config.set_user(*user)
if password is not None:
Expand All@@ -950,7 +1048,8 @@ def _resolve_config_options(
if tls_ca_file is not None:
if tls_ca is not None:
raise errors.ClientConnectionError(
f"{tls_ca[1]} and {tls_ca_file[1]} are mutually exclusive")
f"{tls_ca[1]} and {tls_ca_file[1]} are mutually exclusive"
)
resolved_config.set_tls_ca_file(*tls_ca_file)
if tls_ca is not None:
resolved_config.set_tls_ca_data(*tls_ca)
Expand DownExpand Up@@ -1018,7 +1117,23 @@ def _resolve_config_options(

resolved_config.set_host(creds.get('host'), source)
resolved_config.set_port(creds.get('port'), source)
resolved_config.set_database(creds.get('database'), source)
# We know that credentials have been validated, but they might be
# inconsistent with other resolved config settings.
if 'database' in creds:
if resolved_config._branch is not None:
raise errors.ClientConnectionError(
f"`branch` in configuration and `database` "
f"in credentials are mutually exclusive"
)
resolved_config.set_database(creds.get('database'), source)

elif 'branch' in creds:
if resolved_config._database is not None:
raise errors.ClientConnectionError(
f"`database` in configuration and `branch` "
f"in credentials are mutually exclusive"
)
resolved_config.set_branch(creds.get('branch'), source)
resolved_config.set_user(creds.get('user'), source)
resolved_config.set_password(creds.get('password'), source)
resolved_config.set_tls_ca_data(creds.get('tls_ca'), source)
Expand DownExpand Up@@ -1068,6 +1183,7 @@ def parse_connect_arguments(
credentials,
credentials_file,
database,
branch,
user,
password,
secret_key,
Expand DownExpand Up@@ -1100,6 +1216,7 @@ def parse_connect_arguments(
credentials=credentials,
credentials_file=credentials_file,
database=database,
branch=branch,
user=user,
password=password,
secret_key=secret_key,
Expand Down
11 changes: 11 additions & 0 deletions edgedb/credentials.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,7 +14,9 @@ class RequiredCredentials(typing.TypedDict, total=True):
class Credentials(RequiredCredentials, total=False):
host: typing.Optional[str]
password: typing.Optional[str]
# Either database or branch may appear in credentials, but not both.
database: typing.Optional[str]
branch: typing.Optional[str]
tls_ca: typing.Optional[str]
tls_security: typing.Optional[str]

Expand DownExpand Up@@ -64,6 +66,15 @@ def validate_credentials(data: dict) -> Credentials:
raise ValueError("`database` must be a string")
result['database'] = database

branch = data.get('branch')
if branch is not None:
if not isinstance(branch, str):
raise ValueError("`branch` must be a string")
if database is not None:
raise ValueError(
f"`database` and `branch` cannot both be set")
result['branch'] = branch

password = data.get('password')
if password is not None:
if not isinstance(password, str):
Expand Down
2 changes: 1 addition & 1 deletion tests/shared-client-testcases
31 changes: 30 additions & 1 deletion tests/test_con_utils.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -120,6 +120,7 @@ def run_testcase(self, testcase):
host = opts.get('host')
port = opts.get('port')
database = opts.get('database')
branch = opts.get('branch')
user = opts.get('user')
password = opts.get('password')
secret_key = opts.get('secretKey')
Expand DownExpand Up@@ -233,6 +234,7 @@ def mocked_open(filepath, *args, **kwargs):
credentials=credentials,
credentials_file=credentials_file,
database=database,
branch=branch,
user=user,
password=password,
secret_key=secret_key,
Expand All@@ -250,6 +252,7 @@ def mocked_open(filepath, *args, **kwargs):
connect_config.address[0], connect_config.address[1]
],
'database': connect_config.database,
'branch': connect_config.branch,
'user': connect_config.user,
'password': connect_config.password,
'secretKey': connect_config.secret_key,
Expand DownExpand Up@@ -289,7 +292,7 @@ def test_test_connect_params_environ(self):
if key in os.environ:
del os.environ[key]

def test_test_connect_params_run_testcase(self):
def test_test_connect_params_run_testcase_01(self):
with self.environ(EDGEDB_PORT='777'):
self.run_testcase({
'env': {
Expand All@@ -301,6 +304,31 @@ def test_test_connect_params_run_testcase(self):
'result': {
'address': ['abc', 5656],
'database': 'edgedb',
'branch': '__default__',
'user': '__test__',
'password': None,
'secretKey': None,
'tlsCAData': None,
'tlsSecurity': 'strict',
'serverSettings': {},
'waitUntilAvailable': 30,
},
})

def test_test_connect_params_run_testcase_02(self):
with self.environ(EDGEDB_PORT='777'):
self.run_testcase({
'env': {
'EDGEDB_HOST': 'abc'
},
'opts': {
'user': '__test__',
'branch': 'new_branch',
},
'result': {
'address': ['abc', 5656],
'database': 'new_branch',
'branch': 'new_branch',
'user': '__test__',
'password': None,
'secretKey': None,
Expand DownExpand Up@@ -399,6 +427,7 @@ def test_project_config(self):
password=None,
secret_key=None,
database=None,
branch=None,
tls_ca=None,
tls_ca_file=None,
tls_security=None,
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
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
6 changes: 6 additions & 0 deletions edgedb/base_client.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -118,6 +118,10 @@ def remove_log_listener(
def dbname(self) -> str:
return self._params.database

@property
def branch(self) -> str:
return self._params.branch

@abc.abstractmethod
def is_closed(self) -> bool:
...
Expand DownExpand Up@@ -679,6 +683,7 @@ def __init__(
password: str = None,
secret_key: str = None,
database: str = None,
branch: str = None,
tls_ca: str = None,
tls_ca_file: str = None,
tls_security: str = None,
Expand All@@ -697,6 +702,7 @@ def __init__(
"password": password,
"secret_key": secret_key,
"database": database,
"branch": branch,
"timeout": timeout,
"tls_ca": tls_ca,
"tls_ca_file": tls_ca_file,
Expand Down
133 changes: 125 additions & 8 deletions edgedb/con_utils.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -181,9 +181,15 @@ class ResolvedConnectConfig:
_port = None
_port_source = None

# We keep track of database and branch separately, because we want to make
# sure that all the configuration is consistent and uses one or the other
# exclusively.
_database = None
_database_source = None

_branch = None
_branch_source = None

_user = None
_user_source = None

Expand DownExpand Up@@ -226,6 +232,9 @@ def set_port(self, port, source):
def set_database(self, database, source):
self._set_param('database', database, source, _validate_database)

def set_branch(self, branch, source):
self._set_param('branch', branch, source, _validate_branch)

def set_user(self, user, source):
self._set_param('user', user, source, _validate_user)

Expand DownExpand Up@@ -268,9 +277,24 @@ def address(self):
self._port if self._port else 5656
)

# The properties actually merge database and branch, but "default" is
# different. If you need to know the underlying config use the _database
# and _branch.
@property
def database(self):
return self._database if self._database else 'edgedb'
return (
self._database if self._database else
self._branch if self._branch else
'edgedb'
)

@property
def branch(self):
return (
self._database if self._database else
self._branch if self._branch else
'__default__'
)

@property
def user(self):
Expand DownExpand Up@@ -391,6 +415,12 @@ def _validate_database(database):
return database


def _validate_branch(branch):
if branch == '':
raise ValueError(f'invalid branch name: {branch}')
return branch


def _validate_user(user):
if user == '':
raise ValueError(f'invalid user name: {user}')
Expand DownExpand Up@@ -521,6 +551,7 @@ def _parse_connect_dsn_and_args(
password,
secret_key,
database,
branch,
tls_ca,
tls_ca_file,
tls_security,
Expand DownExpand Up@@ -557,6 +588,10 @@ def _parse_connect_dsn_and_args(
(database, '"database" option')
if database is not None else None
),
branch=(
(branch, '"branch" option')
if branch is not None else None
),
user=(user, '"user" option') if user is not None else None,
password=(
(password, '"password" option')
Expand DownExpand Up@@ -604,6 +639,7 @@ def _parse_connect_dsn_and_args(
env_credentials_file = os.getenv('EDGEDB_CREDENTIALS_FILE')
env_host = os.getenv('EDGEDB_HOST')
env_database = os.getenv('EDGEDB_DATABASE')
env_branch = os.getenv('EDGEDB_BRANCH')
env_user = os.getenv('EDGEDB_USER')
env_password = os.getenv('EDGEDB_PASSWORD')
env_secret_key = os.getenv('EDGEDB_SECRET_KEY')
Expand DownExpand Up@@ -643,6 +679,10 @@ def _parse_connect_dsn_and_args(
(env_database, '"EDGEDB_DATABASE" environment variable')
if env_database is not None else None
),
branch=(
(env_branch, '"EDGEDB_BRANCH" environment variable')
if env_branch is not None else None
),
user=(
(env_user, '"EDGEDB_USER" environment variable')
if env_user is not None else None
Expand DownExpand Up@@ -818,11 +858,52 @@ def handle_dsn_part(
def strip_leading_slash(str):
return str[1:] if str.startswith('/') else str

handle_dsn_part(
'database', strip_leading_slash(database),
resolved_config._database, resolved_config.set_database,
strip_leading_slash
)
if (
'branch' in query or
'branch_env' in query or
'branch_file' in query
):
if (
'database' in query or
'database_env' in query or
'database_file' in query
):
raise ValueError(
f"invalid DSN: `database` and `branch` cannot be present "
f"at the same time"
)
if resolved_config._database is not None:
raise errors.ClientConnectionError(
f"`branch` in DSN and {resolved_config._database_source} "
f"are mutually exclusive"
)
handle_dsn_part(
'branch', strip_leading_slash(database),
resolved_config._branch, resolved_config.set_branch,
strip_leading_slash
)
else:
if resolved_config._branch is not None:
if (
'database' in query or
'database_env' in query or
'database_file' in query
):
raise errors.ClientConnectionError(
f"`database` in DSN and {resolved_config._branch_source} "
f"are mutually exclusive"
)
handle_dsn_part(
'branch', strip_leading_slash(database),
resolved_config._branch, resolved_config.set_branch,
strip_leading_slash
)
else:
handle_dsn_part(
'database', strip_leading_slash(database),
resolved_config._database, resolved_config.set_database,
strip_leading_slash
)

handle_dsn_part(
'user', user, resolved_config._user, resolved_config.set_user
Expand DownExpand Up@@ -929,6 +1010,7 @@ def _resolve_config_options(
host=None,
port=None,
database=None,
branch=None,
user=None,
password=None,
secret_key=None,
Expand All@@ -940,7 +1022,23 @@ def _resolve_config_options(
cloud_profile=None,
):
if database is not None:
if branch is not None:
raise errors.ClientConnectionError(
f"{database[1]} and {branch[1]} are mutually exclusive"
)
if resolved_config._branch is not None:
raise errors.ClientConnectionError(
f"{database[1]} and {resolved_config._branch_source} are "
f"mutually exclusive"
)
resolved_config.set_database(*database)
if branch is not None:
if resolved_config._database is not None:
raise errors.ClientConnectionError(
f"{resolved_config._database_source} and {branch[1]} are "
f"mutually exclusive"
)
resolved_config.set_branch(*branch)
if user is not None:
resolved_config.set_user(*user)
if password is not None:
Expand All@@ -950,7 +1048,8 @@ def _resolve_config_options(
if tls_ca_file is not None:
if tls_ca is not None:
raise errors.ClientConnectionError(
f"{tls_ca[1]} and {tls_ca_file[1]} are mutually exclusive")
f"{tls_ca[1]} and {tls_ca_file[1]} are mutually exclusive"
)
resolved_config.set_tls_ca_file(*tls_ca_file)
if tls_ca is not None:
resolved_config.set_tls_ca_data(*tls_ca)
Expand DownExpand Up@@ -1018,7 +1117,23 @@ def _resolve_config_options(

resolved_config.set_host(creds.get('host'), source)
resolved_config.set_port(creds.get('port'), source)
resolved_config.set_database(creds.get('database'), source)
# We know that credentials have been validated, but they might be
# inconsistent with other resolved config settings.
if 'database' in creds:
if resolved_config._branch is not None:
raise errors.ClientConnectionError(
f"`branch` in configuration and `database` "
f"in credentials are mutually exclusive"
)
resolved_config.set_database(creds.get('database'), source)

elif 'branch' in creds:
if resolved_config._database is not None:
raise errors.ClientConnectionError(
f"`database` in configuration and `branch` "
f"in credentials are mutually exclusive"
)
resolved_config.set_branch(creds.get('branch'), source)
resolved_config.set_user(creds.get('user'), source)
resolved_config.set_password(creds.get('password'), source)
resolved_config.set_tls_ca_data(creds.get('tls_ca'), source)
Expand DownExpand Up@@ -1068,6 +1183,7 @@ def parse_connect_arguments(
credentials,
credentials_file,
database,
branch,
user,
password,
secret_key,
Expand DownExpand Up@@ -1100,6 +1216,7 @@ def parse_connect_arguments(
credentials=credentials,
credentials_file=credentials_file,
database=database,
branch=branch,
user=user,
password=password,
secret_key=secret_key,
Expand Down
11 changes: 11 additions & 0 deletions edgedb/credentials.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,7 +14,9 @@ class RequiredCredentials(typing.TypedDict, total=True):
class Credentials(RequiredCredentials, total=False):
host: typing.Optional[str]
password: typing.Optional[str]
# Either database or branch may appear in credentials, but not both.
database: typing.Optional[str]
branch: typing.Optional[str]
tls_ca: typing.Optional[str]
tls_security: typing.Optional[str]

Expand DownExpand Up@@ -64,6 +66,15 @@ def validate_credentials(data: dict) -> Credentials:
raise ValueError("`database` must be a string")
result['database'] = database

branch = data.get('branch')
if branch is not None:
if not isinstance(branch, str):
raise ValueError("`branch` must be a string")
if database is not None:
raise ValueError(
f"`database` and `branch` cannot both be set")
result['branch'] = branch

password = data.get('password')
if password is not None:
if not isinstance(password, str):
Expand Down
2 changes: 1 addition & 1 deletion tests/shared-client-testcases
31 changes: 30 additions & 1 deletion tests/test_con_utils.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -120,6 +120,7 @@ def run_testcase(self, testcase):
host = opts.get('host')
port = opts.get('port')
database = opts.get('database')
branch = opts.get('branch')
user = opts.get('user')
password = opts.get('password')
secret_key = opts.get('secretKey')
Expand DownExpand Up@@ -233,6 +234,7 @@ def mocked_open(filepath, *args, **kwargs):
credentials=credentials,
credentials_file=credentials_file,
database=database,
branch=branch,
user=user,
password=password,
secret_key=secret_key,
Expand All@@ -250,6 +252,7 @@ def mocked_open(filepath, *args, **kwargs):
connect_config.address[0], connect_config.address[1]
],
'database': connect_config.database,
'branch': connect_config.branch,
'user': connect_config.user,
'password': connect_config.password,
'secretKey': connect_config.secret_key,
Expand DownExpand Up@@ -289,7 +292,7 @@ def test_test_connect_params_environ(self):
if key in os.environ:
del os.environ[key]

def test_test_connect_params_run_testcase(self):
def test_test_connect_params_run_testcase_01(self):
with self.environ(EDGEDB_PORT='777'):
self.run_testcase({
'env': {
Expand All@@ -301,6 +304,31 @@ def test_test_connect_params_run_testcase(self):
'result': {
'address': ['abc', 5656],
'database': 'edgedb',
'branch': '__default__',
'user': '__test__',
'password': None,
'secretKey': None,
'tlsCAData': None,
'tlsSecurity': 'strict',
'serverSettings': {},
'waitUntilAvailable': 30,
},
})

def test_test_connect_params_run_testcase_02(self):
with self.environ(EDGEDB_PORT='777'):
self.run_testcase({
'env': {
'EDGEDB_HOST': 'abc'
},
'opts': {
'user': '__test__',
'branch': 'new_branch',
},
'result': {
'address': ['abc', 5656],
'database': 'new_branch',
'branch': 'new_branch',
'user': '__test__',
'password': None,
'secretKey': None,
Expand DownExpand Up@@ -399,6 +427,7 @@ def test_project_config(self):
password=None,
secret_key=None,
database=None,
branch=None,
tls_ca=None,
tls_ca_file=None,
tls_security=None,
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content
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
6 changes: 6 additions & 0 deletions edgedb/base_client.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -118,6 +118,10 @@ def remove_log_listener(
def dbname(self) -> str:
return self._params.database

@property
def branch(self) -> str:
return self._params.branch

@abc.abstractmethod
def is_closed(self) -> bool:
...
Expand DownExpand Up@@ -679,6 +683,7 @@ def __init__(
password: str = None,
secret_key: str = None,
database: str = None,
branch: str = None,
tls_ca: str = None,
tls_ca_file: str = None,
tls_security: str = None,
Expand All@@ -697,6 +702,7 @@ def __init__(
"password": password,
"secret_key": secret_key,
"database": database,
"branch": branch,
"timeout": timeout,
"tls_ca": tls_ca,
"tls_ca_file": tls_ca_file,
Expand Down
133 changes: 125 additions & 8 deletions edgedb/con_utils.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -181,9 +181,15 @@ class ResolvedConnectConfig:
_port = None
_port_source = None

# We keep track of database and branch separately, because we want to make
# sure that all the configuration is consistent and uses one or the other
# exclusively.
_database = None
_database_source = None

_branch = None
_branch_source = None

_user = None
_user_source = None

Expand DownExpand Up@@ -226,6 +232,9 @@ def set_port(self, port, source):
def set_database(self, database, source):
self._set_param('database', database, source, _validate_database)

def set_branch(self, branch, source):
self._set_param('branch', branch, source, _validate_branch)

def set_user(self, user, source):
self._set_param('user', user, source, _validate_user)

Expand DownExpand Up@@ -268,9 +277,24 @@ def address(self):
self._port if self._port else 5656
)

# The properties actually merge database and branch, but "default" is
# different. If you need to know the underlying config use the _database
# and _branch.
@property
def database(self):
return self._database if self._database else 'edgedb'
return (
self._database if self._database else
self._branch if self._branch else
'edgedb'
)

@property
def branch(self):
return (
self._database if self._database else
self._branch if self._branch else
'__default__'
)

@property
def user(self):
Expand DownExpand Up@@ -391,6 +415,12 @@ def _validate_database(database):
return database


def _validate_branch(branch):
if branch == '':
raise ValueError(f'invalid branch name: {branch}')
return branch


def _validate_user(user):
if user == '':
raise ValueError(f'invalid user name: {user}')
Expand DownExpand Up@@ -521,6 +551,7 @@ def _parse_connect_dsn_and_args(
password,
secret_key,
database,
branch,
tls_ca,
tls_ca_file,
tls_security,
Expand DownExpand Up@@ -557,6 +588,10 @@ def _parse_connect_dsn_and_args(
(database, '"database" option')
if database is not None else None
),
branch=(
(branch, '"branch" option')
if branch is not None else None
),
user=(user, '"user" option') if user is not None else None,
password=(
(password, '"password" option')
Expand DownExpand Up@@ -604,6 +639,7 @@ def _parse_connect_dsn_and_args(
env_credentials_file = os.getenv('EDGEDB_CREDENTIALS_FILE')
env_host = os.getenv('EDGEDB_HOST')
env_database = os.getenv('EDGEDB_DATABASE')
env_branch = os.getenv('EDGEDB_BRANCH')
env_user = os.getenv('EDGEDB_USER')
env_password = os.getenv('EDGEDB_PASSWORD')
env_secret_key = os.getenv('EDGEDB_SECRET_KEY')
Expand DownExpand Up@@ -643,6 +679,10 @@ def _parse_connect_dsn_and_args(
(env_database, '"EDGEDB_DATABASE" environment variable')
if env_database is not None else None
),
branch=(
(env_branch, '"EDGEDB_BRANCH" environment variable')
if env_branch is not None else None
),
user=(
(env_user, '"EDGEDB_USER" environment variable')
if env_user is not None else None
Expand DownExpand Up@@ -818,11 +858,52 @@ def handle_dsn_part(
def strip_leading_slash(str):
return str[1:] if str.startswith('/') else str

handle_dsn_part(
'database', strip_leading_slash(database),
resolved_config._database, resolved_config.set_database,
strip_leading_slash
)
if (
'branch' in query or
'branch_env' in query or
'branch_file' in query
):
if (
'database' in query or
'database_env' in query or
'database_file' in query
):
raise ValueError(
f"invalid DSN: `database` and `branch` cannot be present "
f"at the same time"
)
if resolved_config._database is not None:
raise errors.ClientConnectionError(
f"`branch` in DSN and {resolved_config._database_source} "
f"are mutually exclusive"
)
handle_dsn_part(
'branch', strip_leading_slash(database),
resolved_config._branch, resolved_config.set_branch,
strip_leading_slash
)
else:
if resolved_config._branch is not None:
if (
'database' in query or
'database_env' in query or
'database_file' in query
):
raise errors.ClientConnectionError(
f"`database` in DSN and {resolved_config._branch_source} "
f"are mutually exclusive"
)
handle_dsn_part(
'branch', strip_leading_slash(database),
resolved_config._branch, resolved_config.set_branch,
strip_leading_slash
)
else:
handle_dsn_part(
'database', strip_leading_slash(database),
resolved_config._database, resolved_config.set_database,
strip_leading_slash
)

handle_dsn_part(
'user', user, resolved_config._user, resolved_config.set_user
Expand DownExpand Up@@ -929,6 +1010,7 @@ def _resolve_config_options(
host=None,
port=None,
database=None,
branch=None,
user=None,
password=None,
secret_key=None,
Expand All@@ -940,7 +1022,23 @@ def _resolve_config_options(
cloud_profile=None,
):
if database is not None:
if branch is not None:
raise errors.ClientConnectionError(
f"{database[1]} and {branch[1]} are mutually exclusive"
)
if resolved_config._branch is not None:
raise errors.ClientConnectionError(
f"{database[1]} and {resolved_config._branch_source} are "
f"mutually exclusive"
)
resolved_config.set_database(*database)
if branch is not None:
if resolved_config._database is not None:
raise errors.ClientConnectionError(
f"{resolved_config._database_source} and {branch[1]} are "
f"mutually exclusive"
)
resolved_config.set_branch(*branch)
if user is not None:
resolved_config.set_user(*user)
if password is not None:
Expand All@@ -950,7 +1048,8 @@ def _resolve_config_options(
if tls_ca_file is not None:
if tls_ca is not None:
raise errors.ClientConnectionError(
f"{tls_ca[1]} and {tls_ca_file[1]} are mutually exclusive")
f"{tls_ca[1]} and {tls_ca_file[1]} are mutually exclusive"
)
resolved_config.set_tls_ca_file(*tls_ca_file)
if tls_ca is not None:
resolved_config.set_tls_ca_data(*tls_ca)
Expand DownExpand Up@@ -1018,7 +1117,23 @@ def _resolve_config_options(

resolved_config.set_host(creds.get('host'), source)
resolved_config.set_port(creds.get('port'), source)
resolved_config.set_database(creds.get('database'), source)
# We know that credentials have been validated, but they might be
# inconsistent with other resolved config settings.
if 'database' in creds:
if resolved_config._branch is not None:
raise errors.ClientConnectionError(
f"`branch` in configuration and `database` "
f"in credentials are mutually exclusive"
)
resolved_config.set_database(creds.get('database'), source)

elif 'branch' in creds:
if resolved_config._database is not None:
raise errors.ClientConnectionError(
f"`database` in configuration and `branch` "
f"in credentials are mutually exclusive"
)
resolved_config.set_branch(creds.get('branch'), source)
resolved_config.set_user(creds.get('user'), source)
resolved_config.set_password(creds.get('password'), source)
resolved_config.set_tls_ca_data(creds.get('tls_ca'), source)
Expand DownExpand Up@@ -1068,6 +1183,7 @@ def parse_connect_arguments(
credentials,
credentials_file,
database,
branch,
user,
password,
secret_key,
Expand DownExpand Up@@ -1100,6 +1216,7 @@ def parse_connect_arguments(
credentials=credentials,
credentials_file=credentials_file,
database=database,
branch=branch,
user=user,
password=password,
secret_key=secret_key,
Expand Down
11 changes: 11 additions & 0 deletions edgedb/credentials.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,7 +14,9 @@ class RequiredCredentials(typing.TypedDict, total=True):
class Credentials(RequiredCredentials, total=False):
host: typing.Optional[str]
password: typing.Optional[str]
# Either database or branch may appear in credentials, but not both.
database: typing.Optional[str]
branch: typing.Optional[str]
tls_ca: typing.Optional[str]
tls_security: typing.Optional[str]

Expand DownExpand Up@@ -64,6 +66,15 @@ def validate_credentials(data: dict) -> Credentials:
raise ValueError("`database` must be a string")
result['database'] = database

branch = data.get('branch')
if branch is not None:
if not isinstance(branch, str):
raise ValueError("`branch` must be a string")
if database is not None:
raise ValueError(
f"`database` and `branch` cannot both be set")
result['branch'] = branch

password = data.get('password')
if password is not None:
if not isinstance(password, str):
Expand Down
2 changes: 1 addition & 1 deletion tests/shared-client-testcases
31 changes: 30 additions & 1 deletion tests/test_con_utils.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -120,6 +120,7 @@ def run_testcase(self, testcase):
host = opts.get('host')
port = opts.get('port')
database = opts.get('database')
branch = opts.get('branch')
user = opts.get('user')
password = opts.get('password')
secret_key = opts.get('secretKey')
Expand DownExpand Up@@ -233,6 +234,7 @@ def mocked_open(filepath, *args, **kwargs):
credentials=credentials,
credentials_file=credentials_file,
database=database,
branch=branch,
user=user,
password=password,
secret_key=secret_key,
Expand All@@ -250,6 +252,7 @@ def mocked_open(filepath, *args, **kwargs):
connect_config.address[0], connect_config.address[1]
],
'database': connect_config.database,
'branch': connect_config.branch,
'user': connect_config.user,
'password': connect_config.password,
'secretKey': connect_config.secret_key,
Expand DownExpand Up@@ -289,7 +292,7 @@ def test_test_connect_params_environ(self):
if key in os.environ:
del os.environ[key]

def test_test_connect_params_run_testcase(self):
def test_test_connect_params_run_testcase_01(self):
with self.environ(EDGEDB_PORT='777'):
self.run_testcase({
'env': {
Expand All@@ -301,6 +304,31 @@ def test_test_connect_params_run_testcase(self):
'result': {
'address': ['abc', 5656],
'database': 'edgedb',
'branch': '__default__',
'user': '__test__',
'password': None,
'secretKey': None,
'tlsCAData': None,
'tlsSecurity': 'strict',
'serverSettings': {},
'waitUntilAvailable': 30,
},
})

def test_test_connect_params_run_testcase_02(self):
with self.environ(EDGEDB_PORT='777'):
self.run_testcase({
'env': {
'EDGEDB_HOST': 'abc'
},
'opts': {
'user': '__test__',
'branch': 'new_branch',
},
'result': {
'address': ['abc', 5656],
'database': 'new_branch',
'branch': 'new_branch',
'user': '__test__',
'password': None,
'secretKey': None,
Expand DownExpand Up@@ -399,6 +427,7 @@ def test_project_config(self):
password=None,
secret_key=None,
database=None,
branch=None,
tls_ca=None,
tls_ca_file=None,
tls_security=None,
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
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
6 changes: 6 additions & 0 deletions edgedb/base_client.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -118,6 +118,10 @@ def remove_log_listener(
def dbname(self) -> str:
return self._params.database

@property
def branch(self) -> str:
return self._params.branch

@abc.abstractmethod
def is_closed(self) -> bool:
...
Expand DownExpand Up@@ -679,6 +683,7 @@ def __init__(
password: str = None,
secret_key: str = None,
database: str = None,
branch: str = None,
tls_ca: str = None,
tls_ca_file: str = None,
tls_security: str = None,
Expand All@@ -697,6 +702,7 @@ def __init__(
"password": password,
"secret_key": secret_key,
"database": database,
"branch": branch,
"timeout": timeout,
"tls_ca": tls_ca,
"tls_ca_file": tls_ca_file,
Expand Down
133 changes: 125 additions & 8 deletions edgedb/con_utils.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -181,9 +181,15 @@ class ResolvedConnectConfig:
_port = None
_port_source = None

# We keep track of database and branch separately, because we want to make
# sure that all the configuration is consistent and uses one or the other
# exclusively.
_database = None
_database_source = None

_branch = None
_branch_source = None

_user = None
_user_source = None

Expand DownExpand Up@@ -226,6 +232,9 @@ def set_port(self, port, source):
def set_database(self, database, source):
self._set_param('database', database, source, _validate_database)

def set_branch(self, branch, source):
self._set_param('branch', branch, source, _validate_branch)

def set_user(self, user, source):
self._set_param('user', user, source, _validate_user)

Expand DownExpand Up@@ -268,9 +277,24 @@ def address(self):
self._port if self._port else 5656
)

# The properties actually merge database and branch, but "default" is
# different. If you need to know the underlying config use the _database
# and _branch.
@property
def database(self):
return self._database if self._database else 'edgedb'
return (
self._database if self._database else
self._branch if self._branch else
'edgedb'
)

@property
def branch(self):
return (
self._database if self._database else
self._branch if self._branch else
'__default__'
)

@property
def user(self):
Expand DownExpand Up@@ -391,6 +415,12 @@ def _validate_database(database):
return database


def _validate_branch(branch):
if branch == '':
raise ValueError(f'invalid branch name: {branch}')
return branch


def _validate_user(user):
if user == '':
raise ValueError(f'invalid user name: {user}')
Expand DownExpand Up@@ -521,6 +551,7 @@ def _parse_connect_dsn_and_args(
password,
secret_key,
database,
branch,
tls_ca,
tls_ca_file,
tls_security,
Expand DownExpand Up@@ -557,6 +588,10 @@ def _parse_connect_dsn_and_args(
(database, '"database" option')
if database is not None else None
),
branch=(
(branch, '"branch" option')
if branch is not None else None
),
user=(user, '"user" option') if user is not None else None,
password=(
(password, '"password" option')
Expand DownExpand Up@@ -604,6 +639,7 @@ def _parse_connect_dsn_and_args(
env_credentials_file = os.getenv('EDGEDB_CREDENTIALS_FILE')
env_host = os.getenv('EDGEDB_HOST')
env_database = os.getenv('EDGEDB_DATABASE')
env_branch = os.getenv('EDGEDB_BRANCH')
env_user = os.getenv('EDGEDB_USER')
env_password = os.getenv('EDGEDB_PASSWORD')
env_secret_key = os.getenv('EDGEDB_SECRET_KEY')
Expand DownExpand Up@@ -643,6 +679,10 @@ def _parse_connect_dsn_and_args(
(env_database, '"EDGEDB_DATABASE" environment variable')
if env_database is not None else None
),
branch=(
(env_branch, '"EDGEDB_BRANCH" environment variable')
if env_branch is not None else None
),
user=(
(env_user, '"EDGEDB_USER" environment variable')
if env_user is not None else None
Expand DownExpand Up@@ -818,11 +858,52 @@ def handle_dsn_part(
def strip_leading_slash(str):
return str[1:] if str.startswith('/') else str

handle_dsn_part(
'database', strip_leading_slash(database),
resolved_config._database, resolved_config.set_database,
strip_leading_slash
)
if (
'branch' in query or
'branch_env' in query or
'branch_file' in query
):
if (
'database' in query or
'database_env' in query or
'database_file' in query
):
raise ValueError(
f"invalid DSN: `database` and `branch` cannot be present "
f"at the same time"
)
if resolved_config._database is not None:
raise errors.ClientConnectionError(
f"`branch` in DSN and {resolved_config._database_source} "
f"are mutually exclusive"
)
handle_dsn_part(
'branch', strip_leading_slash(database),
resolved_config._branch, resolved_config.set_branch,
strip_leading_slash
)
else:
if resolved_config._branch is not None:
if (
'database' in query or
'database_env' in query or
'database_file' in query
):
raise errors.ClientConnectionError(
f"`database` in DSN and {resolved_config._branch_source} "
f"are mutually exclusive"
)
handle_dsn_part(
'branch', strip_leading_slash(database),
resolved_config._branch, resolved_config.set_branch,
strip_leading_slash
)
else:
handle_dsn_part(
'database', strip_leading_slash(database),
resolved_config._database, resolved_config.set_database,
strip_leading_slash
)

handle_dsn_part(
'user', user, resolved_config._user, resolved_config.set_user
Expand DownExpand Up@@ -929,6 +1010,7 @@ def _resolve_config_options(
host=None,
port=None,
database=None,
branch=None,
user=None,
password=None,
secret_key=None,
Expand All@@ -940,7 +1022,23 @@ def _resolve_config_options(
cloud_profile=None,
):
if database is not None:
if branch is not None:
raise errors.ClientConnectionError(
f"{database[1]} and {branch[1]} are mutually exclusive"
)
if resolved_config._branch is not None:
raise errors.ClientConnectionError(
f"{database[1]} and {resolved_config._branch_source} are "
f"mutually exclusive"
)
resolved_config.set_database(*database)
if branch is not None:
if resolved_config._database is not None:
raise errors.ClientConnectionError(
f"{resolved_config._database_source} and {branch[1]} are "
f"mutually exclusive"
)
resolved_config.set_branch(*branch)
if user is not None:
resolved_config.set_user(*user)
if password is not None:
Expand All@@ -950,7 +1048,8 @@ def _resolve_config_options(
if tls_ca_file is not None:
if tls_ca is not None:
raise errors.ClientConnectionError(
f"{tls_ca[1]} and {tls_ca_file[1]} are mutually exclusive")
f"{tls_ca[1]} and {tls_ca_file[1]} are mutually exclusive"
)
resolved_config.set_tls_ca_file(*tls_ca_file)
if tls_ca is not None:
resolved_config.set_tls_ca_data(*tls_ca)
Expand DownExpand Up@@ -1018,7 +1117,23 @@ def _resolve_config_options(

resolved_config.set_host(creds.get('host'), source)
resolved_config.set_port(creds.get('port'), source)
resolved_config.set_database(creds.get('database'), source)
# We know that credentials have been validated, but they might be
# inconsistent with other resolved config settings.
if 'database' in creds:
if resolved_config._branch is not None:
raise errors.ClientConnectionError(
f"`branch` in configuration and `database` "
f"in credentials are mutually exclusive"
)
resolved_config.set_database(creds.get('database'), source)

elif 'branch' in creds:
if resolved_config._database is not None:
raise errors.ClientConnectionError(
f"`database` in configuration and `branch` "
f"in credentials are mutually exclusive"
)
resolved_config.set_branch(creds.get('branch'), source)
resolved_config.set_user(creds.get('user'), source)
resolved_config.set_password(creds.get('password'), source)
resolved_config.set_tls_ca_data(creds.get('tls_ca'), source)
Expand DownExpand Up@@ -1068,6 +1183,7 @@ def parse_connect_arguments(
credentials,
credentials_file,
database,
branch,
user,
password,
secret_key,
Expand DownExpand Up@@ -1100,6 +1216,7 @@ def parse_connect_arguments(
credentials=credentials,
credentials_file=credentials_file,
database=database,
branch=branch,
user=user,
password=password,
secret_key=secret_key,
Expand Down
11 changes: 11 additions & 0 deletions edgedb/credentials.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,7 +14,9 @@ class RequiredCredentials(typing.TypedDict, total=True):
class Credentials(RequiredCredentials, total=False):
host: typing.Optional[str]
password: typing.Optional[str]
# Either database or branch may appear in credentials, but not both.
database: typing.Optional[str]
branch: typing.Optional[str]
tls_ca: typing.Optional[str]
tls_security: typing.Optional[str]

Expand DownExpand Up@@ -64,6 +66,15 @@ def validate_credentials(data: dict) -> Credentials:
raise ValueError("`database` must be a string")
result['database'] = database

branch = data.get('branch')
if branch is not None:
if not isinstance(branch, str):
raise ValueError("`branch` must be a string")
if database is not None:
raise ValueError(
f"`database` and `branch` cannot both be set")
result['branch'] = branch

password = data.get('password')
if password is not None:
if not isinstance(password, str):
Expand Down
2 changes: 1 addition & 1 deletion tests/shared-client-testcases
31 changes: 30 additions & 1 deletion tests/test_con_utils.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -120,6 +120,7 @@ def run_testcase(self, testcase):
host = opts.get('host')
port = opts.get('port')
database = opts.get('database')
branch = opts.get('branch')
user = opts.get('user')
password = opts.get('password')
secret_key = opts.get('secretKey')
Expand DownExpand Up@@ -233,6 +234,7 @@ def mocked_open(filepath, *args, **kwargs):
credentials=credentials,
credentials_file=credentials_file,
database=database,
branch=branch,
user=user,
password=password,
secret_key=secret_key,
Expand All@@ -250,6 +252,7 @@ def mocked_open(filepath, *args, **kwargs):
connect_config.address[0], connect_config.address[1]
],
'database': connect_config.database,
'branch': connect_config.branch,
'user': connect_config.user,
'password': connect_config.password,
'secretKey': connect_config.secret_key,
Expand DownExpand Up@@ -289,7 +292,7 @@ def test_test_connect_params_environ(self):
if key in os.environ:
del os.environ[key]

def test_test_connect_params_run_testcase(self):
def test_test_connect_params_run_testcase_01(self):
with self.environ(EDGEDB_PORT='777'):
self.run_testcase({
'env': {
Expand All@@ -301,6 +304,31 @@ def test_test_connect_params_run_testcase(self):
'result': {
'address': ['abc', 5656],
'database': 'edgedb',
'branch': '__default__',
'user': '__test__',
'password': None,
'secretKey': None,
'tlsCAData': None,
'tlsSecurity': 'strict',
'serverSettings': {},
'waitUntilAvailable': 30,
},
})

def test_test_connect_params_run_testcase_02(self):
with self.environ(EDGEDB_PORT='777'):
self.run_testcase({
'env': {
'EDGEDB_HOST': 'abc'
},
'opts': {
'user': '__test__',
'branch': 'new_branch',
},
'result': {
'address': ['abc', 5656],
'database': 'new_branch',
'branch': 'new_branch',
'user': '__test__',
'password': None,
'secretKey': None,
Expand DownExpand Up@@ -399,6 +427,7 @@ def test_project_config(self):
password=None,
secret_key=None,
database=None,
branch=None,
tls_ca=None,
tls_ca_file=None,
tls_security=None,
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
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
6 changes: 6 additions & 0 deletions edgedb/base_client.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -118,6 +118,10 @@ def remove_log_listener(
def dbname(self) -> str:
return self._params.database

@property
def branch(self) -> str:
return self._params.branch

@abc.abstractmethod
def is_closed(self) -> bool:
...
Expand DownExpand Up@@ -679,6 +683,7 @@ def __init__(
password: str = None,
secret_key: str = None,
database: str = None,
branch: str = None,
tls_ca: str = None,
tls_ca_file: str = None,
tls_security: str = None,
Expand All@@ -697,6 +702,7 @@ def __init__(
"password": password,
"secret_key": secret_key,
"database": database,
"branch": branch,
"timeout": timeout,
"tls_ca": tls_ca,
"tls_ca_file": tls_ca_file,
Expand Down
133 changes: 125 additions & 8 deletions edgedb/con_utils.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -181,9 +181,15 @@ class ResolvedConnectConfig:
_port = None
_port_source = None

# We keep track of database and branch separately, because we want to make
# sure that all the configuration is consistent and uses one or the other
# exclusively.
_database = None
_database_source = None

_branch = None
_branch_source = None

_user = None
_user_source = None

Expand DownExpand Up@@ -226,6 +232,9 @@ def set_port(self, port, source):
def set_database(self, database, source):
self._set_param('database', database, source, _validate_database)

def set_branch(self, branch, source):
self._set_param('branch', branch, source, _validate_branch)

def set_user(self, user, source):
self._set_param('user', user, source, _validate_user)

Expand DownExpand Up@@ -268,9 +277,24 @@ def address(self):
self._port if self._port else 5656
)

# The properties actually merge database and branch, but "default" is
# different. If you need to know the underlying config use the _database
# and _branch.
@property
def database(self):
return self._database if self._database else 'edgedb'
return (
self._database if self._database else
self._branch if self._branch else
'edgedb'
)

@property
def branch(self):
return (
self._database if self._database else
self._branch if self._branch else
'__default__'
)

@property
def user(self):
Expand DownExpand Up@@ -391,6 +415,12 @@ def _validate_database(database):
return database


def _validate_branch(branch):
if branch == '':
raise ValueError(f'invalid branch name: {branch}')
return branch


def _validate_user(user):
if user == '':
raise ValueError(f'invalid user name: {user}')
Expand DownExpand Up@@ -521,6 +551,7 @@ def _parse_connect_dsn_and_args(
password,
secret_key,
database,
branch,
tls_ca,
tls_ca_file,
tls_security,
Expand DownExpand Up@@ -557,6 +588,10 @@ def _parse_connect_dsn_and_args(
(database, '"database" option')
if database is not None else None
),
branch=(
(branch, '"branch" option')
if branch is not None else None
),
user=(user, '"user" option') if user is not None else None,
password=(
(password, '"password" option')
Expand DownExpand Up@@ -604,6 +639,7 @@ def _parse_connect_dsn_and_args(
env_credentials_file = os.getenv('EDGEDB_CREDENTIALS_FILE')
env_host = os.getenv('EDGEDB_HOST')
env_database = os.getenv('EDGEDB_DATABASE')
env_branch = os.getenv('EDGEDB_BRANCH')
env_user = os.getenv('EDGEDB_USER')
env_password = os.getenv('EDGEDB_PASSWORD')
env_secret_key = os.getenv('EDGEDB_SECRET_KEY')
Expand DownExpand Up@@ -643,6 +679,10 @@ def _parse_connect_dsn_and_args(
(env_database, '"EDGEDB_DATABASE" environment variable')
if env_database is not None else None
),
branch=(
(env_branch, '"EDGEDB_BRANCH" environment variable')
if env_branch is not None else None
),
user=(
(env_user, '"EDGEDB_USER" environment variable')
if env_user is not None else None
Expand DownExpand Up@@ -818,11 +858,52 @@ def handle_dsn_part(
def strip_leading_slash(str):
return str[1:] if str.startswith('/') else str

handle_dsn_part(
'database', strip_leading_slash(database),
resolved_config._database, resolved_config.set_database,
strip_leading_slash
)
if (
'branch' in query or
'branch_env' in query or
'branch_file' in query
):
if (
'database' in query or
'database_env' in query or
'database_file' in query
):
raise ValueError(
f"invalid DSN: `database` and `branch` cannot be present "
f"at the same time"
)
if resolved_config._database is not None:
raise errors.ClientConnectionError(
f"`branch` in DSN and {resolved_config._database_source} "
f"are mutually exclusive"
)
handle_dsn_part(
'branch', strip_leading_slash(database),
resolved_config._branch, resolved_config.set_branch,
strip_leading_slash
)
else:
if resolved_config._branch is not None:
if (
'database' in query or
'database_env' in query or
'database_file' in query
):
raise errors.ClientConnectionError(
f"`database` in DSN and {resolved_config._branch_source} "
f"are mutually exclusive"
)
handle_dsn_part(
'branch', strip_leading_slash(database),
resolved_config._branch, resolved_config.set_branch,
strip_leading_slash
)
else:
handle_dsn_part(
'database', strip_leading_slash(database),
resolved_config._database, resolved_config.set_database,
strip_leading_slash
)

handle_dsn_part(
'user', user, resolved_config._user, resolved_config.set_user
Expand DownExpand Up@@ -929,6 +1010,7 @@ def _resolve_config_options(
host=None,
port=None,
database=None,
branch=None,
user=None,
password=None,
secret_key=None,
Expand All@@ -940,7 +1022,23 @@ def _resolve_config_options(
cloud_profile=None,
):
if database is not None:
if branch is not None:
raise errors.ClientConnectionError(
f"{database[1]} and {branch[1]} are mutually exclusive"
)
if resolved_config._branch is not None:
raise errors.ClientConnectionError(
f"{database[1]} and {resolved_config._branch_source} are "
f"mutually exclusive"
)
resolved_config.set_database(*database)
if branch is not None:
if resolved_config._database is not None:
raise errors.ClientConnectionError(
f"{resolved_config._database_source} and {branch[1]} are "
f"mutually exclusive"
)
resolved_config.set_branch(*branch)
if user is not None:
resolved_config.set_user(*user)
if password is not None:
Expand All@@ -950,7 +1048,8 @@ def _resolve_config_options(
if tls_ca_file is not None:
if tls_ca is not None:
raise errors.ClientConnectionError(
f"{tls_ca[1]} and {tls_ca_file[1]} are mutually exclusive")
f"{tls_ca[1]} and {tls_ca_file[1]} are mutually exclusive"
)
resolved_config.set_tls_ca_file(*tls_ca_file)
if tls_ca is not None:
resolved_config.set_tls_ca_data(*tls_ca)
Expand DownExpand Up@@ -1018,7 +1117,23 @@ def _resolve_config_options(

resolved_config.set_host(creds.get('host'), source)
resolved_config.set_port(creds.get('port'), source)
resolved_config.set_database(creds.get('database'), source)
# We know that credentials have been validated, but they might be
# inconsistent with other resolved config settings.
if 'database' in creds:
if resolved_config._branch is not None:
raise errors.ClientConnectionError(
f"`branch` in configuration and `database` "
f"in credentials are mutually exclusive"
)
resolved_config.set_database(creds.get('database'), source)

elif 'branch' in creds:
if resolved_config._database is not None:
raise errors.ClientConnectionError(
f"`database` in configuration and `branch` "
f"in credentials are mutually exclusive"
)
resolved_config.set_branch(creds.get('branch'), source)
resolved_config.set_user(creds.get('user'), source)
resolved_config.set_password(creds.get('password'), source)
resolved_config.set_tls_ca_data(creds.get('tls_ca'), source)
Expand DownExpand Up@@ -1068,6 +1183,7 @@ def parse_connect_arguments(
credentials,
credentials_file,
database,
branch,
user,
password,
secret_key,
Expand DownExpand Up@@ -1100,6 +1216,7 @@ def parse_connect_arguments(
credentials=credentials,
credentials_file=credentials_file,
database=database,
branch=branch,
user=user,
password=password,
secret_key=secret_key,
Expand Down
11 changes: 11 additions & 0 deletions edgedb/credentials.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,7 +14,9 @@ class RequiredCredentials(typing.TypedDict, total=True):
class Credentials(RequiredCredentials, total=False):
host: typing.Optional[str]
password: typing.Optional[str]
# Either database or branch may appear in credentials, but not both.
database: typing.Optional[str]
branch: typing.Optional[str]
tls_ca: typing.Optional[str]
tls_security: typing.Optional[str]

Expand DownExpand Up@@ -64,6 +66,15 @@ def validate_credentials(data: dict) -> Credentials:
raise ValueError("`database` must be a string")
result['database'] = database

branch = data.get('branch')
if branch is not None:
if not isinstance(branch, str):
raise ValueError("`branch` must be a string")
if database is not None:
raise ValueError(
f"`database` and `branch` cannot both be set")
result['branch'] = branch

password = data.get('password')
if password is not None:
if not isinstance(password, str):
Expand Down
2 changes: 1 addition & 1 deletion tests/shared-client-testcases
31 changes: 30 additions & 1 deletion tests/test_con_utils.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -120,6 +120,7 @@ def run_testcase(self, testcase):
host = opts.get('host')
port = opts.get('port')
database = opts.get('database')
branch = opts.get('branch')
user = opts.get('user')
password = opts.get('password')
secret_key = opts.get('secretKey')
Expand DownExpand Up@@ -233,6 +234,7 @@ def mocked_open(filepath, *args, **kwargs):
credentials=credentials,
credentials_file=credentials_file,
database=database,
branch=branch,
user=user,
password=password,
secret_key=secret_key,
Expand All@@ -250,6 +252,7 @@ def mocked_open(filepath, *args, **kwargs):
connect_config.address[0], connect_config.address[1]
],
'database': connect_config.database,
'branch': connect_config.branch,
'user': connect_config.user,
'password': connect_config.password,
'secretKey': connect_config.secret_key,
Expand DownExpand Up@@ -289,7 +292,7 @@ def test_test_connect_params_environ(self):
if key in os.environ:
del os.environ[key]

def test_test_connect_params_run_testcase(self):
def test_test_connect_params_run_testcase_01(self):
with self.environ(EDGEDB_PORT='777'):
self.run_testcase({
'env': {
Expand All@@ -301,6 +304,31 @@ def test_test_connect_params_run_testcase(self):
'result': {
'address': ['abc', 5656],
'database': 'edgedb',
'branch': '__default__',
'user': '__test__',
'password': None,
'secretKey': None,
'tlsCAData': None,
'tlsSecurity': 'strict',
'serverSettings': {},
'waitUntilAvailable': 30,
},
})

def test_test_connect_params_run_testcase_02(self):
with self.environ(EDGEDB_PORT='777'):
self.run_testcase({
'env': {
'EDGEDB_HOST': 'abc'
},
'opts': {
'user': '__test__',
'branch': 'new_branch',
},
'result': {
'address': ['abc', 5656],
'database': 'new_branch',
'branch': 'new_branch',
'user': '__test__',
'password': None,
'secretKey': None,
Expand DownExpand Up@@ -399,6 +427,7 @@ def test_project_config(self):
password=None,
secret_key=None,
database=None,
branch=None,
tls_ca=None,
tls_ca_file=None,
tls_security=None,
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content
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
6 changes: 6 additions & 0 deletions edgedb/base_client.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -118,6 +118,10 @@ def remove_log_listener(
def dbname(self) -> str:
return self._params.database

@property
def branch(self) -> str:
return self._params.branch

@abc.abstractmethod
def is_closed(self) -> bool:
...
Expand DownExpand Up@@ -679,6 +683,7 @@ def __init__(
password: str = None,
secret_key: str = None,
database: str = None,
branch: str = None,
tls_ca: str = None,
tls_ca_file: str = None,
tls_security: str = None,
Expand All@@ -697,6 +702,7 @@ def __init__(
"password": password,
"secret_key": secret_key,
"database": database,
"branch": branch,
"timeout": timeout,
"tls_ca": tls_ca,
"tls_ca_file": tls_ca_file,
Expand Down
133 changes: 125 additions & 8 deletions edgedb/con_utils.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -181,9 +181,15 @@ class ResolvedConnectConfig:
_port = None
_port_source = None

# We keep track of database and branch separately, because we want to make
# sure that all the configuration is consistent and uses one or the other
# exclusively.
_database = None
_database_source = None

_branch = None
_branch_source = None

_user = None
_user_source = None

Expand DownExpand Up@@ -226,6 +232,9 @@ def set_port(self, port, source):
def set_database(self, database, source):
self._set_param('database', database, source, _validate_database)

def set_branch(self, branch, source):
self._set_param('branch', branch, source, _validate_branch)

def set_user(self, user, source):
self._set_param('user', user, source, _validate_user)

Expand DownExpand Up@@ -268,9 +277,24 @@ def address(self):
self._port if self._port else 5656
)

# The properties actually merge database and branch, but "default" is
# different. If you need to know the underlying config use the _database
# and _branch.
@property
def database(self):
return self._database if self._database else 'edgedb'
return (
self._database if self._database else
self._branch if self._branch else
'edgedb'
)

@property
def branch(self):
return (
self._database if self._database else
self._branch if self._branch else
'__default__'
)

@property
def user(self):
Expand DownExpand Up@@ -391,6 +415,12 @@ def _validate_database(database):
return database


def _validate_branch(branch):
if branch == '':
raise ValueError(f'invalid branch name: {branch}')
return branch


def _validate_user(user):
if user == '':
raise ValueError(f'invalid user name: {user}')
Expand DownExpand Up@@ -521,6 +551,7 @@ def _parse_connect_dsn_and_args(
password,
secret_key,
database,
branch,
tls_ca,
tls_ca_file,
tls_security,
Expand DownExpand Up@@ -557,6 +588,10 @@ def _parse_connect_dsn_and_args(
(database, '"database" option')
if database is not None else None
),
branch=(
(branch, '"branch" option')
if branch is not None else None
),
user=(user, '"user" option') if user is not None else None,
password=(
(password, '"password" option')
Expand DownExpand Up@@ -604,6 +639,7 @@ def _parse_connect_dsn_and_args(
env_credentials_file = os.getenv('EDGEDB_CREDENTIALS_FILE')
env_host = os.getenv('EDGEDB_HOST')
env_database = os.getenv('EDGEDB_DATABASE')
env_branch = os.getenv('EDGEDB_BRANCH')
env_user = os.getenv('EDGEDB_USER')
env_password = os.getenv('EDGEDB_PASSWORD')
env_secret_key = os.getenv('EDGEDB_SECRET_KEY')
Expand DownExpand Up@@ -643,6 +679,10 @@ def _parse_connect_dsn_and_args(
(env_database, '"EDGEDB_DATABASE" environment variable')
if env_database is not None else None
),
branch=(
(env_branch, '"EDGEDB_BRANCH" environment variable')
if env_branch is not None else None
),
user=(
(env_user, '"EDGEDB_USER" environment variable')
if env_user is not None else None
Expand DownExpand Up@@ -818,11 +858,52 @@ def handle_dsn_part(
def strip_leading_slash(str):
return str[1:] if str.startswith('/') else str

handle_dsn_part(
'database', strip_leading_slash(database),
resolved_config._database, resolved_config.set_database,
strip_leading_slash
)
if (
'branch' in query or
'branch_env' in query or
'branch_file' in query
):
if (
'database' in query or
'database_env' in query or
'database_file' in query
):
raise ValueError(
f"invalid DSN: `database` and `branch` cannot be present "
f"at the same time"
)
if resolved_config._database is not None:
raise errors.ClientConnectionError(
f"`branch` in DSN and {resolved_config._database_source} "
f"are mutually exclusive"
)
handle_dsn_part(
'branch', strip_leading_slash(database),
resolved_config._branch, resolved_config.set_branch,
strip_leading_slash
)
else:
if resolved_config._branch is not None:
if (
'database' in query or
'database_env' in query or
'database_file' in query
):
raise errors.ClientConnectionError(
f"`database` in DSN and {resolved_config._branch_source} "
f"are mutually exclusive"
)
handle_dsn_part(
'branch', strip_leading_slash(database),
resolved_config._branch, resolved_config.set_branch,
strip_leading_slash
)
else:
handle_dsn_part(
'database', strip_leading_slash(database),
resolved_config._database, resolved_config.set_database,
strip_leading_slash
)

handle_dsn_part(
'user', user, resolved_config._user, resolved_config.set_user
Expand DownExpand Up@@ -929,6 +1010,7 @@ def _resolve_config_options(
host=None,
port=None,
database=None,
branch=None,
user=None,
password=None,
secret_key=None,
Expand All@@ -940,7 +1022,23 @@ def _resolve_config_options(
cloud_profile=None,
):
if database is not None:
if branch is not None:
raise errors.ClientConnectionError(
f"{database[1]} and {branch[1]} are mutually exclusive"
)
if resolved_config._branch is not None:
raise errors.ClientConnectionError(
f"{database[1]} and {resolved_config._branch_source} are "
f"mutually exclusive"
)
resolved_config.set_database(*database)
if branch is not None:
if resolved_config._database is not None:
raise errors.ClientConnectionError(
f"{resolved_config._database_source} and {branch[1]} are "
f"mutually exclusive"
)
resolved_config.set_branch(*branch)
if user is not None:
resolved_config.set_user(*user)
if password is not None:
Expand All@@ -950,7 +1048,8 @@ def _resolve_config_options(
if tls_ca_file is not None:
if tls_ca is not None:
raise errors.ClientConnectionError(
f"{tls_ca[1]} and {tls_ca_file[1]} are mutually exclusive")
f"{tls_ca[1]} and {tls_ca_file[1]} are mutually exclusive"
)
resolved_config.set_tls_ca_file(*tls_ca_file)
if tls_ca is not None:
resolved_config.set_tls_ca_data(*tls_ca)
Expand DownExpand Up@@ -1018,7 +1117,23 @@ def _resolve_config_options(

resolved_config.set_host(creds.get('host'), source)
resolved_config.set_port(creds.get('port'), source)
resolved_config.set_database(creds.get('database'), source)
# We know that credentials have been validated, but they might be
# inconsistent with other resolved config settings.
if 'database' in creds:
if resolved_config._branch is not None:
raise errors.ClientConnectionError(
f"`branch` in configuration and `database` "
f"in credentials are mutually exclusive"
)
resolved_config.set_database(creds.get('database'), source)

elif 'branch' in creds:
if resolved_config._database is not None:
raise errors.ClientConnectionError(
f"`database` in configuration and `branch` "
f"in credentials are mutually exclusive"
)
resolved_config.set_branch(creds.get('branch'), source)
resolved_config.set_user(creds.get('user'), source)
resolved_config.set_password(creds.get('password'), source)
resolved_config.set_tls_ca_data(creds.get('tls_ca'), source)
Expand DownExpand Up@@ -1068,6 +1183,7 @@ def parse_connect_arguments(
credentials,
credentials_file,
database,
branch,
user,
password,
secret_key,
Expand DownExpand Up@@ -1100,6 +1216,7 @@ def parse_connect_arguments(
credentials=credentials,
credentials_file=credentials_file,
database=database,
branch=branch,
user=user,
password=password,
secret_key=secret_key,
Expand Down
11 changes: 11 additions & 0 deletions edgedb/credentials.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,7 +14,9 @@ class RequiredCredentials(typing.TypedDict, total=True):
class Credentials(RequiredCredentials, total=False):
host: typing.Optional[str]
password: typing.Optional[str]
# Either database or branch may appear in credentials, but not both.
database: typing.Optional[str]
branch: typing.Optional[str]
tls_ca: typing.Optional[str]
tls_security: typing.Optional[str]

Expand DownExpand Up@@ -64,6 +66,15 @@ def validate_credentials(data: dict) -> Credentials:
raise ValueError("`database` must be a string")
result['database'] = database

branch = data.get('branch')
if branch is not None:
if not isinstance(branch, str):
raise ValueError("`branch` must be a string")
if database is not None:
raise ValueError(
f"`database` and `branch` cannot both be set")
result['branch'] = branch

password = data.get('password')
if password is not None:
if not isinstance(password, str):
Expand Down
2 changes: 1 addition & 1 deletion tests/shared-client-testcases
31 changes: 30 additions & 1 deletion tests/test_con_utils.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -120,6 +120,7 @@ def run_testcase(self, testcase):
host = opts.get('host')
port = opts.get('port')
database = opts.get('database')
branch = opts.get('branch')
user = opts.get('user')
password = opts.get('password')
secret_key = opts.get('secretKey')
Expand DownExpand Up@@ -233,6 +234,7 @@ def mocked_open(filepath, *args, **kwargs):
credentials=credentials,
credentials_file=credentials_file,
database=database,
branch=branch,
user=user,
password=password,
secret_key=secret_key,
Expand All@@ -250,6 +252,7 @@ def mocked_open(filepath, *args, **kwargs):
connect_config.address[0], connect_config.address[1]
],
'database': connect_config.database,
'branch': connect_config.branch,
'user': connect_config.user,
'password': connect_config.password,
'secretKey': connect_config.secret_key,
Expand DownExpand Up@@ -289,7 +292,7 @@ def test_test_connect_params_environ(self):
if key in os.environ:
del os.environ[key]

def test_test_connect_params_run_testcase(self):
def test_test_connect_params_run_testcase_01(self):
with self.environ(EDGEDB_PORT='777'):
self.run_testcase({
'env': {
Expand All@@ -301,6 +304,31 @@ def test_test_connect_params_run_testcase(self):
'result': {
'address': ['abc', 5656],
'database': 'edgedb',
'branch': '__default__',
'user': '__test__',
'password': None,
'secretKey': None,
'tlsCAData': None,
'tlsSecurity': 'strict',
'serverSettings': {},
'waitUntilAvailable': 30,
},
})

def test_test_connect_params_run_testcase_02(self):
with self.environ(EDGEDB_PORT='777'):
self.run_testcase({
'env': {
'EDGEDB_HOST': 'abc'
},
'opts': {
'user': '__test__',
'branch': 'new_branch',
},
'result': {
'address': ['abc', 5656],
'database': 'new_branch',
'branch': 'new_branch',
'user': '__test__',
'password': None,
'secretKey': None,
Expand DownExpand Up@@ -399,6 +427,7 @@ def test_project_config(self):
password=None,
secret_key=None,
database=None,
branch=None,
tls_ca=None,
tls_ca_file=None,
tls_security=None,
Expand Down