Skip to main content

stormchaser_api/db/
cron.rs

1use serde_json::Value;
2use sqlx::PgPool;
3use stormchaser_model::CronWorkflowId;
4
5use stormchaser_model::cron;
6
7/// Creates a new cron workflow configuration.
8#[allow(clippy::too_many_arguments)]
9pub async fn create_cron_workflow(
10    pool: &PgPool,
11    id: CronWorkflowId,
12    name: &str,
13    description: &Option<String>,
14    cronspec: &str,
15    workflow_name: &str,
16    repo_url: &str,
17    workflow_path: &str,
18    git_ref: &str,
19    inputs: &Value,
20    secret_token: &str,
21    external_job_id: &Option<String>,
22) -> Result<(), sqlx::Error> {
23    sqlx::query(
24        r#"
25        INSERT INTO cron_workflows (id, name, description, cronspec, workflow_name, repo_url, workflow_path, git_ref, inputs, secret_token, external_job_id)
26        VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
27        "#,
28    )
29    .bind(id)
30    .bind(name)
31    .bind(description)
32    .bind(cronspec)
33    .bind(workflow_name)
34    .bind(repo_url)
35    .bind(workflow_path)
36    .bind(git_ref)
37    .bind(inputs)
38    .bind(secret_token)
39    .bind(external_job_id)
40    .execute(pool)
41    .await?;
42    Ok(())
43}
44
45/// Retrieves all cron workflows.
46/// List cron workflows.
47pub async fn list_cron_workflows(pool: &PgPool) -> Result<Vec<cron::CronWorkflow>, sqlx::Error> {
48    sqlx::query_as("SELECT * FROM cron_workflows ORDER BY created_at DESC")
49        .fetch_all(pool)
50        .await
51}
52
53/// Retrieves a cron workflow by ID.
54/// Get cron workflow.
55pub async fn get_cron_workflow(
56    pool: &PgPool,
57    id: CronWorkflowId,
58) -> Result<Option<cron::CronWorkflow>, sqlx::Error> {
59    sqlx::query_as("SELECT * FROM cron_workflows WHERE id = $1")
60        .bind(id)
61        .fetch_optional(pool)
62        .await
63}
64
65/// Retrieves an active cron workflow by ID.
66/// Get active cron workflow.
67pub async fn get_active_cron_workflow(
68    pool: &PgPool,
69    id: CronWorkflowId,
70) -> Result<Option<cron::CronWorkflow>, sqlx::Error> {
71    sqlx::query_as("SELECT * FROM cron_workflows WHERE id = $1 AND is_active = TRUE")
72        .bind(id)
73        .fetch_optional(pool)
74        .await
75}
76
77/// Deletes a scheduled workflow configuration
78pub async fn delete_cron_workflow(pool: &PgPool, id: CronWorkflowId) -> Result<(), sqlx::Error> {
79    sqlx::query("DELETE FROM cron_workflows WHERE id = $1")
80        .bind(id)
81        .execute(pool)
82        .await?;
83    Ok(())
84}
85
86/// Inserts a new cron workflow.
87pub async fn insert_cron_workflow(
88    pool: &PgPool,
89    id: CronWorkflowId,
90    payload: &crate::routes::CreateCronWorkflowRequest,
91    secret_token: &str,
92    external_job_id: Option<String>,
93) -> Result<(), sqlx::Error> {
94    sqlx::query(
95        r#"
96        INSERT INTO cron_workflows (id, name, description, cronspec, workflow_name, repo_url, workflow_path, git_ref, inputs, secret_token, external_job_id)
97        VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
98        "#,
99    )
100    .bind(id)
101    .bind(&payload.name)
102    .bind(&payload.description)
103    .bind(&payload.cronspec)
104    .bind(&payload.workflow_name)
105    .bind(&payload.repo_url)
106    .bind(&payload.workflow_path)
107    .bind(&payload.git_ref)
108    .bind(&payload.inputs)
109    .bind(secret_token)
110    .bind(external_job_id)
111    .execute(pool)
112    .await?;
113    Ok(())
114}