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;
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");

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()?;
tx.execute(
"CREATE TABLE peer (
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,
)",
()
)?;
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 (
);
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),
)",
()
)?;
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 (
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)",
()
FOREIGN KEY(peer_id) REFERENCES peer(id)
)",
)?;
tx.pragma_update(None, "user_version", 1)?;
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(conn).expect("Local database migration should be done correctly")
})
}
pub trait LocalDatabaseConnection {
pub trait LocalDatabaseConnection: Sized {
fn from_path<P>(path: &P) -> Self
where
P: AsRef<Path>;
fn from_storage_config<T>(config: &T) -> Self
where
T: AsRef<StorageConfig>
{
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<Path>
{
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
}
}

View file

@ -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 {