Rework sqlite db to implement nostr_database trait

This commit is contained in:
Tony Klink 2024-01-25 20:43:46 -06:00
parent 0bbce25d39
commit 5b7d0b7938
Signed by: klink
GPG key ID: 85175567C4D19231
10 changed files with 1342 additions and 814 deletions

View file

@ -21,3 +21,10 @@ CREATE TABLE tags (
CREATE INDEX idx_tags_tag ON tags (tag);
CREATE INDEX idx_tags_value ON tags (value);
CREATE INDEX idx_tags_event_id ON tags (event_id);
CREATE TABLE deleted_coordinates (
coordinate TEXT NOT NULL,
created_at INTEGER NOT NULL
);
CREATE INDEX idx_coordinates_coordinate ON coordinates (coordinate);

View file

@ -1,2 +1,6 @@
PRAGMA encoding = "UTF-8";
PRAGMA journal_mode=WAL;
PRAGMA foreign_keys = ON;
PRAGMA auto_vacuum = FULL;
PRAGMA journal_size_limit=32768;
PRAGMA mmap_size = 17179869184; -- cap mmap at 16GB

View file

@ -0,0 +1,7 @@
CREATE TABLE IF NOT EXISTS event_seen_by_relays (
id INTEGER PRIMARY KEY AUTOINCREMENT,
event_id TEXT NOT NULL,
relay_url TEXT NOT NULL
);
CREATE UNIQUE INDEX IF NOT EXISTS event_seen_by_relays_index ON event_seen_by_relays(event_id,relay_url);

View file

@ -0,0 +1,50 @@
use rusqlite::Connection;
use rusqlite_migration::{Migrations, M};
pub struct MigrationRunner {}
impl MigrationRunner {
pub fn up(connection: &mut Connection) -> bool {
let m_create_events = include_str!("./1697409647688_create_events.sql");
let m_event_seen_by_relays = include_str!("1706115586021_event_seen_by_relays.sql");
let m_add_realys = include_str!("./1697410161900_add_relays.sql");
let m_events_fts = include_str!("./1697410223576_events_fts.sql");
let m_users = include_str!("./1697410294265_users.sql");
let m_unattached_media = include_str!("./1697410480767_unattached_media.sql");
let m_pragma = include_str!("./1697410424624_pragma.sql");
let migrations = Migrations::new(vec![
M::up(m_create_events),
M::up(m_event_seen_by_relays),
M::up(m_add_realys),
M::up(m_events_fts),
M::up(m_users),
M::up(m_unattached_media),
M::up(m_pragma),
]);
match migrations.to_latest(connection) {
Ok(()) => true,
Err(err) => {
log::error!("Migrations failed: {}", err.to_string());
false
}
}
}
}
#[cfg(test)]
mod tests {
use rusqlite::Connection;
use super::MigrationRunner;
#[test]
fn get_sql_path() {
let mut connection = Connection::open_in_memory().unwrap();
let runner = MigrationRunner::up(&mut connection);
assert!(runner);
}
}