|
1 | 1 | //! Shared state used by all endpoints.
|
2 |
| -use std::sync::Arc; |
| 2 | +use std::sync::{Arc, Mutex}; |
3 | 3 |
|
4 | 4 | use crate::{
|
5 | 5 | cli::Error,
|
6 | 6 | event_db::{establish_connection, queries::EventDbQueries},
|
7 | 7 | };
|
8 | 8 |
|
| 9 | +/// The status of the expected DB schema version. |
| 10 | +pub(crate) enum SchemaVersionStatus { |
| 11 | + /// The current DB schema version matches what is expected. |
| 12 | + Ok, |
| 13 | + /// There is a mismatch between the current DB schema version |
| 14 | + /// and what is expected. |
| 15 | + Mismatch, |
| 16 | +} |
| 17 | + |
9 | 18 | /// Global State of the service
|
10 | 19 | pub(crate) struct State {
|
11 | 20 | /// This can be None, or a handle to the DB.
|
12 | 21 | /// If the DB fails, it can be set to None.
|
13 | 22 | /// If its None, an attempt to get it will try and connect to the DB.
|
14 | 23 | /// This is Private, it needs to be accessed with a function.
|
15 |
| - // event_db_handle: Arc<ArcSwap<Option<dyn EventDbQueries>>>, // Private need to get it with a |
16 |
| - // function. |
| 24 | + // event_db_handle: Arc<ArcSwap<Option<dyn EventDbQueries>>>, |
| 25 | + // Private need to get it with a function. |
17 | 26 | pub(crate) event_db: Arc<dyn EventDbQueries>, /* This needs to be obsoleted, we want the DB
|
18 | 27 | * to be able to be down. */
|
| 28 | + /// Status of the last DB schema version check. |
| 29 | + pub(crate) schema_version_status: Mutex<SchemaVersionStatus>, |
19 | 30 | }
|
20 | 31 |
|
21 | 32 | impl State {
|
22 | 33 | /// Create a new global [`State`]
|
23 | 34 | pub(crate) async fn new(database_url: Option<String>) -> Result<Self, Error> {
|
24 |
| - // Get a configured pool to the Database. |
| 35 | + // Get a configured pool to the Database, runs schema version check internally. |
25 | 36 | let event_db = Arc::new(establish_connection(database_url, false).await?);
|
26 | 37 |
|
27 |
| - let state = Self { event_db }; |
| 38 | + let state = Self { |
| 39 | + event_db, |
| 40 | + // It is safe to assume that the schema version matches if `event_db` doesn't fail, |
| 41 | + // due to the interior check ran by `establish_connection`. |
| 42 | + schema_version_status: Mutex::new(SchemaVersionStatus::Ok), |
| 43 | + }; |
28 | 44 |
|
29 | 45 | // We don't care if this succeeds or not.
|
30 | 46 | // We just try our best to connect to the event DB.
|
|
0 commit comments