Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion reflection-doc/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions reflection-node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
mod author_tracker;
mod database;
mod ephemeral_message;
mod migration;
mod node;
mod topic_store;
mod topic_stream;
Expand Down
14 changes: 14 additions & 0 deletions reflection-node/src/migration.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
29 changes: 18 additions & 11 deletions reflection-node/src/node.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::path::{Path, PathBuf};
use std::path::PathBuf;
use std::sync::{Arc, LazyLock};

use chrono::{DateTime, Utc};
Expand All @@ -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};
Expand Down Expand Up @@ -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),
}
Expand Down Expand Up @@ -89,7 +93,7 @@ impl Node {
pub async fn new(
signing_key: SigningKey,
network_id: impl Into<NetworkId>,
db_location: Option<&Path>,
base_path: Option<PathBuf>,
) -> Result<Self, NodeError> {
let runtime = if let Ok(handle) = tokio::runtime::Handle::try_current() {
OwnedRuntimeOrHandle::Handle(handle)
Expand All @@ -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),
Expand Down Expand Up @@ -213,10 +212,13 @@ impl NodeInner {
pub async fn new(
signing_key: SigningKey,
network_id: impl Into<NetworkId>,
db_file: Option<PathBuf>,
base_path: Option<PathBuf>,
) -> Result<Self, NodeError> {
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?;

Expand All @@ -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(),
Expand Down
Loading