Skip to content

Commit

Permalink
feat: added description and comment columns in all existing tables an…
Browse files Browse the repository at this point in the history
…d apis
  • Loading branch information
sauraww committed Nov 14, 2024
1 parent c155bb0 commit a37e9b2
Show file tree
Hide file tree
Showing 17 changed files with 180 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-- This file should undo anything in `up.sql`
-- contexts table
ALTER TABLE public.contexts DROP COLUMN IF EXISTS description;
ALTER TABLE public.contexts DROP COLUMN IF EXISTS comment;

-- dimensions table
ALTER TABLE public.dimensions DROP COLUMN IF EXISTS description;
ALTER TABLE public.dimensions DROP COLUMN IF EXISTS comment;

-- default_configs table
ALTER TABLE public.default_configs DROP COLUMN IF EXISTS description;
ALTER TABLE public.default_configs DROP COLUMN IF EXISTS comment;

-- type_templates table
ALTER TABLE public.type_templates DROP COLUMN IF EXISTS description;
ALTER TABLE public.type_templates DROP COLUMN IF EXISTS comment;

-- Revert column rename from description back to function_description
ALTER TABLE public.functions RENAME COLUMN description TO function_description;
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-- contexts table
ALTER TABLE public.contexts ADD COLUMN description TEXT;
ALTER TABLE public.contexts ADD COLUMN comment TEXT;

-- dimensions table
ALTER TABLE public.dimensions ADD COLUMN description TEXT;
ALTER TABLE public.dimensions ADD COLUMN comment TEXT;

-- default_configs table
ALTER TABLE public.default_configs ADD COLUMN description TEXT;
ALTER TABLE public.default_configs ADD COLUMN comment TEXT;

-- type_templates table
ALTER TABLE public.type_templates ADD COLUMN description TEXT;
ALTER TABLE public.type_templates ADD COLUMN comment TEXT;

-- functions table
-- Rename function_description to description
ALTER TABLE public.functions RENAME COLUMN function_description TO description;

12 changes: 12 additions & 0 deletions crates/context_aware_config/src/api/config/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,9 +378,21 @@ fn construct_new_payload(
},
)?;

let description = res
.get("description")
.and_then(|val| val.as_str())
.map(|s| s.to_string());

let comment = res
.get("comment")
.and_then(|val| val.as_str())
.map(|s| s.to_string());

return Ok(web::Json(PutReq {
context: context,
r#override: override_,
description,
comment,
}));
}

Expand Down
8 changes: 8 additions & 0 deletions crates/context_aware_config/src/api/context/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ fn create_ctx_from_put_req(
tenant_config: &TenantConfig,
) -> superposition::Result<Context> {
let ctx_condition = req.context.to_owned().into_inner();
let description = req.description.to_owned();
let comment = req.comment.clone();
let condition_val = Value::Object(ctx_condition.clone().into());
let r_override = req.r#override.clone().into_inner();
let ctx_override = Value::Object(r_override.clone().into());
Expand Down Expand Up @@ -242,6 +244,8 @@ fn create_ctx_from_put_req(
created_by: user.get_email(),
last_modified_at: Utc::now().naive_utc(),
last_modified_by: user.get_email(),
description,
comment,
})
}

Expand Down Expand Up @@ -445,6 +449,8 @@ fn r#move(
) -> superposition::Result<PutResp> {
use contexts::dsl;
let req = req.into_inner();
let description = req.description.clone();
let comment = req.comment.clone();
let ctx_condition = req.context.to_owned().into_inner();
let ctx_condition_value = Value::Object(ctx_condition.clone().into());
let new_ctx_id = hash(&ctx_condition_value);
Expand Down Expand Up @@ -488,6 +494,8 @@ fn r#move(
override_: ctx.override_,
last_modified_at: Utc::now().naive_utc(),
last_modified_by: user.get_email(),
description,
comment,
};

let handle_unique_violation =
Expand Down
6 changes: 6 additions & 0 deletions crates/context_aware_config/src/api/context/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ use superposition_types::{Cac, Condition, Overrides};
pub struct PutReq {
pub context: Cac<Condition>,
pub r#override: Cac<Overrides>,
pub description: Option<String>,
pub comment: Option<String>,
}

#[cfg_attr(test, derive(Debug, PartialEq))] // Derive traits only when running tests
#[derive(Deserialize, Clone)]
pub struct MoveReq {
pub context: Cac<Condition>,
pub description: Option<String>,
pub comment: Option<String>,
}

