Skip to content

Commit

Permalink
Merge pull request #706 from blockscout/kf/chore/db-settings
Browse files Browse the repository at this point in the history
chore: common db settings
  • Loading branch information
k1rill-fedoseev authored Dec 19, 2023
2 parents d3f2112 + f8d39bc commit 22ba428
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
2 changes: 1 addition & 1 deletion libs/blockscout-service-launcher/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "blockscout-service-launcher"
version = "0.9.0"
version = "0.10.0"
description = "Allows to launch blazingly fast blockscout rust services"
license = "MIT"
repository = "https://github.com/blockscout/blockscout-rs"
Expand Down
57 changes: 57 additions & 0 deletions libs/blockscout-service-launcher/src/database.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use anyhow::Context;
use serde::Deserialize;
use std::str::FromStr;

cfg_if::cfg_if! {
Expand Down Expand Up @@ -102,3 +103,59 @@ fn with_connect_options(url: impl Into<String>, source_options: &ConnectOptions)
options.sqlx_logging_level(source_options.get_sqlx_logging_level());
options
}

#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct DatabaseSettings {
pub connect: DatabaseConnectSettings,
#[serde(default)]
pub create_database: bool,
#[serde(default)]
pub run_migrations: bool,
}

#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
#[serde(deny_unknown_fields, rename_all = "lowercase")]
pub enum DatabaseConnectSettings {
Url(String),
Kv(DatabaseKvConnection),
}

impl DatabaseConnectSettings {
pub fn url(self) -> String {
match self {
DatabaseConnectSettings::Url(s) => s,
DatabaseConnectSettings::Kv(kv) => kv.url(),
}
}
}

#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct DatabaseKvConnection {
pub host: String,
pub port: u16,
pub user: String,
pub password: String,
#[serde(default)]
pub dbname: Option<String>,
#[serde(default)]
pub options: Option<String>,
}

impl DatabaseKvConnection {
pub fn url(self) -> String {
let dbname = self
.dbname
.map(|dbname| format!("/{dbname}"))
.unwrap_or_default();
let options = self
.options
.map(|options| format!("?{options}"))
.unwrap_or_default();
format!(
"postgresql://{}:{}@{}:{}{}{}",
self.user, self.password, self.host, self.port, dbname, options
)
}
}

0 comments on commit 22ba428

Please sign in to comment.