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

View File

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

View File

@@ -102,8 +102,9 @@ async fn main() {
let now = Utc::now(); let now = Utc::now();
let next_appointment = db.lock().await.transaction(|db| { let next_appointment = db.lock().await.transaction(|db| {
use schema::chat::dsl::*; use schema::chats::dsl::*;
chat.select(next_appointment_start) chats
.select(next_appointment_start)
.filter(next_appointment_start.is_not_null()) .filter(next_appointment_start.is_not_null())
.filter(next_appointment_start.gt(now.timestamp())) .filter(next_appointment_start.gt(now.timestamp()))
.order(next_appointment_start.asc()) .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 // 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<()> { async fn check_task(bot: &Throttle<Bot>, config: &Config, db: &Database) -> Result<()> {
let chats = db.lock().await.transaction::<_, Error, _>(|db| { let chats = db.lock().await.transaction::<_, Error, _>(|db| {
use schema::chat::dsl::*; use schema::chats::dsl::*;
use schema::reminder::dsl::*; use schema::reminders::dsl::*;
let chats = chat.load::<DbChat>(db)?; 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()) .select(DbReminder::as_select())
.order(days_ahead.asc()) .order(days_ahead.asc())
.load(db)?; .load(db)?;
let reminders_per_chat = reminders let reminders_per_chat = db_reminders
.grouped_by(&chats) .grouped_by(&db_chats)
.into_iter() .into_iter()
.zip(chats) .zip(db_chats)
.collect::<Vec<_>>(); .collect::<Vec<_>>();
Ok(reminders_per_chat) Ok(reminders_per_chat)
@@ -241,8 +242,8 @@ async fn check_task(bot: &Throttle<Bot>, config: &Config, db: &Database) -> Resu
.await?; .await?;
db.lock().await.transaction(|db| { db.lock().await.transaction(|db| {
use schema::chat::dsl::*; use schema::chats::dsl::*;
diesel::update(chat.filter(telegram_id.eq(chat_info.id))) diesel::update(chats.filter(telegram_id.eq(chat_info.id)))
.set((last_reminder.eq(now.timestamp()),)) .set((last_reminder.eq(now.timestamp()),))
.execute(db) .execute(db)
})?; })?;

View File

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