Skip to content

Commit

Permalink
clarify branch and database in credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
quinchs committed Mar 14, 2024
1 parent a7a0f84 commit 9bb809f
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 25 deletions.
79 changes: 54 additions & 25 deletions edgedb-tokio/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,18 +541,7 @@ impl<'a> DsnHelper<'a> {
}

async fn retrieve_branch(&mut self) -> Result<Option<String>, Error> {
let v = self.url.path().strip_prefix("/").and_then(|s| {
if s.is_empty() {
None
} else {
Some(s.to_owned())
}
});
self.retrieve_value("branch", v, |s| {
let s = s.strip_prefix("/").unwrap_or(&s);
validate_branch(&s)?;
Ok(s.to_owned())
}).await
self.retrieve_value("branch", None, validate_branch).await
}

async fn retrieve_secret_key(&mut self) -> Result<Option<String>, Error> {
Expand Down Expand Up @@ -842,7 +831,7 @@ impl Builder {
.or_else(|| creds.map(|c| c.database.clone()).flatten())
.unwrap_or_else(|| "edgedb".into()),
branch: self.branch.clone()
.or_else(|| creds.map(|c| c.database.clone()).flatten())
.or_else(|| creds.map(|c| c.branch.clone()).flatten())
.unwrap_or_else(|| "__default__".into()),
instance_name: None,
wait: self.wait_until_available.unwrap_or(DEFAULT_WAIT),
Expand Down Expand Up @@ -1250,24 +1239,63 @@ impl Builder {
} else {
dsn.ignore_value("password");
}
if self.database.is_none() {
match dsn.retrieve_database().await {
Ok(Some(value)) => cfg.database = value,
Ok(None) => {},
Err(e) => errors.push(e),
let database = match dsn.retrieve_database().await {
Ok(v) => v,
Err(e) => {
errors.push(e);
None
}
} else {
dsn.ignore_value("database");
}
};
if self.branch.is_none() {
match dsn.retrieve_branch().await {
Ok(Some(value)) => cfg.branch = value,
Ok(None) => {},
Ok(Some(value)) => {
if database.is_some() {
errors.push(InvalidArgumentError::with_message(
"Invalid DSN: `database` and `branch` cannot be present at the same\
time"
));
return;
}

cfg.branch = value;
}
Ok(None) => {
if let Some(database) = database {
cfg.database = database;
}
},
Err(e) => errors.push(e)
}
} else if database.is_some() {
errors.push(InvalidArgumentError::with_message(
"Invalid DSN: `database` in DSN and `branch` are mutually exclusive"
));
} else {
dsn.ignore_value("branch");
dsn.ignore_value("branch")
}

if self.database.is_some() {
dsn.ignore_value("database");
}

// if self.branch.is_none() {
// match dsn.retrieve_branch().await {
// Ok(Some(value)) => cfg.branch = value,
// Ok(None) => {},
// Err(e) => errors.push(e)
// }
// } else {
// dsn.ignore_value("branch");
// }
// if self.database.is_none() {
// match dsn.retrieve_database().await {
// Ok(Some(value)) => cfg.database = value,
// Ok(None) => {},
// Err(e) => errors.push(e),
// }
// } else {
// dsn.ignore_value("database");
// }
match dsn.retrieve_secret_key().await {
Ok(Some(value)) => cfg.secret_key = Some(value),
Ok(None) => {},
Expand Down Expand Up @@ -1726,7 +1754,8 @@ impl Config {
port: *port,
user: self.0.user.clone(),
password: self.0.password.clone(),
database: if self.0.branch == "__default__" { Some(self.0.database.clone()) } else { Some(self.0.database.clone()) },
database: if self.0.branch == "__default__" { Some(self.0.database.clone()) } else { None },
branch: if self.0.branch == "__default__" { None } else { Some(self.0.branch.clone()) },
tls_ca: self.0.pem_certificates.clone(),
tls_security: self.0.tls_security,
file_outdated: false,
Expand Down
6 changes: 6 additions & 0 deletions edgedb-tokio/src/credentials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub struct Credentials {
pub user: String,
pub password: Option<String>,
pub database: Option<String>,
pub branch: Option<String>,
pub tls_ca: Option<String>,
pub tls_security: TlsSecurity,
pub(crate) file_outdated: bool,
Expand All @@ -56,6 +57,8 @@ struct CredentialsCompat {
#[serde(default, skip_serializing_if="Option::is_none")]
database: Option<String>,
#[serde(default, skip_serializing_if="Option::is_none")]
branch: Option<String>,
#[serde(default, skip_serializing_if="Option::is_none")]
tls_cert_data: Option<String>, // deprecated
#[serde(default, skip_serializing_if="Option::is_none")]
tls_ca: Option<String>,
Expand Down Expand Up @@ -114,6 +117,7 @@ impl Default for Credentials {
user: "edgedb".into(),
password: None,
database: None,
branch: None,
tls_ca: None,
tls_security: TlsSecurity::Default,
file_outdated: false,
Expand All @@ -133,6 +137,7 @@ impl Serialize for Credentials {
user: self.user.clone(),
password: self.password.clone(),
database: self.database.clone(),
branch: self.branch.clone(),
tls_ca: self.tls_ca.clone(),
tls_cert_data: self.tls_ca.clone(),
tls_security: Some(self.tls_security),
Expand Down Expand Up @@ -192,6 +197,7 @@ impl<'de> Deserialize<'de> for Credentials {
user: creds.user,
password: creds.password,
database: creds.database,
branch: creds.branch,
tls_ca: creds.tls_ca.or(creds.tls_cert_data.clone()),
tls_security: creds.tls_security.unwrap_or(
match creds.tls_verify_hostname {
Expand Down

0 comments on commit 9bb809f

Please sign in to comment.