From c06407a138c8dd1732a247ff63863b38b0835daa Mon Sep 17 00:00:00 2001 From: bodymindarts Date: Tue, 11 Jun 2024 09:26:34 +0200 Subject: [PATCH] refactor: inject job_id --- bats/examples.bats | 3 +++ .../20231211151809_cala_server_setup.sql | 3 ++- cala-server/schema.graphql | 1 + .../src/extension/cala_outbox_import/mutation.rs | 4 +++- cala-server/src/graphql/primitives.rs | 16 +++++++++++----- cala-server/src/integration/mod.rs | 4 ++-- cala-server/src/job/entity.rs | 6 ++---- cala-server/src/job/mod.rs | 2 ++ 8 files changed, 26 insertions(+), 13 deletions(-) diff --git a/bats/examples.bats b/bats/examples.bats index 563d98f7..d0f26cce 100644 --- a/bats/examples.bats +++ b/bats/examples.bats @@ -23,10 +23,13 @@ wait_for_new_import_job() { exec_graphql 'list-accounts' accounts_before=$(graphql_output '.data.accounts.nodes | length') + job_id=$(random_uuid) variables=$( jq -n \ + --arg jobId "$job_id" \ '{ input: { + jobId: $jobId, name: "rust-example", endpoint: "http://localhost:2253" } diff --git a/cala-server/migrations/20231211151809_cala_server_setup.sql b/cala-server/migrations/20231211151809_cala_server_setup.sql index e60fce03..94b40527 100644 --- a/cala-server/migrations/20231211151809_cala_server_setup.sql +++ b/cala-server/migrations/20231211151809_cala_server_setup.sql @@ -1,8 +1,9 @@ CREATE TABLE jobs ( id UUID NOT NULL UNIQUE, - name VARCHAR NOT NULL UNIQUE, + name VARCHAR NOT NULL, created_at TIMESTAMPTZ NOT NULL DEFAULT NOW() ); +CREATE INDEX idx_jobs_name ON jobs (name); CREATE TABLE job_events ( id UUID REFERENCES jobs(id) NOT NULL, diff --git a/cala-server/schema.graphql b/cala-server/schema.graphql index 5bc02231..e120eb59 100644 --- a/cala-server/schema.graphql +++ b/cala-server/schema.graphql @@ -122,6 +122,7 @@ type BalanceAmount { input CalaOutboxImportJobCreateInput { + jobId: UUID! name: String! description: String endpoint: String! diff --git a/cala-server/src/extension/cala_outbox_import/mutation.rs b/cala-server/src/extension/cala_outbox_import/mutation.rs index 1d2d2874..f8687642 100644 --- a/cala-server/src/extension/cala_outbox_import/mutation.rs +++ b/cala-server/src/extension/cala_outbox_import/mutation.rs @@ -3,11 +3,12 @@ use async_graphql::*; use super::job::*; use crate::{ app::CalaApp, - graphql::{DbOp, Job}, + graphql::{primitives::UUID, DbOp, Job}, }; #[derive(InputObject)] pub struct CalaOutboxImportJobCreateInput { + pub job_id: UUID, pub name: String, pub description: Option, pub endpoint: String, @@ -37,6 +38,7 @@ impl Mutation { .jobs() .create_and_spawn_in_op::( &mut op, + input.job_id, input.name.clone(), input.description.clone(), CalaOutboxImportConfig::from(input), diff --git a/cala-server/src/graphql/primitives.rs b/cala-server/src/graphql/primitives.rs index 6f0fe7f4..cd753ae6 100644 --- a/cala-server/src/graphql/primitives.rs +++ b/cala-server/src/graphql/primitives.rs @@ -68,7 +68,7 @@ pub enum ParamDataType { Json, } -#[derive(Clone, Copy, Serialize, Deserialize)] +#[derive(Clone, Debug, Copy, Serialize, Deserialize)] #[serde(transparent)] pub struct UUID(uuid::Uuid); scalar!(UUID); @@ -103,15 +103,21 @@ impl From for cala_ledger::TxTemplateId { } } -impl From for crate::integration::IntegrationId { +impl From for cala_ledger::TransactionId { fn from(uuid: UUID) -> Self { - crate::integration::IntegrationId::from(uuid.0) + cala_ledger::TransactionId::from(uuid.0) } } -impl From for cala_ledger::TransactionId { +impl From for crate::primitives::JobId { fn from(uuid: UUID) -> Self { - cala_ledger::TransactionId::from(uuid.0) + crate::primitives::JobId::from(uuid.0) + } +} + +impl From for crate::integration::IntegrationId { + fn from(uuid: UUID) -> Self { + crate::integration::IntegrationId::from(uuid.0) } } diff --git a/cala-server/src/integration/mod.rs b/cala-server/src/integration/mod.rs index e6414c9c..78931fb7 100644 --- a/cala-server/src/integration/mod.rs +++ b/cala-server/src/integration/mod.rs @@ -35,11 +35,11 @@ impl Integrations { pub async fn create_in_op( &self, op: &mut AtomicOperation<'_>, - id: IntegrationId, + id: impl Into + std::fmt::Debug, name: String, data: impl serde::Serialize, ) -> Result { - let integration = Integration::new(id, name, data); + let integration = Integration::new(id.into(), name, data); sqlx::query!( r#"INSERT INTO integrations (id, name, data) VALUES ($1, $2, $3)"#, diff --git a/cala-server/src/job/entity.rs b/cala-server/src/job/entity.rs index 89940ba4..9b20f94d 100644 --- a/cala-server/src/job/entity.rs +++ b/cala-server/src/job/entity.rs @@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize}; use std::borrow::Cow; use super::error::JobError; -use crate::primitives::JobId; +pub use crate::primitives::JobId; #[derive(Clone, Eq, Hash, PartialEq, Debug, Serialize, Deserialize, sqlx::Type)] #[sqlx(transparent)] @@ -111,9 +111,7 @@ pub struct NewJob { impl NewJob { pub fn builder() -> NewJobBuilder { - let mut builder = NewJobBuilder::default(); - builder.id(JobId::new()); - builder + NewJobBuilder::default() } pub(super) fn initial_events(self) -> EntityEvents { diff --git a/cala-server/src/job/mod.rs b/cala-server/src/job/mod.rs index 9a6b5e4c..b38d5620 100644 --- a/cala-server/src/job/mod.rs +++ b/cala-server/src/job/mod.rs @@ -47,11 +47,13 @@ impl Jobs { pub async fn create_and_spawn_in_op( &self, op: &mut AtomicOperation<'_>, + id: impl Into + std::fmt::Debug, name: String, description: Option, data: D, ) -> Result { let new_job = NewJob::builder() + .id(id) .name(name) .description(description) .data(data)?