|
@@ -2,20 +2,18 @@ use crate::*;
|
|
|
|
|
|
#[inline]
|
|
#[inline]
|
|
pub async fn get_db_connection() -> DbPoolConnection {
|
|
pub async fn get_db_connection() -> DbPoolConnection {
|
|
- if let Some(db_pool) = DB.read().await.clone() {
|
|
|
|
- return db_pool;
|
|
|
|
|
|
+ if let Some(db_pool) = DB.get() {
|
|
|
|
+ return db_pool.clone();
|
|
};
|
|
};
|
|
let db_pool: DbPoolConnection = connection_db().await;
|
|
let db_pool: DbPoolConnection = connection_db().await;
|
|
- {
|
|
|
|
- let mut db_pool_lock: RwLockWriteGuard<'_, Option<DbPoolConnection>> = DB.write().await;
|
|
|
|
- *db_pool_lock = Some(db_pool.clone());
|
|
|
|
- }
|
|
|
|
|
|
+ DB.set(db_pool.clone())
|
|
|
|
+ .expect("Failed to initialize DB_POOL");
|
|
db_pool
|
|
db_pool
|
|
}
|
|
}
|
|
|
|
|
|
#[inline]
|
|
#[inline]
|
|
#[cfg(feature = "dev")]
|
|
#[cfg(feature = "dev")]
|
|
-pub async fn create_batabase() {
|
|
|
|
|
|
+pub async fn create_database() {
|
|
let db_pool: DbPoolConnection = get_db_connection().await;
|
|
let db_pool: DbPoolConnection = get_db_connection().await;
|
|
let _ = query(&format!("CREATE DATABASE {};", DATABASE_NAME))
|
|
let _ = query(&format!("CREATE DATABASE {};", DATABASE_NAME))
|
|
.execute(&db_pool)
|
|
.execute(&db_pool)
|
|
@@ -97,8 +95,7 @@ pub async fn init_cache() {
|
|
res.push(QueryRow::new(id, random_number));
|
|
res.push(QueryRow::new(id, random_number));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- let mut cache: RwLockWriteGuard<'_, Vec<QueryRow>> = CACHE.write().await;
|
|
|
|
- *cache = res;
|
|
|
|
|
|
+ let _ = CACHE.set(res);
|
|
}
|
|
}
|
|
|
|
|
|
#[inline]
|
|
#[inline]
|
|
@@ -170,13 +167,10 @@ pub async fn get_update_data(
|
|
|
|
|
|
#[inline]
|
|
#[inline]
|
|
pub async fn init_db() {
|
|
pub async fn init_db() {
|
|
- {
|
|
|
|
- let mut db_pool_lock: RwLockWriteGuard<'_, Option<DbPoolConnection>> = DB.write().await;
|
|
|
|
- *db_pool_lock = Some(connection_db().await);
|
|
|
|
- }
|
|
|
|
|
|
+ get_db_connection().await;
|
|
#[cfg(feature = "dev")]
|
|
#[cfg(feature = "dev")]
|
|
{
|
|
{
|
|
- create_batabase().await;
|
|
|
|
|
|
+ create_database().await;
|
|
create_table().await;
|
|
create_table().await;
|
|
insert_records().await;
|
|
insert_records().await;
|
|
}
|
|
}
|