Skip to content

Commit

Permalink
fix: remove change_reason from config_version
Browse files Browse the repository at this point in the history
  • Loading branch information
pratikmishra356 committed Feb 13, 2025
1 parent d7965d0 commit 4d11865
Show file tree
Hide file tree
Showing 14 changed files with 54 additions and 74 deletions.
13 changes: 2 additions & 11 deletions crates/context_aware_config/src/api/context/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ async fn put_handler(
let version_id = add_config_version(
&state,
tags,
description,
req_change_reason,
transaction_conn,
&schema_name,
Expand Down Expand Up @@ -158,7 +157,6 @@ async fn update_override_handler(
let version_id = add_config_version(
&state,
tags,
description.clone(),
req_change_reason.clone(),
transaction_conn,
&schema_name,
Expand Down Expand Up @@ -219,7 +217,6 @@ async fn move_handler(
let version_id = add_config_version(
&state,
tags,
move_response.description.clone(),
move_response.change_reason.clone(),
transaction_conn,
&schema_name,
Expand Down Expand Up @@ -384,17 +381,15 @@ async fn delete_context_handler(
let tags = parse_config_tags(custom_headers.config_tags)?;
let version_id =
db_conn.transaction::<_, superposition::AppError, _>(|transaction_conn| {
let context = contexts_table
contexts_table
.filter(context_id.eq(ctx_id.clone()))
.schema_name(&schema_name)
.first::<Context>(transaction_conn)?;
operations::delete(ctx_id.clone(), &user, transaction_conn, &schema_name)?;
let description = context.description;
let change_reason = format!("Deleted context by {}", user.username);
let version_id = add_config_version(
&state,
tags,
description,
change_reason,
transaction_conn,
&schema_name,
Expand Down Expand Up @@ -548,14 +543,11 @@ async fn bulk_operations(
}
}

let combined_description = all_descriptions.join(",");

let combined_change_reasons = all_change_reasons.join(",");

let version_id = add_config_version(
&state,
tags,
combined_description,
combined_change_reasons,
transaction_conn,
&schema_name,
Expand Down Expand Up @@ -647,9 +639,8 @@ async fn weight_recompute(
db_error!(err)
})?;
}
let description = "Recomputed weight".to_string();
let change_reason = "Recomputed weight".to_string();
let version_id = add_config_version(&state, tags, description, change_reason, transaction_conn, &schema_name)?;
let version_id = add_config_version(&state, tags, change_reason, transaction_conn, &schema_name)?;
Ok(version_id)
})?;
#[cfg(feature = "high-performance-mode")]
Expand Down
21 changes: 5 additions & 16 deletions crates/context_aware_config/src/api/default_config/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use superposition_types::{
models::cac::{self as models, Context, DefaultConfig},
schema::{self, contexts::dsl::contexts, default_configs::dsl},
},
result as superposition, DBConnection, FunctionNameEnum, PaginatedResponse, User,
result as superposition, DBConnection, PaginatedResponse, User,
};

#[cfg(feature = "high-performance-mode")]
Expand Down Expand Up @@ -134,7 +134,6 @@ async fn create_default_config(
let version_id = add_config_version(
&state,
tags,
description,
change_reason,
transaction_conn,
&schema_name,
Expand Down Expand Up @@ -183,10 +182,6 @@ async fn update_default_config(
}
})?;

let description = req
.description
.clone()
.unwrap_or_else(|| existing.description.clone());
let change_reason = req.change_reason.clone();

let value = req.value.clone().unwrap_or_else(|| existing.value.clone());
Expand Down Expand Up @@ -215,11 +210,10 @@ async fn update_default_config(
));
}

let function_name = match req.function_name.clone() {
Some(FunctionNameEnum::Name(func_name)) => Some(func_name),
Some(FunctionNameEnum::Remove) => None,
None => existing.function_name.clone(),
};
let function_name = req
.function_name
.clone()
.map_or_else(|| existing.function_name.clone(), |f| f.to_option());

if let Err(e) = validate_and_get_function_code(
&mut conn,
Expand Down Expand Up @@ -247,7 +241,6 @@ async fn update_default_config(
let version_id = add_config_version(
&state,
tags.clone(),
description,
change_reason,
transaction_conn,
&schema_name,
Expand Down Expand Up @@ -384,8 +377,6 @@ async fn delete(
let key: String = path.into_inner().into();
let mut version_id = 0;

let default_config_row = fetch_default_key(&key, &mut conn, &schema_name)?;

let context_ids = get_key_usage_context_ids(&key, &mut conn, &schema_name)
.map_err(|_| unexpected_error!("Something went wrong"))?;
if context_ids.is_empty() {
Expand All @@ -400,7 +391,6 @@ async fn delete(
.schema_name(&schema_name)
.execute(transaction_conn)?;

let description = default_config_row.description;
let change_reason = format!("Context Deleted by {}", user.get_email());

let deleted_row =
Expand All @@ -415,7 +405,6 @@ async fn delete(
version_id = add_config_version(
&state,
tags,
description,
change_reason,
transaction_conn,
&schema_name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use diesel::AsChangeset;
use serde::{Deserialize, Deserializer};
use serde_json::{Map, Value};
use superposition_types::{
database::schema::default_configs, FunctionNameEnum, RegexEnum,
api::function::FunctionNameEnum, database::schema::default_configs, RegexEnum,
};

#[derive(Debug, Deserialize)]
Expand Down
4 changes: 1 addition & 3 deletions crates/context_aware_config/src/api/dimension/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,7 @@ async fn update(
.execute(transaction_conn)?
};
}
let update_position =
Position::try_from(new_position).map_err(|err| unexpected_error!(err))?;
update_req.position = Some(update_position);
update_req.position = Some(new_position.clone());
diesel::update(dimensions)
.filter(dsl::dimension.eq(name))
.set((
Expand Down
2 changes: 1 addition & 1 deletion crates/context_aware_config/src/api/dimension/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use diesel::AsChangeset;
use serde::Deserialize;
use serde_json::Value;
use superposition_types::{
database::schema::dimensions, FunctionNameEnum, Position, RegexEnum,
api::function::FunctionNameEnum, database::schema::dimensions, Position, RegexEnum,
};

#[derive(Debug, Deserialize)]
Expand Down
2 changes: 0 additions & 2 deletions crates/context_aware_config/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,6 @@ pub fn add_config_version(
state: &Data<AppState>,
tags: Option<Vec<String>>,
description: String,
change_reason: String,
db_conn: &mut DBConnection,
schema_name: &SchemaName,
) -> superposition::Result<i64> {
Expand All @@ -310,7 +309,6 @@ pub fn add_config_version(
tags,
created_at: Utc::now().naive_utc(),
description,
change_reason,
};
diesel::insert_into(config_versions)
.values(&config_version)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- This file should undo anything in `up.sql`
ALTER TABLE public.config_versions
ADD COLUMN description NOT NULL default '';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- Your SQL goes here
ALTER TABLE public.config_versions
DROP COLUMN description;
1 change: 1 addition & 0 deletions crates/superposition_types/src/api.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod function;
pub mod workspace;
36 changes: 36 additions & 0 deletions crates/superposition_types/src/api/function.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use serde::{Deserialize, Deserializer};
use serde_json::Value;

#[derive(Debug, Clone)]
pub enum FunctionNameEnum {
Name(String),
Remove,
}

impl<'de> Deserialize<'de> for FunctionNameEnum {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let map: Value = Deserialize::deserialize(deserializer)?;
match map {
Value::String(func_name) => Ok(Self::Name(func_name)),
Value::Null => Ok(Self::Remove),
_ => {
log::error!("Expected a string or null literal as the function name.");
Err(serde::de::Error::custom(
"Expected a string or null literal as the function name.",
))
}
}
}
}

impl FunctionNameEnum {
pub fn to_option(&self) -> Option<String> {
match self {
Self::Name(name) => Some(name.to_owned()),
Self::Remove => None,
}
}
}
35 changes: 0 additions & 35 deletions crates/superposition_types/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::collections::{HashMap, HashSet};

use derive_more::{AsRef, Deref, DerefMut, Into};
// use diesel::{deserialize::{self, FromSql}, pg::{Pg, PgValue}, serialize::{self, Output, ToSql}};
#[cfg(feature = "diesel_derives")]
use diesel::{
deserialize::{FromSql, FromSqlRow, Result as DResult},
Expand Down Expand Up @@ -266,40 +265,6 @@ impl ToSql<Integer, Pg> for Position {
}
}

#[derive(Debug, Clone)]
pub enum FunctionNameEnum {
Name(String),
Remove,
}

impl<'de> Deserialize<'de> for FunctionNameEnum {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let map: Value = Deserialize::deserialize(deserializer)?;
match map {
Value::String(func_name) => Ok(Self::Name(func_name)),
Value::Null => Ok(Self::Remove),
_ => {
log::error!("Expected a string or null literal as the function name.");
Err(serde::de::Error::custom(
"Expected a string or null literal as the function name.",
))
}
}
}
}

impl FunctionNameEnum {
pub fn to_option(&self) -> Option<String> {
match self {
Self::Name(name) => Some(name.to_owned()),
Self::Remove => None,
}
}
}

#[cfg(test)]
pub(crate) mod tests {
use std::collections::{HashMap, HashSet};
Expand Down
1 change: 0 additions & 1 deletion crates/superposition_types/src/database/models/cac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ pub struct ConfigVersion {
pub tags: Option<Vec<String>>,
pub created_at: NaiveDateTime,
pub description: String,
pub change_reason: String,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
Expand Down
1 change: 0 additions & 1 deletion crates/superposition_types/src/database/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ diesel::table! {
tags -> Nullable<Array<Varchar>>,
created_at -> Timestamp,
description -> Text,
change_reason -> Text,
}
}

Expand Down
4 changes: 1 addition & 3 deletions crates/superposition_types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ use regex::Regex;
use serde::{Deserialize, Serialize};
use webhook::WebhookConfig;

pub use crate::config::{
Condition, Config, Context, FunctionNameEnum, Overrides, Position,
};
pub use crate::config::{Condition, Config, Context, Overrides, Position};
pub use crate::contextual::Contextual;
pub use crate::overridden::Overridden;

Expand Down

0 comments on commit 4d11865

Please sign in to comment.