#[derive(Deserialize, Clone)]
Expand Down Expand Up @@ -120,6 +124,8 @@ mod tests {
let expected_action = ContextAction::Put(PutReq {
context: context,
r#override: override_,
description: None,
comment: None,
});

let action_deserialized =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ async fn create(
))
}
};
let description = req.description.clone();
let comment = req.comment.clone();

let result = fetch_default_key(&key, &mut conn);

Expand Down Expand Up @@ -129,6 +131,8 @@ async fn create(
created_at: created_at_val,
last_modified_at: Utc::now().naive_utc(),
last_modified_by: user.get_email(),
description,
comment,
};

let schema_compile_result = JSONSchema::options()
Expand Down
2 changes: 2 additions & 0 deletions crates/context_aware_config/src/api/default_config/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ pub struct CreateReq {
pub schema: Option<Map<String, Value>>,
#[serde(default, deserialize_with = "deserialize_option")]
pub function_name: Option<Value>,
pub description: Option<String>,
pub comment: Option<String>,
}

#[derive(Debug, Deserialize, AsRef, Deref, DerefMut, Into)]
Expand Down
32 changes: 18 additions & 14 deletions crates/context_aware_config/src/api/dimension/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use crate::{
api::dimension::{types::CreateReq, utils::get_dimension_usage_context_ids},
db::{
models::Dimension,
schema::{dimensions, dimensions::dsl::*},
schema::dimensions::dsl::{
created_at, dimension, dimensions as dimension_table, last_modified_at,
last_modified_by,
},
},
helpers::validate_jsonschema,
};
Expand Down Expand Up @@ -81,11 +84,13 @@ async fn create(
function_name: fun_name.clone(),
last_modified_at: Utc::now().naive_utc(),
last_modified_by: user.get_email(),
comment: create_req.comment.into(),
description: create_req.description.into(),
};

