Skip to main content

durable_db/entity/
sea_orm_active_enums.rs

1//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.19
2
3use sea_orm::entity::prelude::*;
4use serde::{Deserialize, Serialize};
5
6#[derive(
7    Debug,
8    Clone,
9    PartialEq,
10    Eq,
11    EnumIter,
12    DeriveActiveEnum,
13    Serialize,
14    Deserialize,
15    strum :: Display,
16    strum :: EnumString,
17)]
18// HACK: The embedded quotes make sea-query's Iden::quoted() produce
19// "durable"."task_status" — a valid schema-qualified PostgreSQL identifier.
20// Without this, SeaORM generates CAST($1 AS "task_status") which fails when
21// the `durable` schema is not in the connection's search_path.
22// Verified against sea-orm 1.x / sea-query 0.32. If a future sea-orm release
23// sanitises Iden strings, this will break — see the assertion test below.
24#[sea_orm(rs_type = "String", db_type = "Enum", enum_name = "durable\".\"task_status")]
25#[strum(serialize_all = "SCREAMING_SNAKE_CASE")]
26pub enum TaskStatus {
27    #[sea_orm(string_value = "PENDING")]
28    Pending,
29    #[sea_orm(string_value = "RUNNING")]
30    Running,
31    #[sea_orm(string_value = "COMPLETED")]
32    Completed,
33    #[sea_orm(string_value = "FAILED")]
34    Failed,
35    #[sea_orm(string_value = "PAUSED")]
36    Paused,
37    #[sea_orm(string_value = "CANCELLED")]
38    Cancelled,
39}
40
41#[cfg(test)]
42mod tests {
43    use super::*;
44    use sea_orm::ActiveEnum;
45
46    /// Guard against sea-orm/sea-query changes that would break the
47    /// schema-qualified enum_name hack above.
48    #[test]
49    fn task_status_iden_is_schema_qualified() {
50        let iden = TaskStatus::name();
51        let quoted = iden.to_string();
52        assert_eq!(
53            quoted,
54            r#"durable"."task_status"#,
55            "TaskStatus Iden must render as the unquoted schema-qualified name; \
56             sea-query wraps it to produce \"durable\".\"task_status\" in SQL"
57        );
58    }
59
60    /// Verify the actual SQL generated by SeaORM insert/update/select contains
61    /// the schema-qualified enum cast, not bare `task_status`.
62    #[test]
63    fn task_status_sql_is_schema_qualified() {
64        use sea_orm::{DbBackend, EntityTrait, Set, QueryTrait, ColumnTrait, QueryFilter, IntoActiveModel};
65        use crate::entity::task;
66
67        // INSERT
68        let model = task::ActiveModel {
69            id: Set(uuid::Uuid::nil()),
70            name: Set("test".into()),
71            kind: Set("WORKFLOW".into()),
72            status: Set(TaskStatus::Pending),
73            ..Default::default()
74        };
75        let sql = task::Entity::insert(model).build(DbBackend::Postgres).to_string();
76        assert!(
77            !sql.contains(r#"AS "task_status""#),
78            "INSERT must not contain bare CAST AS \"task_status\": {sql}"
79        );
80
81        // SELECT with filter
82        let sql = task::Entity::find()
83            .filter(task::Column::Status.eq(TaskStatus::Running))
84            .build(DbBackend::Postgres)
85            .to_string();
86        assert!(
87            !sql.contains(r#"AS "task_status""#),
88            "SELECT must not contain bare CAST AS \"task_status\": {sql}"
89        );
90
91        // UPDATE
92        let model = task::Model {
93            id: uuid::Uuid::nil(),
94            parent_id: None,
95            sequence: None,
96            name: "test".into(),
97            status: TaskStatus::Running,
98            kind: "WORKFLOW".into(),
99            input: None,
100            output: None,
101            error: None,
102            max_retries: 3,
103            retry_count: 0,
104            cron: None,
105            next_run_at: None,
106            queue_name: None,
107            handler: None,
108            executor_id: None,
109            app_version: None,
110            timeout_ms: None,
111            deadline_epoch_ms: None,
112            recovery_count: 0,
113            max_recovery_attempts: 3,
114            created_at: sea_orm::prelude::DateTimeWithTimeZone::default(),
115            started_at: None,
116            completed_at: None,
117        };
118        let mut active = model.into_active_model();
119        active.status = Set(TaskStatus::Completed);
120        let sql = task::Entity::update(active).build(DbBackend::Postgres).to_string();
121        assert!(
122            !sql.contains(r#"AS "task_status""#),
123            "UPDATE must not contain bare CAST AS \"task_status\": {sql}"
124        );
125    }
126}