Rename database tables to use plural names

This commit is contained in:
Manuel Vögele
2024-01-22 16:45:21 +01:00
parent 97ca239ee1
commit d338f89a15
6 changed files with 42 additions and 35 deletions

View File

@@ -0,0 +1,2 @@
ALTER TABLE chats RENAME TO chat;
ALTER TABLE reminders RENAME TO reminder;

View File

@@ -0,0 +1,2 @@
ALTER TABLE chat RENAME TO chats;
ALTER TABLE reminder RENAME TO reminders;

View File

@@ -85,8 +85,8 @@ async fn handle_my_chat_member(msg: ChatMemberUpdated, db: Database) -> Result<(
match msg.new_chat_member.kind {
ChatMemberKind::Left | ChatMemberKind::Banned(_) => {
db.lock().await.transaction::<_, Error, _>(|db| {
use schema::chat::dsl::*;
diesel::delete(chat.filter(telegram_id.eq(msg.chat.id.0))).execute(db)?;
use schema::chats::dsl::*;
diesel::delete(chats.filter(telegram_id.eq(msg.chat.id.0))).execute(db)?;
Ok(())
})?;
}
@@ -106,9 +106,9 @@ async fn set_calendar(bot: Throttle<Bot>, msg: Message, db: Database) -> Result<
}
db.lock().await.transaction::<_, Error, _>(|db| {
use schema::chat::dsl::*;
diesel::delete(chat.filter(telegram_id.eq(msg.chat.id.0))).execute(db)?;
diesel::insert_into(chat)
use schema::chats::dsl::*;
diesel::delete(chats.filter(telegram_id.eq(msg.chat.id.0))).execute(db)?;
diesel::insert_into(chats)
.values((telegram_id.eq(msg.chat.id.0), calendar.eq(url)))
.execute(db)?;
Ok(())
@@ -139,8 +139,8 @@ async fn set_locale(_bot: Throttle<Bot>, msg: Message, db: Database) -> Result<(
let new_locale = new_locale.unwrap();
db.lock().await.transaction::<_, Error, _>(|db| {
use schema::chat::dsl::*;
diesel::update(chat)
use schema::chats::dsl::*;
diesel::update(chats)
.filter(telegram_id.eq(msg.chat.id.0))
.set(locale.eq(new_locale))
.execute(db)?;
@@ -156,8 +156,9 @@ async fn set_remind_days_ahead(
db: Database,
) -> Result<(), Error> {
let chat = db.lock().await.transaction(|db| {
use schema::chat::dsl::*;
chat.filter(telegram_id.eq(msg.chat.id.0))
use schema::chats::dsl::*;
chats
.filter(telegram_id.eq(msg.chat.id.0))
.first::<DbChat>(db)
})?;
@@ -205,13 +206,13 @@ async fn set_remind_days_ahead(
}
db.lock().await.transaction(|db| {
use schema::reminder::dsl::*;
diesel::delete(reminder.filter(chat_id.eq(chat.db_id))).execute(db)?;
use schema::reminders::dsl::*;
diesel::delete(reminders.filter(chat_id.eq(chat.db_id))).execute(db)?;
let values = days
.iter()
.map(|days| (chat_id.eq(chat.db_id), days_ahead.eq(days)))
.collect::<Vec<_>>();
diesel::insert_into(reminder).values(&values).execute(db)
diesel::insert_into(reminders).values(&values).execute(db)
})?;
Ok(())
@@ -229,8 +230,9 @@ pub async fn fetch_and_announce_appointment(
let appointment = appointment.with_timezone(&Utc);
let entry = db.lock().await.transaction(|db| {
use schema::chat::dsl::*;
chat.filter(telegram_id.eq(chat_info.id))
use schema::chats::dsl::*;
chats
.filter(telegram_id.eq(chat_info.id))
.first::<DbChat>(db)
})?;
@@ -318,8 +320,8 @@ pub async fn fetch_and_announce_appointment(
};
db.lock().await.transaction(|db| {
use schema::chat::dsl::*;
diesel::update(chat.filter(telegram_id.eq(chat_info.id)))
use schema::chats::dsl::*;
diesel::update(chats.filter(telegram_id.eq(chat_info.id)))
.set((
next_appointment_start.eq(appointment.start.timestamp()),
next_appointment_end.eq(appointment.end.timestamp()),

View File

@@ -5,10 +5,10 @@ use diesel::{
};
use crate::appointment::Appointment;
use crate::schema::{chat, reminder};
use crate::schema::{chats, reminders};
#[derive(Queryable, Selectable, Identifiable)]
#[diesel(table_name = chat)]
#[diesel(table_name = chats)]
pub struct DbChat {
id: i32,
telegram_id: i64,
@@ -22,7 +22,7 @@ pub struct DbChat {
#[derive(Queryable, Selectable, Identifiable, Associations)]
#[diesel(belongs_to(DbChat, foreign_key=chat_id))]
#[diesel(table_name = reminder)]
#[diesel(table_name = reminders)]
pub struct DbReminder {
id: i32,
chat_id: i32,

View File

@@ -102,8 +102,9 @@ async fn main() {
let now = Utc::now();
let next_appointment = db.lock().await.transaction(|db| {
use schema::chat::dsl::*;
chat.select(next_appointment_start)
use schema::chats::dsl::*;
chats
.select(next_appointment_start)
.filter(next_appointment_start.is_not_null())
.filter(next_appointment_start.gt(now.timestamp()))
.order(next_appointment_start.asc())
@@ -149,19 +150,19 @@ struct Reminder<Tz: TimeZone> {
// Additionally, checks if it is time for a reminder and sends that reminder if necessary
async fn check_task(bot: &Throttle<Bot>, config: &Config, db: &Database) -> Result<()> {
let chats = db.lock().await.transaction::<_, Error, _>(|db| {
use schema::chat::dsl::*;
use schema::reminder::dsl::*;
let chats = chat.load::<DbChat>(db)?;
use schema::chats::dsl::*;
use schema::reminders::dsl::*;
let db_chats = chats.load::<DbChat>(db)?;
let reminders: Vec<DbReminder> = DbReminder::belonging_to(&chats)
let db_reminders: Vec<DbReminder> = DbReminder::belonging_to(&db_chats)
.select(DbReminder::as_select())
.order(days_ahead.asc())
.load(db)?;
let reminders_per_chat = reminders
.grouped_by(&chats)
let reminders_per_chat = db_reminders
.grouped_by(&db_chats)
.into_iter()
.zip(chats)
.zip(db_chats)
.collect::<Vec<_>>();
Ok(reminders_per_chat)
@@ -241,8 +242,8 @@ async fn check_task(bot: &Throttle<Bot>, config: &Config, db: &Database) -> Resu
.await?;
db.lock().await.transaction(|db| {
use schema::chat::dsl::*;
diesel::update(chat.filter(telegram_id.eq(chat_info.id)))
use schema::chats::dsl::*;
diesel::update(chats.filter(telegram_id.eq(chat_info.id)))
.set((last_reminder.eq(now.timestamp()),))
.execute(db)
})?;

View File

@@ -1,7 +1,7 @@
// @generated automatically by Diesel CLI.
diesel::table! {
chat (id) {
chats (id) {
id -> Integer,
telegram_id -> BigInt,
calendar -> Text,
@@ -14,16 +14,16 @@ diesel::table! {
}
diesel::table! {
reminder (id) {
reminders (id) {
id -> Integer,
chat_id -> Integer,
days_ahead -> BigInt,
}
}
diesel::joinable!(reminder -> chat (chat_id));
diesel::joinable!(reminders -> chats (chat_id));
diesel::allow_tables_to_appear_in_same_query!(
chat,
reminder,
chats,
reminders,
);