Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new database table #251

Merged
merged 2 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
25 changes: 25 additions & 0 deletions fplus-database/src/database/comparable_applications.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use crate::get_database_connection;
use crate::models::comparable_applications::{
ActiveModel, ApplicationComparableData, Entity as ComparableApplication,
Model as ComparableApplicationModel,
};
use sea_orm::{entity::*, DbErr};

pub async fn create_comparable_application(
client_address: &str,
comparable_data: &ApplicationComparableData,
) -> Result<(), sea_orm::DbErr> {
let conn = get_database_connection().await?;
let new_comparable_data = ActiveModel {
client_address: Set(client_address.to_string()),
application: Set(comparable_data.clone()),
};
new_comparable_data.insert(&conn).await?;
Ok(())
}

pub async fn get_comparable_applications() -> Result<Vec<ComparableApplicationModel>, DbErr> {
let conn = get_database_connection().await?;
let response = ComparableApplication::find().all(&conn).await?;
Ok(response)
}
1 change: 1 addition & 0 deletions fplus-database/src/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ pub mod allocation_amounts;
pub mod allocators;
pub mod applications;
pub mod autoallocations;
pub mod comparable_applications;
24 changes: 24 additions & 0 deletions fplus-database/src/models/comparable_applications.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use sea_orm::{entity::prelude::*, FromJsonQueryResult};
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
#[sea_orm(table_name = "comparable_applications")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub client_address: String,
#[sea_orm(column_type = "JsonBinary")]
pub application: ApplicationComparableData,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, FromJsonQueryResult)]
pub struct ApplicationComparableData {
pub project_desc: String,
pub stored_data_desc: String,
pub data_owner_name: String,
pub data_set_sample: String,
}
1 change: 1 addition & 0 deletions fplus-database/src/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ pub mod allocation_amounts;
pub mod allocators;
pub mod applications;
pub mod autoallocations;
pub mod comparable_applications;
15 changes: 15 additions & 0 deletions fplus-lib/src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use alloy::primitives::Address;

use application::file::{SpsChangeRequest, StorageProviderChangeVerifier};
use chrono::{DateTime, Local, Utc};
use fplus_database::database::comparable_applications::create_comparable_application;
use fplus_database::models::comparable_applications::ApplicationComparableData;
use futures::future;
use octocrab::models::{
pulls::PullRequest,
Expand Down Expand Up @@ -870,6 +872,19 @@ impl LDNApplication {
application_id, e
))
})?;
create_comparable_application(
&application_id,
&ApplicationComparableData {
project_desc: application_file.project.history,
stored_data_desc: application_file.project.stored_data_desc,
data_owner_name: application_file.client.name,
data_set_sample: application_file.project.data_sample_link,
},
)
.await
.map_err(|e| {
LDNError::New(format!("Failed to create application in DB: {}", e))
})?;
}

Ok(LDNApplication {
Expand Down
9 changes: 9 additions & 0 deletions manual-migrations/2025-01-31.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE TABLE public.comparable_applications
(
client_address text NOT NULL,
application jsonb NOT NULL,
PRIMARY KEY (client_address)
);

ALTER TABLE IF EXISTS public.comparable_applications
OWNER to postgres;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will break in prod, as in prod we use other user (dbmaster if I remember correctly).
No need to set ownership explicitly, plain create table will be enough and it will be owned by whoever is logged in.