updated db schema
This commit is contained in:
parent
d033543d0b
commit
d01cfcfe0a
6 changed files with 64 additions and 0 deletions
|
@ -10,3 +10,4 @@ pretty_env_logger = "0.5"
|
|||
tokio = { version = "1.8", features = ["rt-multi-thread", "macros"] }
|
||||
diesel = { version = "2.2.4", features = ["sqlite"] }
|
||||
chrono = "0.4.38"
|
||||
dotenvy = "0.15.7"
|
||||
|
|
9
diesel.toml
Normal file
9
diesel.toml
Normal file
|
@ -0,0 +1,9 @@
|
|||
# For documentation on how to configure this file,
|
||||
# see https://diesel.rs/guides/configuring-diesel-cli
|
||||
|
||||
[print_schema]
|
||||
file = "src/db/schema.rs"
|
||||
custom_type_derives = ["diesel::query_builder::QueryId", "Clone"]
|
||||
|
||||
[migrations_directory]
|
||||
dir = "migrations"
|
20
src/db/action.rs
Normal file
20
src/db/action.rs
Normal file
|
@ -0,0 +1,20 @@
|
|||
use crate::db::schema::users::dsl::*;
|
||||
use diesel::prelude::*;
|
||||
use teloxide::types::ChatId;
|
||||
|
||||
pub fn set_reminder_time(conn: &mut SqliteConnection, _chat_id: ChatId, time: &str) {
|
||||
diesel::insert_into(users)
|
||||
.values((chat_id.eq(_chat_id.0), reminder_time.eq(time)))
|
||||
.on_conflict(chat_id)
|
||||
.do_update()
|
||||
.set(reminder_time.eq(time))
|
||||
.execute(conn)
|
||||
.expect("Error on setting reminder time");
|
||||
}
|
||||
|
||||
pub fn clear_reminder_time(conn: &mut SqliteConnection, _chat_id: i64) {
|
||||
diesel::delete(users.filter(chat_id.eq(_chat_id)))
|
||||
.filter(chat_id.eq(_chat_id))
|
||||
.execute(conn)
|
||||
.expect("Error on clearing reminder time");
|
||||
}
|
15
src/db/mod.rs
Normal file
15
src/db/mod.rs
Normal file
|
@ -0,0 +1,15 @@
|
|||
use diesel::{Connection, SqliteConnection};
|
||||
use dotenvy::dotenv;
|
||||
use std::env;
|
||||
|
||||
pub mod action;
|
||||
pub mod model;
|
||||
pub mod schema;
|
||||
|
||||
pub fn establish_connection() -> SqliteConnection {
|
||||
dotenv().ok();
|
||||
|
||||
let database_uri = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
|
||||
SqliteConnection::establish(&database_uri)
|
||||
.unwrap_or_else(|_| panic!("Error connecting to {database_uri}"))
|
||||
}
|
11
src/db/model.rs
Normal file
11
src/db/model.rs
Normal file
|
@ -0,0 +1,11 @@
|
|||
use diesel::prelude::*;
|
||||
|
||||
#[derive(Queryable, Selectable)]
|
||||
#[diesel(table_name = crate::db::schema::users)]
|
||||
#[diesel(check_for_backend(diesel::sqlite::Sqlite))]
|
||||
pub struct Users {
|
||||
pub id: i32,
|
||||
pub chat_id: i64,
|
||||
pub reminder_time: String,
|
||||
pub last_reminder_time: Option<String>,
|
||||
}
|
8
src/db/schema.rs
Normal file
8
src/db/schema.rs
Normal file
|
@ -0,0 +1,8 @@
|
|||
diesel::table! {
|
||||
users (id) {
|
||||
id -> Integer,
|
||||
chat_id -> BigInt,
|
||||
reminder_time -> Text,
|
||||
last_reminder_time -> Nullable<Text>
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue