@@ -5,16 +5,20 @@ use std::{env, net::SocketAddr, path::PathBuf};
5
5
6
6
use clap:: { crate_version, value_parser, Arg , Command } ;
7
7
8
+ use redis:: aio:: MultiplexedConnection ;
8
9
use slog:: info;
9
10
10
11
use adapter:: { primitives:: AdapterTypes , Adapter } ;
11
12
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 ,
14
18
} ;
15
19
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 } ,
18
22
platform:: PlatformApi ,
19
23
Application ,
20
24
} ;
@@ -127,20 +131,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
127
131
} ;
128
132
129
133
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
- } ) ;
138
134
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 ?;
144
136
145
137
let campaign_remaining = CampaignRemaining :: new ( redis. clone ( ) ) ;
146
138
@@ -163,10 +155,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
163
155
platform_api,
164
156
) ;
165
157
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 {
170
159
seed_ethereum ( app. clone ( ) ) . await ?;
171
160
}
172
161
@@ -183,10 +172,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
183
172
platform_api,
184
173
) ;
185
174
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 {
190
176
seed_dummy ( app. clone ( ) ) . await ?;
191
177
}
192
178
@@ -196,3 +182,38 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
196
182
197
183
Ok ( ( ) )
198
184
}
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