Skip to content

Commit 857f523

Browse files
authored
Add new database table (#251)
1 parent 4269ca4 commit 857f523

File tree

6 files changed

+72
-0
lines changed

6 files changed

+72
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use crate::get_database_connection;
2+
use crate::models::comparable_applications::{
3+
ActiveModel, ApplicationComparableData, Entity as ComparableApplication,
4+
Model as ComparableApplicationModel,
5+
};
6+
use sea_orm::{entity::*, DbErr};
7+
8+
pub async fn create_comparable_application(
9+
client_address: &str,
10+
comparable_data: &ApplicationComparableData,
11+
) -> Result<(), sea_orm::DbErr> {
12+
let conn = get_database_connection().await?;
13+
let new_comparable_data = ActiveModel {
14+
client_address: Set(client_address.to_string()),
15+
application: Set(comparable_data.clone()),
16+
};
17+
new_comparable_data.insert(&conn).await?;
18+
Ok(())
19+
}
20+
21+
pub async fn get_comparable_applications() -> Result<Vec<ComparableApplicationModel>, DbErr> {
22+
let conn = get_database_connection().await?;
23+
let response = ComparableApplication::find().all(&conn).await?;
24+
Ok(response)
25+
}

fplus-database/src/database/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ pub mod allocation_amounts;
22
pub mod allocators;
33
pub mod applications;
44
pub mod autoallocations;
5+
pub mod comparable_applications;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use sea_orm::{entity::prelude::*, FromJsonQueryResult};
2+
use serde::{Deserialize, Serialize};
3+
4+
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
5+
#[sea_orm(table_name = "comparable_applications")]
6+
pub struct Model {
7+
#[sea_orm(primary_key, auto_increment = false)]
8+
pub client_address: String,
9+
#[sea_orm(column_type = "JsonBinary")]
10+
pub application: ApplicationComparableData,
11+
}
12+
13+
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
14+
pub enum Relation {}
15+
16+
impl ActiveModelBehavior for ActiveModel {}
17+
18+
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, FromJsonQueryResult)]
19+
pub struct ApplicationComparableData {
20+
pub project_desc: String,
21+
pub stored_data_desc: String,
22+
pub data_owner_name: String,
23+
pub data_set_sample: String,
24+
}

fplus-database/src/models/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ pub mod allocation_amounts;
66
pub mod allocators;
77
pub mod applications;
88
pub mod autoallocations;
9+
pub mod comparable_applications;

fplus-lib/src/core/mod.rs

+15
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use alloy::primitives::Address;
55

66
use application::file::{SpsChangeRequest, StorageProviderChangeVerifier};
77
use chrono::{DateTime, Local, Utc};
8+
use fplus_database::database::comparable_applications::create_comparable_application;
9+
use fplus_database::models::comparable_applications::ApplicationComparableData;
810
use futures::future;
911
use octocrab::models::{
1012
pulls::PullRequest,
@@ -870,6 +872,19 @@ impl LDNApplication {
870872
application_id, e
871873
))
872874
})?;
875+
create_comparable_application(
876+
&application_id,
877+
&ApplicationComparableData {
878+
project_desc: application_file.project.history,
879+
stored_data_desc: application_file.project.stored_data_desc,
880+
data_owner_name: application_file.client.name,
881+
data_set_sample: application_file.project.data_sample_link,
882+
},
883+
)
884+
.await
885+
.map_err(|e| {
886+
LDNError::New(format!("Failed to create application in DB: {}", e))
887+
})?;
873888
}
874889

875890
Ok(LDNApplication {

manual-migrations/2025-01-31.sql

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CREATE TABLE comparable_applications
2+
(
3+
client_address text NOT NULL,
4+
application jsonb NOT NULL,
5+
PRIMARY KEY (client_address)
6+
);

0 commit comments

Comments
 (0)