diff --git a/reflection-doc/src/service.rs b/reflection-doc/src/service.rs index cc18830..4690ed1 100644 --- a/reflection-doc/src/service.rs +++ b/reflection-doc/src/service.rs @@ -173,7 +173,7 @@ impl Service { let signing_key = self.signing_key().0; let network_id = Hash::digest(NETWORK_NAME); let path = self.data_dir().and_then(|data_dir| data_dir.path()); - let node = Node::new(signing_key, network_id, path.as_deref()).await?; + let node = Node::new(signing_key, network_id, path.into()).await?; self.imp() .node diff --git a/reflection-node/src/lib.rs b/reflection-node/src/lib.rs index 84dd770..6be80de 100644 --- a/reflection-node/src/lib.rs +++ b/reflection-node/src/lib.rs @@ -8,6 +8,7 @@ mod author_tracker; mod database; mod ephemeral_message; +mod migration; mod node; mod topic_store; mod topic_stream; diff --git a/reflection-node/src/migration.rs b/reflection-node/src/migration.rs new file mode 100644 index 0000000..f8394ac --- /dev/null +++ b/reflection-node/src/migration.rs @@ -0,0 +1,14 @@ +use std::path::PathBuf; + +use p2panda::Node; +use thiserror::Error; + +#[derive(Debug, Error)] +pub enum MigrationError {} + +pub async fn run_p2panda_migrations( + node: &Node, + base_path: &PathBuf, +) -> Result<(), MigrationError> { + Ok(()) +} diff --git a/reflection-node/src/node.rs b/reflection-node/src/node.rs index b31c9ce..cc45978 100644 --- a/reflection-node/src/node.rs +++ b/reflection-node/src/node.rs @@ -1,4 +1,4 @@ -use std::path::{Path, PathBuf}; +use std::path::PathBuf; use std::sync::{Arc, LazyLock}; use chrono::{DateTime, Utc}; @@ -9,6 +9,7 @@ use tokio::sync::{Notify, RwLock}; use tracing::info; use crate::database::{database_pool, run_migrations}; +use crate::migration::run_p2panda_migrations; pub use crate::topic_store::TrackedAuthor; use crate::topic_store::{TopicRow, TrackedTopicStore}; use crate::topic_stream::{TopicStream, TopicStreamError, TopicStreamInner}; @@ -42,6 +43,9 @@ pub enum NodeError { #[error(transparent)] DatabaseMigration(#[from] sqlx::migrate::MigrateError), + #[error(transparent)] + NodeMigration(#[from] crate::migration::MigrationError), + #[error(transparent)] NodeSpawn(#[from] SpawnError), } @@ -89,7 +93,7 @@ impl Node { pub async fn new( signing_key: SigningKey, network_id: impl Into, - db_location: Option<&Path>, + base_path: Option, ) -> Result { let runtime = if let Ok(handle) = tokio::runtime::Handle::try_current() { OwnedRuntimeOrHandle::Handle(handle) @@ -101,14 +105,9 @@ impl Node { ) }; - let inner = { - let network_id = network_id.into(); - let db_file = db_location.map(|location| location.join(DATABASE_FILE)); - - runtime - .spawn(NodeInner::new(signing_key, network_id, db_file)) - .await?? - }; + let inner = runtime + .spawn(NodeInner::new(signing_key, network_id.into(), base_path)) + .await??; Ok(Self { inner: Arc::new(inner), @@ -213,10 +212,13 @@ impl NodeInner { pub async fn new( signing_key: SigningKey, network_id: impl Into, - db_file: Option, + base_path: Option, ) -> Result { let verifying_key = signing_key.verifying_key(); + let db_file = base_path + .clone() + .map(|location| location.join(DATABASE_FILE)); let pool = database_pool(db_file).await?; run_migrations(&pool).await?; @@ -236,6 +238,11 @@ impl NodeInner { let node = builder.spawn().await?; + // Ignore node migrations if this instance is running only temporarily in memory. + if let Some(ref base_path) = base_path { + run_p2panda_migrations(&node, base_path).await?; + } + Ok(Self { network: RwLock::new(node), shutdown_notifier: Notify::new(),