| 188 | } |
| 189 | |
| 190 | fn column_def_from_entity_column<E>(column: E::Column, backend: DbBackend) -> ColumnDef |
| 191 | where |
| 192 | E: EntityTrait, |
| 193 | { |
| 194 | let orm_column_def = column.def(); |
| 195 | let types = match &orm_column_def.col_type { |
| 196 | ColumnType::Enum { name, variants } => match backend { |
| 197 | DbBackend::MySql => { |
| 198 | let variants: Vec<String> = variants.iter().map(|v| v.to_string()).collect(); |
| 199 | ColumnType::custom(format!("ENUM('{}')", variants.join("', '")).as_str()) |
| 200 | } |
| 201 | DbBackend::Postgres => ColumnType::Custom(SeaRc::clone(name)), |
| 202 | DbBackend::Sqlite => orm_column_def.col_type, |
| 203 | }, |
| 204 | _ => orm_column_def.col_type, |
| 205 | }; |
| 206 | let mut column_def = ColumnDef::new_with_type(column, types); |
| 207 | if !orm_column_def.null { |
| 208 | column_def.not_null(); |
| 209 | } |
| 210 | if orm_column_def.unique { |
| 211 | column_def.unique_key(); |
| 212 | } |
| 213 | if let Some(default) = orm_column_def.default { |
| 214 | column_def.default(default); |
| 215 | } |
| 216 | if let Some(comment) = orm_column_def.comment { |
| 217 | column_def.comment(comment); |
| 218 | } |
| 219 | for primary_key in E::PrimaryKey::iter() { |
| 220 | if column.to_string() == primary_key.into_column().to_string() { |
| 221 | if E::PrimaryKey::auto_increment() { |
| 222 | column_def.auto_increment(); |
| 223 | } |
| 224 | if <<E::PrimaryKey as PrimaryKeyTrait>::ValueType as PrimaryKeyArity>::ARITY == 1 { |
| 225 | column_def.primary_key(); |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | column_def |
| 230 | } |
| 231 | |
| 232 | #[cfg(test)] |
| 233 | mod tests { |