Skip to content

Commit

Permalink
fix default for JSON columns
Browse files Browse the repository at this point in the history
  • Loading branch information
m1guelpf committed Sep 1, 2023
1 parent 35903e9 commit f0be3b6
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions ensemble/src/migrations/schema/column.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use ensemble_derive::Column;
use itertools::Itertools;
use rbs::Value;
use std::{fmt::Display, sync::mpsc};

use super::Schemable;

#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Type {
Json,
Uuid,
Expand Down Expand Up @@ -92,7 +93,20 @@ pub struct Column {
impl Column {
/// Specify a "default" value for the column
pub fn default<T: serde::Serialize>(mut self, default: T) -> Self {
self.default = Some(rbs::to_value!(default));
let value = if self.r#type == Type::Json {
Value::String(serde_json::to_string(&default).unwrap())
} else {
rbs::to_value!(default)
};

if let Type::Enum(values) = &self.r#type {
assert!(
values.contains(&value.as_str().unwrap_or_default().to_string()),
"default value must be one of the enum values"
);
}

self.default = Some(value);

self
}
Expand Down

0 comments on commit f0be3b6

Please sign in to comment.