let upsert = diesel::insert_into(dimensions)
let upsert = diesel::insert_into(dimension_table)
.values(&new_dimension)
.on_conflict(dimensions::dimension)
.on_conflict(dimension)
.do_update()
.set(&new_dimension)
.get_result::<Dimension>(&mut conn);
Expand Down Expand Up @@ -129,13 +134,13 @@ async fn get(

let (total_pages, total_items, result) = match filters.all {
Some(true) => {
let result: Vec<Dimension> = dimensions.get_results(&mut conn)?;
let result: Vec<Dimension> = dimension_table.get_results(&mut conn)?;
(1, result.len() as i64, result)
}
_ => {
let n_dimensions: i64 = dimensions.count().get_result(&mut conn)?;
let n_dimensions: i64 = dimension_table.count().get_result(&mut conn)?;
let limit = filters.count.unwrap_or(10);
let mut builder = dimensions
let mut builder = dimension_table
.into_boxed()
.order(created_at.desc())
.limit(limit);
Expand Down Expand Up @@ -173,23 +178,22 @@ async fn delete_dimension(
) -> superposition::Result<HttpResponse> {
let name: String = path.into_inner().into();
let DbConnection(mut conn) = db_conn;
dimensions::dsl::dimensions
.filter(dimensions::dimension.eq(&name))
dimension_table
.filter(dimension.eq(&name))
.select(Dimension::as_select())
.get_result(&mut conn)?;
let context_ids = get_dimension_usage_context_ids(&name, &mut conn)
.map_err(|_| unexpected_error!("Something went wrong"))?;
if context_ids.is_empty() {
conn.transaction::<_, superposition::AppError, _>(|transaction_conn| {
use dimensions::dsl;
diesel::update(dsl::dimensions)
.filter(dsl::dimension.eq(&name))
diesel::update(dimension_table)
.filter(dimension.eq(&name))
.set((
dsl::last_modified_at.eq(Utc::now().naive_utc()),
dsl::last_modified_by.eq(user.get_email()),
last_modified_at.eq(Utc::now().naive_utc()),
last_modified_by.eq(user.get_email()),
))
.execute(transaction_conn)?;
let deleted_row = delete(dsl::dimensions.filter(dsl::dimension.eq(&name)))
let deleted_row = delete(dimension_table.filter(dimension.eq(&name)))
.execute(transaction_conn);
match deleted_row {
Ok(0) => Err(not_found!("Dimension `{}` doesn't exists", name)),
Expand Down
6 changes: 6 additions & 0 deletions crates/context_aware_config/src/api/dimension/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ pub struct CreateReq {
pub schema: Value,
#[serde(default, deserialize_with = "deserialize_option")]
pub function_name: Option<Value>,
pub description: Option<String>,
pub comment: Option<String>,
}

#[derive(Debug, Deserialize, AsRef, Deref, DerefMut, Into)]
Expand Down Expand Up @@ -72,6 +74,8 @@ pub struct DimensionWithMandatory {
pub function_name: Option<String>,
pub last_modified_at: NaiveDateTime,
pub last_modified_by: String,
pub description: Option<String>,
pub comment: Option<String>,
pub mandatory: bool,
}

Expand All @@ -86,6 +90,8 @@ impl DimensionWithMandatory {
function_name: value.function_name,
last_modified_at: value.last_modified_at,
last_modified_by: value.last_modified_by,
description: value.description,
comment: value.comment,
mandatory,
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/context_aware_config/src/api/functions/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ async fn create(
published_at: None,
published_by: None,
published_runtime_version: None,
function_description: req.description,
description: req.description,
last_modified_at: Utc::now().naive_utc(),
last_modified_by: user.get_email(),
};
Expand Down Expand Up @@ -136,7 +136,7 @@ async fn update(
draft_runtime_version: req
.runtime_version
.unwrap_or(result.draft_runtime_version),
function_description: req.description.unwrap_or(result.function_description),
description: req.description.unwrap_or(result.description),
draft_edited_by: user.get_email(),
draft_edited_at: Utc::now().naive_utc(),
published_code: result.published_code,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,16 @@ async fn create_type(
)
})?;
let type_name: String = request.type_name.clone().into();
let description = request.description.clone();
let comment = request.comment.clone();
let type_template = diesel::insert_into(type_templates::table)
.values((
type_templates::type_schema.eq(request.type_schema.clone()),
type_templates::type_name.eq(type_name),
type_templates::created_by.eq(user.email.clone()),
type_templates::last_modified_by.eq(user.email.clone()),
type_templates::description.eq(description),
type_templates::comment.eq(comment),
))
.get_result::<TypeTemplates>(&mut conn)
.map_err(|err| {
Expand Down
4 changes: 4 additions & 0 deletions crates/context_aware_config/src/api/type_templates/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use superposition_types::RegexEnum;
pub struct TypeTemplateRequest {
pub type_schema: Value,
pub type_name: TypeTemplateName,
pub description: Option<String>,
pub comment: Option<String>,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
Expand All @@ -16,6 +18,8 @@ pub struct TypeTemplateResponse {
pub created_at: String,
pub last_modified: String,
pub created_by: String,
pub description: Option<String>,
pub comment: Option<String>,
}

#[derive(Debug, Deserialize, Serialize, AsRef, Deref, DerefMut, Into, Clone)]
Expand Down
10 changes: 9 additions & 1 deletion crates/context_aware_config/src/db/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ pub struct Context {
pub override_: Overrides,
pub last_modified_at: NaiveDateTime,
pub last_modified_by: String,
pub description: Option<String>,
pub comment: Option<String>,
}

impl Contextual for Context {
Expand Down Expand Up @@ -49,6 +51,8 @@ pub struct Dimension {
pub function_name: Option<String>,
pub last_modified_at: NaiveDateTime,
pub last_modified_by: String,
pub description: Option<String>,
pub comment: Option<String>,
}

#[derive(Queryable, Selectable, Insertable, AsChangeset, Serialize, Clone)]
Expand All @@ -64,6 +68,8 @@ pub struct DefaultConfig {
pub function_name: Option<String>,
pub last_modified_at: NaiveDateTime,
pub last_modified_by: String,
pub description: Option<String>,
pub comment: Option<String>,
}

#[derive(Queryable, Selectable, Insertable, AsChangeset, Serialize, Clone, Debug)]
Expand All @@ -73,7 +79,7 @@ pub struct Function {
pub function_name: String,
pub published_code: Option<String>,
pub draft_code: String,
pub function_description: String,
pub description: String,
pub published_runtime_version: Option<String>,
pub draft_runtime_version: String,
pub published_at: Option<NaiveDateTime>,
Expand Down Expand Up @@ -121,4 +127,6 @@ pub struct TypeTemplates {
pub created_at: NaiveDateTime,
pub last_modified_at: NaiveDateTime,
pub last_modified_by: String,
pub description: Option<String>,
pub comment: Option<String>,
}
Loading

0 comments on commit a37e9b2

Please sign in to comment.