diff --git a/db/db-core/src/lib.rs b/db/db-core/src/lib.rs index 1a102bc..1f9f557 100644 --- a/db/db-core/src/lib.rs +++ b/db/db-core/src/lib.rs @@ -58,6 +58,13 @@ pub mod dev { pub use async_trait::async_trait; } +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +/// Data related to a Starchart instance +pub struct Starchart { + /// URL of the Starchart instance + pub instance_url: String, +} + #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] /// create a new forge on the database pub struct CreateForge<'a> { @@ -232,6 +239,13 @@ pub trait SCDatabase: std::marker::Send + std::marker::Sync + CloneSPDatabase { /// Add Starchart instance to introducer async fn add_starchart_to_introducer(&self, url: &Url) -> DBResult<()>; + + /// Get all introduced Starchart instances + async fn get_all_introduced_starchart_instances( + &self, + offset: u32, + limit: u32, + ) -> DBResult>; } /// Trait to clone SCDatabase diff --git a/db/db-core/src/tests.rs b/db/db-core/src/tests.rs index c2b6cf0..ad86b72 100644 --- a/db/db-core/src/tests.rs +++ b/db/db-core/src/tests.rs @@ -108,3 +108,15 @@ pub async fn forge_type_exists_helper(db: &T) { println!("Testing forge implementation exists for: {}", f.to_str()); assert!(db.forge_type_exists(&f).await.unwrap()); } + +/// test if all instance introducer methods work +pub async fn instance_introducer_helper(db: &T, instance_url: &Url) { + db.add_starchart_to_introducer(instance_url).await.unwrap(); + let instances = db + .get_all_introduced_starchart_instances(0, 100) + .await + .unwrap(); + assert!(instances + .iter() + .any(|i| i.instance_url == instance_url.as_str())); +} diff --git a/db/db-sqlx-sqlite/src/lib.rs b/db/db-sqlx-sqlite/src/lib.rs index 406728c..ac12ded 100644 --- a/db/db-sqlx-sqlite/src/lib.rs +++ b/db/db-sqlx-sqlite/src/lib.rs @@ -831,6 +831,29 @@ impl SCDatabase for Database { .map_err(map_register_err)?; Ok(()) } + + /// Get all introduced Starchart instances + async fn get_all_introduced_starchart_instances( + &self, + offset: u32, + limit: u32, + ) -> DBResult> { + let s = sqlx::query_as!( + Starchart, + "SELECT + instance_url + FROM + starchart_introducer + LIMIT $1 OFFSET $2; + ", + limit, + offset + ) + .fetch_all(&self.pool) + .await + .map_err(|e| DBError::DBError(Box::new(e)))?; + Ok(s) + } } fn now_unix_time_stamp() -> i64 { diff --git a/db/db-sqlx-sqlite/src/tests.rs b/db/db-sqlx-sqlite/src/tests.rs index 6874a16..8e4de3f 100644 --- a/db/db-sqlx-sqlite/src/tests.rs +++ b/db/db-sqlx-sqlite/src/tests.rs @@ -89,6 +89,17 @@ async fn everything_works() { .await; } +#[actix_rt::test] +async fn introducer_works() { + let url = env::var("SQLITE_DATABASE_URL").expect("Set SQLITE_DATABASE_URL env var"); + let pool_options = SqlitePoolOptions::new().max_connections(2); + let connection_options = ConnectionOptions::Fresh(Fresh { pool_options, url }); + let db = connection_options.connect().await.unwrap(); + + let instance_url = Url::parse("https://introducer_works_sqlite_sqlx.example.com").unwrap(); + instance_introducer_helper(&db, &instance_url).await; +} + #[actix_rt::test] async fn forge_type_exists() { let url = env::var("SQLITE_DATABASE_URL").expect("Set SQLITE_DATABASE_URL env var");