Store room times as epoch time

Resets migrations because nobody except me is using cove anyways and
this is easier than keeping backwards compatibility.
This commit is contained in:
Joscha 2022-08-03 00:22:07 +02:00
parent cbe2b2e10e
commit b57c70dd01
3 changed files with 13 additions and 18 deletions

View file

@ -7,7 +7,6 @@ use anyhow::bail;
use cookie::{Cookie, CookieJar}; use cookie::{Cookie, CookieJar};
use log::{error, info, warn}; use log::{error, info, warn};
use parking_lot::Mutex; use parking_lot::Mutex;
use time::OffsetDateTime;
use tokio::sync::{mpsc, oneshot}; use tokio::sync::{mpsc, oneshot};
use tokio::{select, task}; use tokio::{select, task};
use tokio_tungstenite::tungstenite; use tokio_tungstenite::tungstenite;
@ -15,6 +14,7 @@ use tokio_tungstenite::tungstenite::client::IntoClientRequest;
use tokio_tungstenite::tungstenite::handshake::client::Response; use tokio_tungstenite::tungstenite::handshake::client::Response;
use tokio_tungstenite::tungstenite::http::{header, HeaderValue}; use tokio_tungstenite::tungstenite::http::{header, HeaderValue};
use crate::euph::api::Time;
use crate::macros::ok_or_return; use crate::macros::ok_or_return;
use crate::ui::UiEvent; use crate::ui::UiEvent;
use crate::vault::{EuphVault, Vault}; use crate::vault::{EuphVault, Vault};
@ -212,7 +212,7 @@ impl State {
} }
Data::SnapshotEvent(d) => { Data::SnapshotEvent(d) => {
info!("e&{}: successfully joined", self.name); info!("e&{}: successfully joined", self.name);
self.vault.join(OffsetDateTime::now_utc()); self.vault.join(Time::now());
self.last_msg_id = Some(d.log.last().map(|m| m.id)); self.last_msg_id = Some(d.log.last().map(|m| m.id));
self.vault.add_messages(d.log, None); self.vault.add_messages(d.log, None);
} }

View file

@ -86,7 +86,7 @@ impl EuphVault {
&self.room &self.room
} }
pub fn join(&self, time: OffsetDateTime) { pub fn join(&self, time: Time) {
let request = EuphRequest::Join { let request = EuphRequest::Join {
room: self.room.clone(), room: self.room.clone(),
time, time,
@ -221,7 +221,7 @@ pub(super) enum EuphRequest {
}, },
Join { Join {
room: String, room: String,
time: OffsetDateTime, time: Time,
}, },
Delete { Delete {
room: String, room: String,
@ -382,7 +382,7 @@ impl EuphRequest {
Ok(()) Ok(())
} }
fn join(conn: &mut Connection, room: String, time: OffsetDateTime) -> rusqlite::Result<()> { fn join(conn: &mut Connection, room: String, time: Time) -> rusqlite::Result<()> {
conn.execute( conn.execute(
" "
INSERT INTO euph_rooms (room, first_joined, last_joined) INSERT INTO euph_rooms (room, first_joined, last_joined)

View file

@ -7,6 +7,7 @@ pub fn migrate(conn: &mut Connection) -> rusqlite::Result<()> {
tx.query_row("SELECT * FROM pragma_user_version", [], |r| r.get(0))?; tx.query_row("SELECT * FROM pragma_user_version", [], |r| r.get(0))?;
let total = MIGRATIONS.len(); let total = MIGRATIONS.len();
assert!(user_version <= total, "malformed database schema");
for (i, migration) in MIGRATIONS.iter().enumerate().skip(user_version) { for (i, migration) in MIGRATIONS.iter().enumerate().skip(user_version) {
println!("Migrating vault from {} to {} (out of {})", i, i + 1, total); println!("Migrating vault from {} to {} (out of {})", i, i + 1, total);
migration(&mut tx)?; migration(&mut tx)?;
@ -16,15 +17,15 @@ pub fn migrate(conn: &mut Connection) -> rusqlite::Result<()> {
tx.commit() tx.commit()
} }
const MIGRATIONS: [fn(&mut Transaction<'_>) -> rusqlite::Result<()>; 2] = [m1, m2]; const MIGRATIONS: [fn(&mut Transaction<'_>) -> rusqlite::Result<()>; 1] = [m1];
fn m1(tx: &mut Transaction<'_>) -> rusqlite::Result<()> { fn m1(tx: &mut Transaction<'_>) -> rusqlite::Result<()> {
tx.execute_batch( tx.execute_batch(
" "
CREATE TABLE euph_rooms ( CREATE TABLE euph_rooms (
room TEXT NOT NULL PRIMARY KEY, room TEXT NOT NULL PRIMARY KEY,
first_joined TEXT NOT NULL, first_joined INT NOT NULL,
last_joined TEXT NOT NULL last_joined INT NOT NULL
) STRICT; ) STRICT;
CREATE TABLE euph_msgs ( CREATE TABLE euph_msgs (
@ -67,6 +68,10 @@ fn m1(tx: &mut Transaction<'_>) -> rusqlite::Result<()> {
CHECK (start IS NULL OR end IS NOT NULL) CHECK (start IS NULL OR end IS NOT NULL)
) STRICT; ) STRICT;
CREATE TABLE euph_cookies (
cookie TEXT NOT NULL
) STRICT;
CREATE INDEX euph_idx_msgs_room_id_parent CREATE INDEX euph_idx_msgs_room_id_parent
ON euph_msgs (room, id, parent); ON euph_msgs (room, id, parent);
@ -75,13 +80,3 @@ fn m1(tx: &mut Transaction<'_>) -> rusqlite::Result<()> {
", ",
) )
} }
fn m2(tx: &mut Transaction<'_>) -> rusqlite::Result<()> {
tx.execute_batch(
"
CREATE TABLE euph_cookies (
cookie TEXT NOT NULL
) STRICT;
",
)
}