use crate::types::{DynIden, IntoIden};
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct MaterializedViewDef {
pub(crate) name: DynIden,
pub(crate) if_not_exists: bool,
pub(crate) columns: Vec<DynIden>,
pub(crate) tablespace: Option<DynIden>,
pub(crate) with_data: Option<bool>,
}
impl MaterializedViewDef {
pub fn new<N: IntoIden>(name: N) -> Self {
Self {
name: name.into_iden(),
if_not_exists: false,
columns: Vec::new(),
tablespace: None,
with_data: None,
}
}
pub fn if_not_exists(mut self, if_not_exists: bool) -> Self {
self.if_not_exists = if_not_exists;
self
}
pub fn columns<I, C>(mut self, cols: I) -> Self
where
I: IntoIterator<Item = C>,
C: IntoIden,
{
for col in cols {
self.columns.push(col.into_iden());
}
self
}
pub fn tablespace<T: IntoIden>(mut self, tablespace: T) -> Self {
self.tablespace = Some(tablespace.into_iden());
self
}
pub fn with_data(mut self, with_data: bool) -> Self {
self.with_data = Some(with_data);
self
}
}
#[derive(Debug, Clone)]
pub enum MaterializedViewOperation {
Rename(DynIden),
OwnerTo(DynIden),
SetSchema(DynIden),
}
#[cfg(test)]
mod tests {
use super::*;
use rstest::*;
#[rstest]
fn test_materialized_view_def_basic() {
let mv = MaterializedViewDef::new("my_mv");
assert_eq!(mv.name.to_string(), "my_mv");
assert!(!mv.if_not_exists);
assert!(mv.columns.is_empty());
assert!(mv.tablespace.is_none());
assert!(mv.with_data.is_none());
}
#[rstest]
fn test_materialized_view_def_if_not_exists() {
let mv = MaterializedViewDef::new("my_mv").if_not_exists(true);
assert_eq!(mv.name.to_string(), "my_mv");
assert!(mv.if_not_exists);
}
#[rstest]
fn test_materialized_view_def_columns() {
let mv = MaterializedViewDef::new("my_mv").columns(vec!["id", "name", "email"]);
assert_eq!(mv.columns.len(), 3);
assert_eq!(mv.columns[0].to_string(), "id");
assert_eq!(mv.columns[1].to_string(), "name");
assert_eq!(mv.columns[2].to_string(), "email");
}
#[rstest]
fn test_materialized_view_def_tablespace() {
let mv = MaterializedViewDef::new("my_mv").tablespace("pg_default");
assert_eq!(mv.tablespace.as_ref().unwrap().to_string(), "pg_default");
}
#[rstest]
fn test_materialized_view_def_with_data() {
let mv = MaterializedViewDef::new("my_mv").with_data(true);
assert_eq!(mv.with_data, Some(true));
}
#[rstest]
fn test_materialized_view_def_with_no_data() {
let mv = MaterializedViewDef::new("my_mv").with_data(false);
assert_eq!(mv.with_data, Some(false));
}
#[rstest]
fn test_materialized_view_def_all_options() {
let mv = MaterializedViewDef::new("my_mv")
.if_not_exists(true)
.columns(vec!["id", "name"])
.tablespace("pg_default")
.with_data(true);
assert_eq!(mv.name.to_string(), "my_mv");
assert!(mv.if_not_exists);
assert_eq!(mv.columns.len(), 2);
assert_eq!(mv.tablespace.as_ref().unwrap().to_string(), "pg_default");
assert_eq!(mv.with_data, Some(true));
}
#[rstest]
fn test_materialized_view_operation_rename() {
let op = MaterializedViewOperation::Rename("new_mv".into_iden());
assert!(matches!(op, MaterializedViewOperation::Rename(_)));
}
#[rstest]
fn test_materialized_view_operation_owner_to() {
let op = MaterializedViewOperation::OwnerTo("new_owner".into_iden());
assert!(matches!(op, MaterializedViewOperation::OwnerTo(_)));
}
#[rstest]
fn test_materialized_view_operation_set_schema() {
let op = MaterializedViewOperation::SetSchema("new_schema".into_iden());
assert!(matches!(op, MaterializedViewOperation::SetSchema(_)));
}
}