use sea_orm::sea_query::{ColumnDef, Index, IndexCreateStatement};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum KeyType {
Integer,
BigInteger,
Uuid,
String,
}
#[must_use]
pub fn morph_columns(name: &str, key: KeyType) -> [ColumnDef; 2] {
let [mut type_column, mut id_column] = columns(name, key);
[
type_column.not_null().to_owned(),
id_column.not_null().to_owned(),
]
}
#[must_use]
pub fn nullable_morph_columns(name: &str, key: KeyType) -> [ColumnDef; 2] {
let [mut type_column, mut id_column] = columns(name, key);
[type_column.null().to_owned(), id_column.null().to_owned()]
}
#[must_use]
pub fn morph_index(table: &str, name: &str) -> IndexCreateStatement {
let type_column = format!("{name}_type");
let id_column = format!("{name}_id");
Index::create()
.name(format!("{table}_{type_column}_{id_column}_index"))
.table(table.to_string())
.col(type_column)
.col(id_column)
.to_owned()
}
fn columns(name: &str, key: KeyType) -> [ColumnDef; 2] {
let mut type_column = ColumnDef::new(format!("{name}_type"));
type_column.string_len(255);
let mut id_column = ColumnDef::new(format!("{name}_id"));
match key {
KeyType::Integer => id_column.integer(),
KeyType::BigInteger => id_column.big_integer(),
KeyType::Uuid => id_column.uuid(),
KeyType::String => id_column.string_len(255),
};
[type_column, id_column]
}