(
db: &DbConn,
creates: &[TypeCreateStatement],
entity: E,
)
| 78 | } |
| 79 | |
| 80 | pub async fn create_enum<E>( |
| 81 | db: &DbConn, |
| 82 | creates: &[TypeCreateStatement], |
| 83 | entity: E, |
| 84 | ) -> Result<(), DbErr> |
| 85 | where |
| 86 | E: EntityTrait, |
| 87 | { |
| 88 | let builder = db.get_database_backend(); |
| 89 | if builder == DbBackend::Postgres { |
| 90 | for col in E::Column::iter() { |
| 91 | let col_def = col.def(); |
| 92 | let col_type = col_def.get_column_type(); |
| 93 | if !matches!(col_type, ColumnType::Enum { .. }) { |
| 94 | continue; |
| 95 | } |
| 96 | let name = match col_type { |
| 97 | ColumnType::Enum { name, .. } => name, |
| 98 | _ => unreachable!(), |
| 99 | }; |
| 100 | let drop_type_stmt = Type::drop() |
| 101 | .name(SeaRc::clone(name)) |
| 102 | .if_exists() |
| 103 | .cascade() |
| 104 | .to_owned(); |
| 105 | let stmt = builder.build(&drop_type_stmt); |
| 106 | db.execute(stmt).await?; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | let expect_stmts: Vec<Statement> = creates.iter().map(|stmt| builder.build(stmt)).collect(); |
| 111 | let schema = Schema::new(builder); |
| 112 | let create_from_entity_stmts: Vec<Statement> = schema |
| 113 | .create_enum_from_entity(entity) |
| 114 | .iter() |
| 115 | .map(|stmt| builder.build(stmt)) |
| 116 | .collect(); |
| 117 | |
| 118 | assert_eq!(expect_stmts, create_from_entity_stmts); |
| 119 | |
| 120 | for stmt in expect_stmts { |
| 121 | db.execute(stmt).await.map(|_| ())?; |
| 122 | } |
| 123 | |
| 124 | Ok(()) |
| 125 | } |
| 126 | |
| 127 | pub async fn create_table<E>( |
| 128 | db: &DbConn, |
no test coverage detected