diff --git a/core/src/data/local/migration/mod.rs b/core/src/data/local/migration/mod.rs index 209a64f..b8aa28b 100644 --- a/core/src/data/local/migration/mod.rs +++ b/core/src/data/local/migration/mod.rs @@ -1,9 +1,9 @@ mod v1; -use rusqlite::{ffi::Error, Connection}; +use rusqlite::{Error, Connection}; use tracing::{event, Level}; -pub fn migrate(con: &Connection) -> Result<(), Error>{ +pub fn migrate(con: &mut Connection) -> Result<(), Error>{ let version: u32 = con.pragma_query_value(None,"user_version", |row| row.get(0)).expect("Failed to get user_version"); if version < 1 { event!(Level::INFO, "Migrate local db to version 1"); diff --git a/core/src/data/local/migration/v1.rs b/core/src/data/local/migration/v1.rs index 1b71d4d..3f2d0ff 100644 --- a/core/src/data/local/migration/v1.rs +++ b/core/src/data/local/migration/v1.rs @@ -1,55 +1,38 @@ -use rusqlite::{ffi::Error, Connection}; +use rusqlite::{Error, Connection}; -pub fn migrate(con: &Connection) -> Result<(), Error>{ +pub fn migrate(con: &mut Connection) -> Result<(), Error>{ let tx = con.transaction()?; - tx.execute( - "CREATE TABLE peer ( - id INTEGER PRIMARY KEY, - libp2p_peer_id TEXT UNIQUE NOT NULL, - created_at TEXT NOT NULL, - updated_at TEXT NOT NULL, - )", - () + tx.execute_batch( + "BEGIN; + CREATE TABLE peer ( + id INTEGER PRIMARY KEY, + libp2p_peer_id TEXT UNIQUE NOT NULL, + created_at TEXT NOT NULL, + updated_at TEXT NOT NULL, + ); + CREATE INDEX idx_peer_created_at ON peer(created_at); + CREATE INDEX idx_peer_updated_at ON peer(updated_at); + CREATE TABLE address ( + id INTEGER PRIMARY KEY, + peer_id INTEGER NOT NULL, + multiaddr TEXT UNIQUE NOT NULL, + created_at TEXT NOT NULL, + updated_at TEXT NOT NULL, + protocol TEXT NOT NULL, + FOREIGN KEY(peer_id) REFERENCES peer(id) + ); + CREATE INDEX idx_address_created_at ON address(created_at); + CREATE INDEX idx_address_updated_at ON address(updated_at); + CREATE TABLE authorized_peer ( + id INTEGER PRIMARY KEY, + peer_id INTEGER NOT NULL UNIQUE, + synced_at TEXT, + created_at TEXT NOT NULL, + updated_at TEXT NOT NULL, + FOREIGN KEY(peer_id) REFERENCES peer(id) + )", )?; - tx.execute( - "CREATE INDEX idx_peer_created_at ON peer(created_at)", - () - )?; - tx.execute( - "CREATE INDEX idx_peer_updated_at ON peer(updated_at)", - () - )?; - tx.execute( - "CREATE TABLE address ( - id INTEGER PRIMARY KEY, - peer_id INTEGER NOT NULL, - multiaddr TEXT UNIQUE NOT NULL, - created_at TEXT NOT NULL, - updated_at TEXT NOT NULL, - protocol TEXT NOT NULL, - FOREIGN KEY(peer_id) REFERENCES peer(id), - )", - () - )?; - tx.execute( - "CREATE INDEX idx_address_created_at ON address(created_at)", - () - )?; - tx.execute( - "CREATE INDEX idx_address_updated_at ON address(updated_at)", - () - )?; - tx.execute( - "CREATE TABLE authorized_peer ( - id INTEGER PRIMARY KEY, - peer_id INTEGER NOT NULL UNIQUE, - synced_at TEXT, - created_at TEXT NOT NULL, - updated_at TEXT NOT NULL, - FOREIGN KEY(peer_id) REFERENCES peer(id)", - () - )?; - tx.pragma_update(None, "user_version", 1)?; tx.commit()?; + Ok(()) } \ No newline at end of file diff --git a/core/src/data/local/mod.rs b/core/src/data/local/mod.rs index de01ac2..73fb7d5 100644 --- a/core/src/data/local/mod.rs +++ b/core/src/data/local/mod.rs @@ -25,24 +25,21 @@ where }) } -fn migrate_once(conn: &Connection) -> () { +fn migrate_once(conn: &mut Connection) -> () { *MIGRATE_RESULT.get_or_init(|| { migrate(conn).expect("Local database migration should be done correctly") }) } -pub trait LocalDatabaseConnection { +pub trait LocalDatabaseConnection: Sized { fn from_path

(path: &P) -> Self where P: AsRef; - fn from_storage_config(config: &T) -> Self - where - T: AsRef - { - Self::from_path(&config.as_ref().get_local_database_path()) + fn from_storage_config(config: &StorageConfig) -> Self { + Self::from_path(&config.get_local_database_path()) } fn from_global_storage_config() -> Self { - Self::from_storage_config(CONFIG.get_unchecked()) + Self::from_storage_config(&CONFIG.get_unchecked().storage) } } @@ -52,8 +49,8 @@ impl LocalDatabaseConnection for Connection { P: AsRef { initialize_parent_directory(path); - let conn = Connection::open(path).expect("local database connection must be opened without error"); - migrate_once(&conn); + let mut conn = Connection::open(path).expect("local database connection must be opened without error"); + migrate_once(&mut conn); conn } } \ No newline at end of file diff --git a/core/src/global/mod.rs b/core/src/global/mod.rs index db09e28..13cbd3a 100644 --- a/core/src/global/mod.rs +++ b/core/src/global/mod.rs @@ -6,7 +6,6 @@ use tokio::sync::{OnceCell, RwLock, RwLockReadGuard, RwLockWriteGuard}; mod config; pub use config::*; -mod database_connection; use uuid::{ContextV7, Timestamp, Uuid}; pub fn generate_uuid() -> Uuid {