Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: inject job_id #99

Merged
merged 1 commit into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions bats/examples.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
3 changes: 2 additions & 1 deletion cala-server/migrations/20231211151809_cala_server_setup.sql
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
1 change: 1 addition & 0 deletions cala-server/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ type BalanceAmount {


input CalaOutboxImportJobCreateInput {
jobId: UUID!
name: String!
description: String
endpoint: String!
Expand Down
4 changes: 3 additions & 1 deletion cala-server/src/extension/cala_outbox_import/mutation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
pub endpoint: String,
Expand Down Expand Up @@ -37,6 +38,7 @@ impl Mutation {
.jobs()
.create_and_spawn_in_op::<CalaOutboxImportJobInitializer, _>(
&mut op,
input.job_id,
input.name.clone(),
input.description.clone(),
CalaOutboxImportConfig::from(input),
Expand Down
16 changes: 11 additions & 5 deletions cala-server/src/graphql/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -103,15 +103,21 @@ impl From<UUID> for cala_ledger::TxTemplateId {
}
}

impl From<UUID> for crate::integration::IntegrationId {
impl From<UUID> for cala_ledger::TransactionId {
fn from(uuid: UUID) -> Self {
crate::integration::IntegrationId::from(uuid.0)
cala_ledger::TransactionId::from(uuid.0)
}
}

impl From<UUID> for cala_ledger::TransactionId {
impl From<UUID> for crate::primitives::JobId {
fn from(uuid: UUID) -> Self {
cala_ledger::TransactionId::from(uuid.0)
crate::primitives::JobId::from(uuid.0)
}
}

impl From<UUID> for crate::integration::IntegrationId {
fn from(uuid: UUID) -> Self {
crate::integration::IntegrationId::from(uuid.0)
}
}

Expand Down
4 changes: 2 additions & 2 deletions cala-server/src/integration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ impl Integrations {
pub async fn create_in_op(
&self,
op: &mut AtomicOperation<'_>,
id: IntegrationId,
id: impl Into<IntegrationId> + std::fmt::Debug,
name: String,
data: impl serde::Serialize,
) -> Result<Integration, sqlx::Error> {
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)"#,
Expand Down
6 changes: 2 additions & 4 deletions cala-server/src/job/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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<JobEvent> {
Expand Down
2 changes: 2 additions & 0 deletions cala-server/src/job/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ impl Jobs {
pub async fn create_and_spawn_in_op<I: JobInitializer + Default, D: serde::Serialize>(
&self,
op: &mut AtomicOperation<'_>,
id: impl Into<JobId> + std::fmt::Debug,
name: String,
description: Option<String>,
data: D,
) -> Result<Job, JobError> {
let new_job = NewJob::builder()
.id(id)
.name(name)
.description(description)
.data(data)?
Expand Down
Loading