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

6838 add prefs repo layer #6885

Merged
merged 9 commits into from
Mar 12, 2025
Merged
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
6 changes: 4 additions & 2 deletions server/repository/src/db_diesel/changelog/changelog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,12 @@ pub enum ChangelogTableName {
Report,
FormSchema,
PluginData,
Preference,
}

pub(crate) enum ChangeLogSyncStyle {
Legacy,
Central,
Legacy, // Everything that goes to Legacy mSupply server
Central, // Data created on Open-mSupply central server
Remote,
File,
RemoteAndCentral, // These records will sync like remote record if store_id exist, otherwise they will sync like central records
Expand Down Expand Up @@ -197,6 +198,7 @@ impl ChangelogTableName {
ChangelogTableName::Report => ChangeLogSyncStyle::Central,
ChangelogTableName::FormSchema => ChangeLogSyncStyle::Central,
ChangelogTableName::PluginData => ChangeLogSyncStyle::RemoteAndCentral,
ChangelogTableName::Preference => ChangeLogSyncStyle::Central,
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions server/repository/src/db_diesel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ mod patient;
pub mod period;
pub mod plugin_data;
mod plugin_data_row;
pub mod preference;
mod preference_row;
pub mod printer;
pub mod printer_row;
pub mod program_enrolment;
Expand Down Expand Up @@ -225,6 +227,8 @@ pub use patient::*;
pub use period::*;
pub use plugin_data::*;
pub use plugin_data_row::*;
pub use preference::*;
pub use preference_row::*;
pub use printer_row::*;
pub use program_enrolment::*;
pub use program_enrolment_row::*;
Expand Down
97 changes: 97 additions & 0 deletions server/repository/src/db_diesel/preference.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
use diesel::{
dsl::{InnerJoin, IntoBoxed},
prelude::*,
};

use crate::diesel_macros::apply_equal_filter;

use super::{
preference_row::{preference, PreferenceRow},
store_row::store,
DBType, EqualFilter, Pagination, RepositoryError, StorageConnection, StoreRow,
};

#[derive(PartialEq, Debug, Clone, Default)]
pub struct Preference {
pub preference_row: PreferenceRow,
}

#[derive(Clone, Default)]
pub struct PreferenceFilter {
pub id: Option<EqualFilter<String>>,
}

impl PreferenceFilter {
pub fn new() -> PreferenceFilter {
PreferenceFilter::default()
}

pub fn id(mut self, filter: EqualFilter<String>) -> Self {
self.id = Some(filter);
self
}
}

pub type PreferenceJoin = (PreferenceRow, StoreRow);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do I need to remove StoreRow here as well?


pub struct PreferenceRepository<'a> {
connection: &'a StorageConnection,
}

impl<'a> PreferenceRepository<'a> {
pub fn new(connection: &'a StorageConnection) -> Self {
PreferenceRepository { connection }
}

pub fn query_one(
&self,
filter: PreferenceFilter,
) -> Result<Option<Preference>, RepositoryError> {
Ok(self.query_by_filter(filter)?.pop())
}

pub fn query_by_filter(
&self,
filter: PreferenceFilter,
) -> Result<Vec<Preference>, RepositoryError> {
self.query(Pagination::new(), Some(filter))
}

pub fn query(
&self,
pagination: Pagination,
filter: Option<PreferenceFilter>,
) -> Result<Vec<Preference>, RepositoryError> {
let query = create_filtered_query(filter);

let final_query = query
.offset(pagination.offset as i64)
.limit(pagination.limit as i64);

// Debug diesel query
//println!(
// "{}",
// diesel::debug_query::<DBType, _>(&final_query).to_string()
//);
let result = final_query
.load::<PreferenceJoin>(self.connection.lock().connection())?
.into_iter()
.map(|(preference_row, _store_row)| Preference { preference_row })
.collect();

Ok(result)
}
}

type BoxedPreferenceQuery = IntoBoxed<'static, InnerJoin<preference::table, store::table>, DBType>;

fn create_filtered_query(filter: Option<PreferenceFilter>) -> BoxedPreferenceQuery {
let mut query = preference::table.inner_join(store::table).into_boxed();

if let Some(f) = filter {
let PreferenceFilter { id } = f;

apply_equal_filter!(query, id, preference::id);
}
query
}
Comment on lines +76 to +97
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do I need to remove StoreRow here as well? I thought we needed storeRow to access store_id?

120 changes: 120 additions & 0 deletions server/repository/src/db_diesel/preference_row.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
use crate::{
ChangeLogInsertRow, ChangelogRepository, ChangelogTableName, RepositoryError, RowActionType,
StorageConnection, Upsert,
};

use super::{preference_row::preference::dsl::*, store_row::store};
use serde::{Deserialize, Serialize};

use diesel::prelude::*;

table! {
preference (id) {
id -> Text,
key -> Text,
value -> Text,
store_id -> Nullable<Text>,
}
}

joinable!(preference -> store (store_id));

allow_tables_to_appear_in_same_query!(preference, store);

#[derive(
Clone, Insertable, Queryable, Debug, PartialEq, AsChangeset, Eq, Serialize, Deserialize, Default,
)]
#[diesel(table_name = preference)]
pub struct PreferenceRow {
pub id: String,
pub key: String,
pub value: String,
pub store_id: Option<String>,
}

