database migration

This commit is contained in:
jellywx 2021-04-17 16:57:46 +01:00
parent 0a9624d12d
commit 4a64238ee4
5 changed files with 206 additions and 116 deletions

2
Cargo.lock generated
View File

@ -1270,7 +1270,7 @@ dependencies = [
[[package]] [[package]]
name = "reminder_rs" name = "reminder_rs"
version = "1.4.13" version = "1.5.0"
dependencies = [ dependencies = [
"Inflector", "Inflector",
"chrono", "chrono",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "reminder_rs" name = "reminder_rs"
version = "1.4.13" version = "1.5.0"
authors = ["jellywx <judesouthworth@pm.me>"] authors = ["jellywx <judesouthworth@pm.me>"]
edition = "2018" edition = "2018"

File diff suppressed because one or more lines are too long

View File

@ -193,7 +193,7 @@ UPDATE reminders
INNER JOIN `channels` INNER JOIN `channels`
ON `channels`.id = reminders.channel_id ON `channels`.id = reminders.channel_id
SET SET
reminders.`time` = reminders.`time` + ? reminders.`utc_time` = reminders.`utc_time` + ?
WHERE channels.guild_id = ? WHERE channels.guild_id = ?
", ",
displacement, displacement,
@ -205,7 +205,7 @@ UPDATE reminders
} else { } else {
sqlx::query!( sqlx::query!(
" "
UPDATE reminders SET `time` = `time` + ? WHERE reminders.channel_id = ? UPDATE reminders SET `utc_time` = `utc_time` + ? WHERE reminders.channel_id = ?
", ",
displacement, displacement,
user_data.dm_channel user_data.dm_channel
@ -345,11 +345,11 @@ impl LookFlags {
struct LookReminder { struct LookReminder {
id: u32, id: u32,
time: u32, time: NaiveDateTime,
interval: Option<u32>, interval: Option<u32>,
channel: u64, channel: u64,
content: String, content: String,
description: Option<String>, description: String,
} }
impl LookReminder { impl LookReminder {
@ -357,7 +357,7 @@ impl LookReminder {
if self.content.len() > 0 { if self.content.len() > 0 {
self.content.clone() self.content.clone()
} else { } else {
self.description.clone().unwrap_or(String::from("")) self.description.clone()
} }
} }
@ -370,7 +370,7 @@ impl LookReminder {
) -> String { ) -> String {
let time_display = match flags.time_display { let time_display = match flags.time_display {
TimeDisplayType::Absolute => timezone TimeDisplayType::Absolute => timezone
.timestamp(self.time as i64, 0) .from_utc_datetime(&self.time)
.format(meridian.fmt_str()) .format(meridian.fmt_str())
.to_string(), .to_string(),
@ -380,7 +380,7 @@ impl LookReminder {
.unwrap() .unwrap()
.as_secs(); .as_secs();
longhand_displacement((self.time as u64).checked_sub(now).unwrap_or(1)) longhand_displacement((self.time.timestamp() as u64).checked_sub(now).unwrap_or(1))
} }
}; };
@ -434,26 +434,18 @@ async fn look(ctx: &Context, msg: &Message, args: String) {
LookReminder, LookReminder,
" "
SELECT SELECT
reminders.id, reminders.time, reminders.interval, channels.channel, messages.content, embeds.description reminders.id, reminders.utc_time AS time, reminders.interval, channels.channel, reminders.content, reminders.embed_description AS description
FROM FROM
reminders reminders
INNER JOIN INNER JOIN
channels channels
ON ON
reminders.channel_id = channels.id reminders.channel_id = channels.id
INNER JOIN
messages
ON
messages.id = reminders.message_id
LEFT JOIN
embeds
ON
embeds.id = messages.embed_id
WHERE WHERE
channels.channel = ? AND channels.channel = ? AND
FIND_IN_SET(reminders.enabled, ?) FIND_IN_SET(reminders.enabled, ?)
ORDER BY ORDER BY
reminders.time reminders.utc_time
LIMIT LIMIT
? ?
", ",
@ -509,21 +501,13 @@ async fn delete(ctx: &Context, msg: &Message, _args: String) {
LookReminder, LookReminder,
" "
SELECT SELECT
reminders.id, reminders.time, reminders.interval, channels.channel, messages.content, embeds.description reminders.id, reminders.utc_time AS time, reminders.interval, channels.channel, reminders.content, reminders.embed_description AS description
FROM FROM
reminders reminders
LEFT OUTER JOIN INNER JOIN
channels channels
ON ON
channels.id = reminders.channel_id channels.id = reminders.channel_id
INNER JOIN
messages
ON
messages.id = reminders.message_id
LEFT JOIN
embeds
ON
embeds.id = messages.embed_id
WHERE WHERE
FIND_IN_SET(channels.channel, ?) FIND_IN_SET(channels.channel, ?)
", ",
@ -536,21 +520,13 @@ WHERE
LookReminder, LookReminder,
" "
SELECT SELECT
reminders.id, reminders.time, reminders.interval, channels.channel, messages.content, embeds.description reminders.id, reminders.utc_time AS time, reminders.interval, channels.channel, reminders.content, reminders.embed_description AS description
FROM FROM
reminders reminders
LEFT OUTER JOIN INNER JOIN
channels channels
ON ON
channels.id = reminders.channel_id channels.id = reminders.channel_id
INNER JOIN
messages
ON
messages.id = reminders.message_id
LEFT JOIN
embeds
ON
embeds.id = messages.embed_id
WHERE WHERE
channels.guild_id = (SELECT id FROM guilds WHERE guild = ?) channels.guild_id = (SELECT id FROM guilds WHERE guild = ?)
", ",
@ -564,17 +540,9 @@ WHERE
LookReminder, LookReminder,
" "
SELECT SELECT
reminders.id, reminders.time, reminders.interval, channels.channel, messages.content, embeds.description reminders.id, reminders.utc_time AS time, reminders.interval, channels.channel, reminders.content, reminders.embed_description AS description
FROM FROM
reminders reminders
INNER JOIN
messages
ON
reminders.message_id = messages.id
LEFT JOIN
embeds
ON
embeds.id = messages.embed_id
INNER JOIN INNER JOIN
channels channels
ON ON
@ -593,7 +561,7 @@ WHERE
let enumerated_reminders = reminders.iter().enumerate().map(|(count, reminder)| { let enumerated_reminders = reminders.iter().enumerate().map(|(count, reminder)| {
reminder_ids.push(reminder.id); reminder_ids.push(reminder.id);
let time = user_data.timezone().timestamp(reminder.time as i64, 0); let time = user_data.timezone().timestamp(reminder.time.timestamp(), 0);
format!( format!(
"**{}**: '{}' *<#{}>* at {}", "**{}**: '{}' *<#{}>* at {}",
@ -1047,70 +1015,41 @@ async fn countdown(ctx: &Context, msg: &Message, args: String) {
event_name, target_ts event_name, target_ts
); );
sqlx::query!(
"
INSERT INTO embeds (title, description, color) VALUES (?, ?, ?)
",
event_name,
description,
*THEME_COLOR
)
.execute(&pool)
.await
.unwrap();
let embed_id = sqlx::query!(
"
SELECT id FROM embeds WHERE title = ? AND description = ?
",
event_name,
description
)
.fetch_one(&pool)
.await
.unwrap();
sqlx::query!(
"
INSERT INTO messages (embed_id) VALUES (?)
",
embed_id.id
)
.execute(&pool)
.await
.unwrap();
sqlx::query!( sqlx::query!(
" "
INSERT INTO reminders ( INSERT INTO reminders (
`uid`, `uid`,
`name`, `name`,
`message_id`,
`channel_id`, `channel_id`,
`time`, `utc_time`,
`interval`, `interval`,
`method`, `expires`,
`set_by`, `embed_title`,
`expires` `embed_description`,
`embed_color`,
`set_by`
) VALUES ( ) VALUES (
?, ?,
'Countdown', 'Countdown',
(SELECT id FROM messages WHERE embed_id = ?),
(SELECT id FROM channels WHERE channel = ?), (SELECT id FROM channels WHERE channel = ?),
?, ?,
?, ?,
'countdown', FROM_UNIXTIME(?),
(SELECT id FROM users WHERE user = ?), ?,
FROM_UNIXTIME(?) ?,
?,
(SELECT id FROM users WHERE user = ?)
) )
", ",
generate_uid(), generate_uid(),
embed_id.id,
msg.channel_id.as_u64(), msg.channel_id.as_u64(),
first_time, first_time,
interval, interval,
target_ts,
event_name,
description,
*THEME_COLOR,
msg.author.id.as_u64(), msg.author.id.as_u64(),
target_ts
) )
.execute(&pool) .execute(&pool)
.await .await
@ -1250,6 +1189,7 @@ async fn remind_command(ctx: &Context, msg: &Message, args: String, command: Rem
msg.guild_id, msg.guild_id,
&scope, &scope,
&time_parser, &time_parser,
timezone.to_string(),
expires_parser.as_ref().clone(), expires_parser.as_ref().clone(),
interval, interval,
&mut content, &mut content,
@ -1479,6 +1419,7 @@ async fn natural(ctx: &Context, msg: &Message, args: String) {
msg.guild_id, msg.guild_id,
&scope, &scope,
timestamp, timestamp,
user_data.timezone.clone(),
expires, expires,
interval.clone(), interval.clone(),
&mut content, &mut content,
@ -1618,6 +1559,7 @@ async fn create_reminder<'a, U: Into<u64>, T: TryInto<i64>>(
guild_id: Option<GuildId>, guild_id: Option<GuildId>,
scope_id: &ReminderScope, scope_id: &ReminderScope,
time_parser: T, time_parser: T,
timezone: String,
expires_parser: Option<T>, expires_parser: Option<T>,
interval: Option<i64>, interval: Option<i64>,
content: &mut Content, content: &mut Content,
@ -1698,29 +1640,17 @@ async fn create_reminder<'a, U: Into<u64>, T: TryInto<i64>>(
} else { } else {
sqlx::query!( sqlx::query!(
" "
INSERT INTO messages (content, tts, attachment, attachment_name) VALUES (?, ?, ?, ?) INSERT INTO reminders (uid, content, tts, attachment, attachment_name, channel_id, `utc_time`, timezone, expires, `interval`, set_by) VALUES
(?, ?, ?, ?, ?, ?, FROM_UNIXTIME(?), ?, FROM_UNIXTIME(?), ?, (SELECT id FROM users WHERE user = ? LIMIT 1))
", ",
generate_uid(),
content.content, content.content,
content.tts, content.tts,
content.attachment, content.attachment,
content.attachment_name, content.attachment_name,
)
.execute(&pool.clone())
.await
.unwrap();
sqlx::query!(
"
INSERT INTO reminders (uid, message_id, channel_id, time, expires, `interval`, method, set_by) VALUES
(?,
(SELECT id FROM messages WHERE content = ? ORDER BY id DESC LIMIT 1),
?, ?, FROM_UNIXTIME(?), ?, 'remind',
(SELECT id FROM users WHERE user = ? LIMIT 1))
",
generate_uid(),
content.content,
db_channel_id, db_channel_id,
time as u32, time,
timezone,
expires, expires,
interval, interval,
user_id user_id

View File

@ -32,7 +32,7 @@ enum ParseType {
} }
pub struct TimeParser { pub struct TimeParser {
timezone: Tz, pub timezone: Tz,
inverted: bool, inverted: bool,
time_string: String, time_string: String,
parse_type: ParseType, parse_type: ParseType,