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(
25    rs_type = "String",
26    db_type = "Enum",
27    enum_name = "durable\".\"task_status"
28)]
29#[strum(serialize_all = "SCREAMING_SNAKE_CASE")]
30pub enum TaskStatus {
31    #[sea_orm(string_value = "PENDING")]
32    Pending,
33    #[sea_orm(string_value = "RUNNING")]
34    Running,
35    #[sea_orm(string_value = "COMPLETED")]
36    Completed,
37    #[sea_orm(string_value = "FAILED")]
38    Failed,
39    #[sea_orm(string_value = "PAUSED")]
40    Paused,
41    #[sea_orm(string_value = "CANCELLED")]
42    Cancelled,
43}
44
45#[cfg(test)]
46mod tests {
47    use super::*;
48    use sea_orm::ActiveEnum;
49
50    /// Guard against sea-orm/sea-query changes that would break the
51    /// schema-qualified enum_name hack above.
52    #[test]
53    fn task_status_iden_is_schema_qualified() {
54        let iden = TaskStatus::name();
55        let quoted = iden.to_string();
56        assert_eq!(
57            quoted, r#"durable"."task_status"#,
58            "TaskStatus Iden must render as the unquoted schema-qualified name; \
59             sea-query wraps it to produce \"durable\".\"task_status\" in SQL"
60        );
61    }
62
63    /// Verify the actual SQL generated by SeaORM insert/update/select contains
64    /// the schema-qualified enum cast, not bare `task_status`.
65    #[test]
66    fn task_status_sql_is_schema_qualified() {
67        use crate::entity::task;
68        use sea_orm::{
69            ColumnTrait, DbBackend, EntityTrait, IntoActiveModel, QueryFilter, QueryTrait, Set,
70        };
71
72        // INSERT
73        let model = task::ActiveModel {
74            id: Set(uuid::Uuid::nil()),
75            name: Set("test".into()),
76            kind: Set("WORKFLOW".into()),
77            status: Set(TaskStatus::Pending),
78            ..Default::default()
79        };
80        let sql = task::Entity::insert(model)
81            .build(DbBackend::Postgres)
82            .to_string();
83        assert!(
84            !sql.contains(r#"AS "task_status""#),
85            "INSERT must not contain bare CAST AS \"task_status\": {sql}"
86        );
87
88        // SELECT with filter
89        let sql = task::Entity::find()
90            .filter(task::Column::Status.eq(TaskStatus::Running))
91            .build(DbBackend::Postgres)
92            .to_string();
93        assert!(
94            !sql.contains(r#"AS "task_status""#),
95            "SELECT must not contain bare CAST AS \"task_status\": {sql}"
96        );
97
98        // UPDATE
99        let model = task::Model {
100            id: uuid::Uuid::nil(),
101            parent_id: None,
102            sequence: None,
103            name: "test".into(),
104            status: TaskStatus::Running,
105            kind: "WORKFLOW".into(),
106            input: None,
107            output: None,
108            error: None,
109            max_retries: 3,
110            retry_count: 0,
111            cron: None,
112            next_run_at: None,
113            queue_name: None,
114            handler: None,
115            executor_id: None,
116            app_version: None,
117            timeout_ms: None,
118            deadline_epoch_ms: None,
119            recovery_count: 0,
120            max_recovery_attempts: 3,
121            created_at: sea_orm::prelude::DateTimeWithTimeZone::default(),
122            started_at: None,
123            completed_at: None,
124        };
125        let mut active = model.into_active_model();
126        active.status = Set(TaskStatus::Completed);
127        let sql = task::Entity::update(active)
128            .build(DbBackend::Postgres)
129            .to_string();
130        assert!(
131            !sql.contains(r#"AS "task_status""#),
132            "UPDATE must not contain bare CAST AS \"task_status\": {sql}"
133        );
134    }
135}