remove init code from db.rs
This commit is contained in:
parent
d8a5542abc
commit
b60e632e80
1 changed files with 3 additions and 58 deletions
|
@ -1,4 +1,3 @@
|
||||||
use log::debug;
|
|
||||||
use serenity::prelude::TypeMapKey;
|
use serenity::prelude::TypeMapKey;
|
||||||
use sqlx::{
|
use sqlx::{
|
||||||
sqlite::{SqliteConnection, SqlitePool},
|
sqlite::{SqliteConnection, SqlitePool},
|
||||||
|
@ -15,7 +14,9 @@ pub(crate) struct DbConnPool {
|
||||||
impl DbConnPool {
|
impl DbConnPool {
|
||||||
pub async fn new() -> Result<Self, Error> {
|
pub async fn new() -> Result<Self, Error> {
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
pool: init_pool().await?,
|
pool: SqlitePool::builder()
|
||||||
|
.build(&env::var("DATABASE_URL").unwrap())
|
||||||
|
.await?,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,59 +24,3 @@ impl DbConnPool {
|
||||||
impl TypeMapKey for DbConnPool {
|
impl TypeMapKey for DbConnPool {
|
||||||
type Value = Self;
|
type Value = Self;
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn init_pool() -> Result<DbPool, Error> {
|
|
||||||
let pool = SqlitePool::builder()
|
|
||||||
.build(&env::var("DATABASE_URL").unwrap())
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
// Heck table
|
|
||||||
sqlx::query!(
|
|
||||||
"CREATE TABLE IF NOT EXISTS Heck (
|
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE,
|
|
||||||
count INTEGER NOT NULL
|
|
||||||
)"
|
|
||||||
)
|
|
||||||
.execute(&pool)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
debug!("Table Heck exists or was created");
|
|
||||||
|
|
||||||
// Arknights row roll counting
|
|
||||||
sqlx::query!(
|
|
||||||
"CREATE TABLE IF NOT EXISTS RollCount (
|
|
||||||
user_id TEXT PRIMARY KEY UNIQUE NOT NULL,
|
|
||||||
count INTEGER NOT NULL
|
|
||||||
)"
|
|
||||||
)
|
|
||||||
.execute(&pool)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
debug!("Table RollCount exists or was created");
|
|
||||||
|
|
||||||
sqlx::query!(
|
|
||||||
"CREATE TABLE IF NOT EXISTS OperatorCount (
|
|
||||||
user_id TEXT NOT NULL,
|
|
||||||
operator TEXT NOT NULL,
|
|
||||||
count INTEGER NOT NULL,
|
|
||||||
UNIQUE(user_id, operator)
|
|
||||||
)"
|
|
||||||
)
|
|
||||||
.execute(&pool)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
debug!("Table OperatorCount exists or was created");
|
|
||||||
|
|
||||||
if sqlx::query!("SELECT count FROM Heck")
|
|
||||||
.fetch_all(&pool)
|
|
||||||
.await?
|
|
||||||
.is_empty()
|
|
||||||
{
|
|
||||||
debug!("No entries in Heck, inserting default one");
|
|
||||||
sqlx::query!("INSERT INTO Heck (count) VALUES (0)")
|
|
||||||
.execute(&pool)
|
|
||||||
.await?;
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(pool)
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue