Fix migration

This commit is contained in:
fluo10 2025-09-03 07:41:11 +09:00
parent 731817d20c
commit 7fe348e803
4 changed files with 41 additions and 62 deletions

View file

@ -1,9 +1,9 @@
mod v1; mod v1;
use rusqlite::{ffi::Error, Connection}; use rusqlite::{Error, Connection};
use tracing::{event, Level}; 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"); let version: u32 = con.pragma_query_value(None,"user_version", |row| row.get(0)).expect("Failed to get user_version");
if version < 1 { if version < 1 {
event!(Level::INFO, "Migrate local db to version 1"); event!(Level::INFO, "Migrate local db to version 1");

View file

@ -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()?; let tx = con.transaction()?;
tx.execute( tx.execute_batch(
"CREATE TABLE peer ( "BEGIN;
id INTEGER PRIMARY KEY, CREATE TABLE peer (
libp2p_peer_id TEXT UNIQUE NOT NULL, id INTEGER PRIMARY KEY,
created_at TEXT NOT NULL, libp2p_peer_id TEXT UNIQUE NOT NULL,
updated_at TEXT 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.pragma_update(None, "user_version", 1)?;
tx.commit()?; tx.commit()?;
Ok(())
} }

View file

@ -25,24 +25,21 @@ where
}) })
} }
fn migrate_once(conn: &Connection) -> () { fn migrate_once(conn: &mut Connection) -> () {
*MIGRATE_RESULT.get_or_init(|| { *MIGRATE_RESULT.get_or_init(|| {
migrate(conn).expect("Local database migration should be done correctly") migrate(conn).expect("Local database migration should be done correctly")
}) })
} }
pub trait LocalDatabaseConnection { pub trait LocalDatabaseConnection: Sized {
fn from_path<P>(path: &P) -> Self fn from_path<P>(path: &P) -> Self
where where
P: AsRef<Path>; P: AsRef<Path>;
fn from_storage_config<T>(config: &T) -> Self fn from_storage_config(config: &StorageConfig) -> Self {
where Self::from_path(&config.get_local_database_path())
T: AsRef<StorageConfig>
{
Self::from_path(&config.as_ref().get_local_database_path())
} }
fn from_global_storage_config() -> Self { 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<Path> P: AsRef<Path>
{ {
initialize_parent_directory(path); initialize_parent_directory(path);
let conn = Connection::open(path).expect("local database connection must be opened without error"); let mut conn = Connection::open(path).expect("local database connection must be opened without error");
migrate_once(&conn); migrate_once(&mut conn);
conn conn
} }
} }

View file

@ -6,7 +6,6 @@ use tokio::sync::{OnceCell, RwLock, RwLockReadGuard, RwLockWriteGuard};
mod config; mod config;
pub use config::*; pub use config::*;
mod database_connection;
use uuid::{ContextV7, Timestamp, Uuid}; use uuid::{ContextV7, Timestamp, Uuid};
pub fn generate_uuid() -> Uuid { pub fn generate_uuid() -> Uuid {