pub struct PreferenceRowRepository<'a> {
connection: &'a StorageConnection,
}

impl<'a> PreferenceRowRepository<'a> {
pub fn new(connection: &'a StorageConnection) -> Self {
PreferenceRowRepository { connection }
}

fn upsert_one(&self, preference_row: &PreferenceRow) -> Result<i64, RepositoryError> {
diesel::insert_into(preference::table)
.values(preference_row)
.on_conflict(id)
.do_update()
.set(preference_row)
.execute(self.connection.lock().connection())?;

self.insert_changelog(preference_row.to_owned(), RowActionType::Upsert)
}

fn insert_changelog(
&self,
row: PreferenceRow,
action: RowActionType,
) -> Result<i64, RepositoryError> {
let row = ChangeLogInsertRow {
table_name: ChangelogTableName::Preference,
record_id: row.id,
row_action: action,
store_id: row.store_id.clone(),
name_link_id: None,
};
ChangelogRepository::new(self.connection).insert(&row)
}

pub fn find_one_by_key(
&self,
preference_key: &str,
) -> Result<Option<PreferenceRow>, RepositoryError> {
let result = preference
.filter(key.eq(preference_key))
.first(self.connection.lock().connection())
.optional()?;
Ok(result)
}

pub fn find_one_by_id(
&self,
preference_id: &str,
) -> Result<Option<PreferenceRow>, RepositoryError> {
let result = preference::table
.filter(preference::id.eq(preference_id))
.first(self.connection.lock().connection())
.optional()?;
Ok(result)
}

pub fn delete(&self, preference_id: &str) -> Result<Option<i64>, RepositoryError> {
let old_row = self.find_one_by_id(preference_id)?;
Copy link
Contributor

Choose a reason for hiding this comment

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

Hey look it came in use! I knew it'd have something to do with sync πŸ˜†

let change_log_id = match old_row {
Some(old_row) => self.insert_changelog(old_row, RowActionType::Delete)?,
None => {
return Ok(None);
}
};

diesel::delete(preference.filter(preference::id.eq(preference_id)))
.execute(self.connection.lock().connection())?;
Ok(Some(change_log_id))
}
}

impl Upsert for PreferenceRow {
fn upsert(&self, con: &StorageConnection) -> Result<Option<i64>, RepositoryError> {
let cursor_id = PreferenceRowRepository::new(con).upsert_one(self)?;
Ok(Some(cursor_id))
}

// Test only
fn assert_upserted(&self, con: &StorageConnection) {
assert_eq!(
PreferenceRowRepository::new(con).find_one_by_key(&self.key),
Ok(Some(self.clone()))
)
}
}
Loading