Skip to content

Commit 59e6b97

Browse files
committed
EnvConfig - use Env. variable for seeding the dbs
1 parent 2c6a333 commit 59e6b97

File tree

5 files changed

+50
-30
lines changed

5 files changed

+50
-30
lines changed

docs/config/ganache.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ admins = [
4747
'0x80690751969B234697e9059e04ed72195c3507fa'
4848
]
4949

50-
seed_db = true
51-
5250
[platform]
5351
# This should be changed for tests and use the wiremock url
5452
url = "https://platform.adex.network"

docs/config/prod.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ validators_whitelist = []
2828
# Galya (for analytics)
2929
admins = ['0x5d6A3F1AD7b124ecDFDf4841D9bB246eD5fBF04c']
3030

31-
seed_db = false
32-
3331
[platform]
3432
# This should be changed for tests and use the wiremock url
3533
url = "https://platform.adex.network"

primitives/src/config.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,6 @@ pub struct Config {
182182
pub chains: HashMap<String, ChainInfo>,
183183
pub platform: PlatformConfig,
184184
pub limits: Limits,
185-
pub seed_db: bool,
186185
}
187186

188187
impl Config {

sentry/src/application.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ pub struct EnvConfig {
7373
/// Defaults to locally running Redis server: [`DEFAULT_REDIS_URL`]
7474
#[serde(deserialize_with = "redis_url", default = "default_redis_url")]
7575
pub redis_url: ConnectionInfo,
76+
/// Whether or not to seed the database in [`Environment::Development`].
77+
#[serde(default)]
78+
pub seed_db: bool,
79+
7680
}
7781

7882
impl EnvConfig {

sentry/src/main.rs

Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,20 @@ use std::{env, net::SocketAddr, path::PathBuf};
55

66
use clap::{crate_version, value_parser, Arg, Command};
77

8+
use redis::aio::MultiplexedConnection;
89
use slog::info;
910

1011
use adapter::{primitives::AdapterTypes, Adapter};
1112
use primitives::{
12-
config::configuration, postgres::POSTGRES_CONFIG, test_util::DUMMY_AUTH,
13-
util::logging::new_logger, ValidatorId,
13+
config::{configuration, Environment},
14+
postgres::POSTGRES_CONFIG,
15+
test_util::DUMMY_AUTH,
16+
util::logging::new_logger,
17+
ValidatorId,
1418
};
1519
use sentry::{
16-
application::{seed_dummy, seed_ethereum, EnableTls},
17-
db::{postgres_connection, redis_connection, setup_migrations, CampaignRemaining},
20+
application::{seed_dummy, seed_ethereum, EnableTls, EnvConfig},
21+
db::{postgres_connection, redis_connection, setup_migrations, CampaignRemaining, DbPool},
1822
platform::PlatformApi,
1923
Application,
2024
};
@@ -127,20 +131,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
127131
};
128132

129133
let logger = new_logger("sentry");
130-
let redis = redis_connection(env_config.redis_url).await?;
131-
info!(&logger, "Checking connection and applying migrations...");
132-
// Check connection and setup migrations before setting up Postgres
133-
tokio::task::block_in_place(|| {
134-
// Migrations are blocking, so we need to wrap it with block_in_place
135-
// otherwise we get a tokio error
136-
setup_migrations(env_config.env)
137-
});
138134

139-
// use the environmental variables to setup the Postgres connection
140-
let postgres = match postgres_connection(POSTGRES_CONFIG.clone()).await {
141-
Ok(pool) => pool,
142-
Err(build_err) => panic!("Failed to build postgres database pool: {build_err}"),
143-
};
135+
let (redis, postgres) = setup_databases(&logger, &env_config).await?;
144136

145137
let campaign_remaining = CampaignRemaining::new(redis.clone());
146138

@@ -163,10 +155,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
163155
platform_api,
164156
);
165157

166-
if config.seed_db {
167-
redis::cmd("FLUSHDB")
168-
.query_async::<_, String>(&mut redis.clone())
169-
.await?;
158+
if env_config.seed_db && Environment::Development == env_config.env {
170159
seed_ethereum(app.clone()).await?;
171160
}
172161

@@ -183,10 +172,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
183172
platform_api,
184173
);
185174

186-
if config.seed_db {
187-
redis::cmd("FLUSHDB")
188-
.query_async::<_, String>(&mut redis.clone())
189-
.await?;
175+
if env_config.seed_db && Environment::Development == env_config.env {
190176
seed_dummy(app.clone()).await?;
191177
}
192178

@@ -196,3 +182,38 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
196182

197183
Ok(())
198184
}
185+
186+
/// Setup the databases before use in the application:
187+
///
188+
/// 1. Runs migrations on `postgres` but if [`Environment::Development`] then it runs them down first.
189+
/// 2. Flushes `redis` if [`Environment::Development`].
190+
async fn setup_databases(
191+
logger: &slog::Logger,
192+
env_config: &EnvConfig,
193+
) -> Result<(MultiplexedConnection, DbPool), Box<dyn std::error::Error>> {
194+
let redis = redis_connection(env_config.redis_url.clone()).await?;
195+
196+
info!(&logger, "Checking connection and applying migrations...");
197+
// Check connection and setup migrations before setting up Postgres
198+
tokio::task::block_in_place(|| {
199+
// Migrations are blocking, so we need to wrap it with block_in_place
200+
// otherwise we get a tokio error
201+
setup_migrations(env_config.env)
202+
});
203+
204+
// clearing up redis
205+
if Environment::Development == env_config.env {
206+
info!(&logger, "Flushing redis...");
207+
redis::cmd("FLUSHDB")
208+
.query_async::<_, String>(&mut redis.clone())
209+
.await?;
210+
}
211+
212+
// use the environmental variables to setup the Postgres connection
213+
let postgres = match postgres_connection(POSTGRES_CONFIG.clone()).await {
214+
Ok(pool) => pool,
215+
Err(build_err) => panic!("Failed to build postgres database pool: {build_err}"),
216+
};
217+
218+
Ok((redis, postgres))
219+
}

0 commit comments

Comments
 (0)