runledger-postgres 0.12.0

PostgreSQL persistence layer for the Runledger durable job and workflow system
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
use std::collections::BTreeSet;

use runledger_core::jobs::{
    StepKey, WorkflowRunEnqueueBuilder, WorkflowStepEnqueueBuilder, WorkflowType,
};
use runledger_postgres::DbPool;
use runledger_postgres::jobs::{
    WorkflowRunDbRecord, WorkflowRunReadCountFilter, WorkflowRunReadListFilter,
    WorkflowRunReadScope, count_workflow_runs, count_workflow_runs_with_scope,
    count_workflow_step_dependencies_with_scope, count_workflow_steps_with_scope,
    enqueue_workflow_run, get_latest_workflow_run_by_type_with_scope, get_workflow_run_by_id,
    get_workflow_run_by_id_with_scope, list_workflow_runs, list_workflow_runs_with_scope,
    list_workflow_step_dependencies_page_with_scope, list_workflow_step_dependencies_with_scope,
    list_workflow_steps_page_with_scope, list_workflow_steps_with_scope,
};
use runledger_test_support::{setup_ephemeral_pool, teardown_ephemeral_pool};
use serde_json::json;
use sqlx::types::Uuid;

const WORKFLOW_TYPE: &str = "workflow.test.read_scope";

macro_rules! assert_read_len {
    ($query:expr, $expected:expr, $message:literal) => {
        assert_eq!(($query).await.expect($message).len(), $expected, $message,);
    };
}

macro_rules! assert_read_count {
    ($query:expr, $expected:expr, $message:literal) => {
        assert_eq!(($query).await.expect($message), $expected, $message);
    };
}

async fn record_postgres_server_version(pool: &DbPool) {
    let server_version = sqlx::query_scalar::<_, String>("SHOW server_version")
        .fetch_one(pool)
        .await
        .expect("read PostgreSQL server_version");
    let server_version_num =
        sqlx::query_scalar::<_, i32>("SELECT current_setting('server_version_num')::int")
            .fetch_one(pool)
            .await
            .expect("read PostgreSQL server_version_num");
    eprintln!(
        "workflow read-scope regression PostgreSQL server_version={server_version}, \
         server_version_num={server_version_num}"
    );
    assert_eq!(
        server_version_num / 10_000,
        18,
        "workflow read-scope regression must run on PostgreSQL 18"
    );
}

async fn enqueue_scoped_workflow(
    pool: &DbPool,
    organization_id: Option<Uuid>,
    label: &str,
) -> WorkflowRunDbRecord {
    let payload = json!({"scope": label});
    let metadata = json!({"scope": label});
    let root = WorkflowStepEnqueueBuilder::new_external(StepKey::new("root"), &payload)
        .try_build()
        .expect("build root external step");
    let dependent = WorkflowStepEnqueueBuilder::new_external(StepKey::new("dependent"), &payload)
        .depends_on_terminal(&[StepKey::new("root")])
        .try_build()
        .expect("build dependent external step");
    let builder = WorkflowRunEnqueueBuilder::new(WorkflowType::new(WORKFLOW_TYPE), &metadata);
    let builder = match organization_id {
        Some(organization_id) => builder.organization_id(organization_id),
        None => builder,
    };
    let workflow = builder
        .step(root)
        .step(dependent)
        .try_build()
        .expect("build scoped workflow");

    enqueue_workflow_run(pool, &workflow)
        .await
        .expect("enqueue scoped workflow")
}

fn run_ids(runs: Vec<WorkflowRunDbRecord>) -> BTreeSet<Uuid> {
    runs.into_iter().map(|run| run.id).collect()
}

fn list_filter(scope: WorkflowRunReadScope) -> WorkflowRunReadListFilter<'static> {
    WorkflowRunReadListFilter {
        scope,
        status: None,
        workflow_type: Some(WORKFLOW_TYPE),
        limit: 10,
        offset: 0,
    }
}

fn count_filter(scope: WorkflowRunReadScope) -> WorkflowRunReadCountFilter<'static> {
    WorkflowRunReadCountFilter {
        scope,
        status: None,
        workflow_type: Some(WORKFLOW_TYPE),
    }
}

#[tokio::test]
async fn workflow_read_scope_is_exact_for_get_list_and_count_apis() {
    let (pool, database) = setup_ephemeral_pool("postgres_workflow_read_scope", 4).await;
    record_postgres_server_version(&pool).await;

    let organization_id = Uuid::from_u128(22_001);
    let other_organization_id = Uuid::from_u128(22_002);
    let wrong_organization_id = Uuid::from_u128(22_003);
    let global_run = enqueue_scoped_workflow(&pool, None, "global").await;
    let organization_run =
        enqueue_scoped_workflow(&pool, Some(organization_id), "organization").await;
    let other_organization_run =
        enqueue_scoped_workflow(&pool, Some(other_organization_id), "other-organization").await;

    let global = WorkflowRunReadScope::Global;
    let organization = WorkflowRunReadScope::Organization(organization_id);
    let wrong_organization = WorkflowRunReadScope::Organization(wrong_organization_id);
    let admin = WorkflowRunReadScope::Admin;

    assert_eq!(
        get_workflow_run_by_id_with_scope(&pool, global, global_run.id)
            .await
            .expect("load global run")
            .map(|run| run.id),
        Some(global_run.id),
        "global scope reads only the exact global run"
    );
    assert!(
        get_workflow_run_by_id_with_scope(&pool, global, organization_run.id)
            .await
            .expect("reject organization run from global scope")
            .is_none(),
        "global scope must not read an organization run"
    );
    assert_eq!(
        get_workflow_run_by_id_with_scope(&pool, organization, organization_run.id)
            .await
            .expect("load organization run")
            .map(|run| run.id),
        Some(organization_run.id),
        "organization scope reads its exact organization run"
    );
    assert!(
        get_workflow_run_by_id_with_scope(&pool, wrong_organization, organization_run.id)
            .await
            .expect("reject wrong organization")
            .is_none(),
        "wrong organization scope must not read another organization run"
    );
    assert_eq!(
        get_workflow_run_by_id_with_scope(&pool, admin, organization_run.id)
            .await
            .expect("admin loads organization run")
            .map(|run| run.id),
        Some(organization_run.id),
        "admin scope reads organization runs"
    );

    assert_eq!(
        get_latest_workflow_run_by_type_with_scope(&pool, global, WorkflowType::new(WORKFLOW_TYPE))
            .await
            .expect("load latest global run")
            .map(|run| run.id),
        Some(global_run.id),
        "latest global read is exact"
    );
    assert_eq!(
        get_latest_workflow_run_by_type_with_scope(
            &pool,
            organization,
            WorkflowType::new(WORKFLOW_TYPE),
        )
        .await
        .expect("load latest organization run")
        .map(|run| run.id),
        Some(organization_run.id),
        "latest organization read is exact"
    );
    assert!(
        get_latest_workflow_run_by_type_with_scope(
            &pool,
            wrong_organization,
            WorkflowType::new(WORKFLOW_TYPE),
        )
        .await
        .expect("reject latest wrong organization")
        .is_none(),
        "wrong organization has no latest run"
    );
    assert!(
        get_latest_workflow_run_by_type_with_scope(&pool, admin, WorkflowType::new(WORKFLOW_TYPE))
            .await
            .expect("admin loads a latest run")
            .is_some(),
        "admin can read across organizations"
    );

    assert_eq!(
        run_ids(
            list_workflow_runs_with_scope(&pool, &list_filter(global))
                .await
                .expect("list global workflow runs"),
        ),
        BTreeSet::from([global_run.id]),
        "global list is exact"
    );
    assert_eq!(
        run_ids(
            list_workflow_runs_with_scope(&pool, &list_filter(organization))
                .await
                .expect("list organization workflow runs"),
        ),
        BTreeSet::from([organization_run.id]),
        "organization list is exact"
    );
    assert_eq!(
        run_ids(
            list_workflow_runs_with_scope(&pool, &list_filter(admin))
                .await
                .expect("list workflow runs as admin"),
        ),
        BTreeSet::from([
            global_run.id,
            organization_run.id,
            other_organization_run.id
        ]),
        "admin list spans global and organization runs"
    );
    assert!(
        list_workflow_runs_with_scope(&pool, &list_filter(wrong_organization))
            .await
            .expect("list workflow runs for wrong organization")
            .is_empty(),
        "wrong organization list must be empty"
    );

    assert_read_count!(
        count_workflow_runs_with_scope(&pool, &count_filter(global)),
        1,
        "global count is exact"
    );
    assert_read_count!(
        count_workflow_runs_with_scope(&pool, &count_filter(organization)),
        1,
        "organization count is exact"
    );
    assert_read_count!(
        count_workflow_runs_with_scope(&pool, &count_filter(admin)),
        3,
        "admin count spans global and organization runs"
    );
    assert_read_count!(
        count_workflow_runs_with_scope(&pool, &count_filter(wrong_organization)),
        0,
        "wrong organization count is empty"
    );

    assert_read_len!(
        list_workflow_steps_with_scope(&pool, global, global_run.id),
        2,
        "global scope lists global steps"
    );
    assert_read_len!(
        list_workflow_steps_with_scope(&pool, global, organization_run.id),
        0,
        "global scope rejects organization steps"
    );
    assert_read_len!(
        list_workflow_steps_with_scope(&pool, organization, organization_run.id),
        2,
        "organization scope lists its steps"
    );
    assert_read_len!(
        list_workflow_steps_with_scope(&pool, admin, organization_run.id),
        2,
        "admin scope lists organization steps"
    );
    assert_read_len!(
        list_workflow_steps_with_scope(&pool, wrong_organization, organization_run.id),
        0,
        "wrong organization cannot list steps"
    );
    assert_read_len!(
        list_workflow_steps_page_with_scope(&pool, global, global_run.id, 10, 0),
        2,
        "global scope lists a global step page"
    );
    assert_read_len!(
        list_workflow_steps_page_with_scope(&pool, global, organization_run.id, 10, 0),
        0,
        "global scope rejects an organization step page"
    );
    assert_read_len!(
        list_workflow_steps_page_with_scope(&pool, organization, organization_run.id, 10, 0),
        2,
        "organization scope lists its step page"
    );
    assert_read_len!(
        list_workflow_steps_page_with_scope(&pool, admin, organization_run.id, 10, 0),
        2,
        "admin scope lists an organization step page"
    );
    assert_read_len!(
        list_workflow_steps_page_with_scope(&pool, wrong_organization, organization_run.id, 10, 0,),
        0,
        "wrong organization cannot list a step page"
    );
    assert_read_count!(
        count_workflow_steps_with_scope(&pool, global, global_run.id),
        2,
        "global scope counts global steps"
    );
    assert_read_count!(
        count_workflow_steps_with_scope(&pool, global, organization_run.id),
        0,
        "global scope rejects organization step count"
    );
    assert_read_count!(
        count_workflow_steps_with_scope(&pool, organization, organization_run.id),
        2,
        "organization scope counts its steps"
    );
    assert_read_count!(
        count_workflow_steps_with_scope(&pool, admin, organization_run.id),
        2,
        "admin scope counts organization steps"
    );
    assert_read_count!(
        count_workflow_steps_with_scope(&pool, wrong_organization, organization_run.id),
        0,
        "wrong organization cannot count steps"
    );

    assert_read_len!(
        list_workflow_step_dependencies_with_scope(&pool, global, global_run.id),
        1,
        "global scope lists global dependencies"
    );
    assert_read_len!(
        list_workflow_step_dependencies_with_scope(&pool, global, organization_run.id),
        0,
        "global scope rejects organization dependencies"
    );
    assert_read_len!(
        list_workflow_step_dependencies_with_scope(&pool, organization, organization_run.id),
        1,
        "organization scope lists its dependencies"
    );
    assert_read_len!(
        list_workflow_step_dependencies_with_scope(&pool, admin, organization_run.id),
        1,
        "admin scope lists organization dependencies"
    );
    assert_read_len!(
        list_workflow_step_dependencies_with_scope(&pool, wrong_organization, organization_run.id),
        0,
        "wrong organization cannot list dependencies"
    );
    assert_read_len!(
        list_workflow_step_dependencies_page_with_scope(&pool, global, global_run.id, 10, 0),
        1,
        "global scope lists a global dependency page"
    );
    assert_read_len!(
        list_workflow_step_dependencies_page_with_scope(&pool, global, organization_run.id, 10, 0),
        0,
        "global scope rejects an organization dependency page"
    );
    assert_read_len!(
        list_workflow_step_dependencies_page_with_scope(
            &pool,
            organization,
            organization_run.id,
            10,
            0,
        ),
        1,
        "organization scope lists its dependency page"
    );
    assert_read_len!(
        list_workflow_step_dependencies_page_with_scope(&pool, admin, organization_run.id, 10, 0),
        1,
        "admin scope lists an organization dependency page"
    );
    assert_read_len!(
        list_workflow_step_dependencies_page_with_scope(
            &pool,
            wrong_organization,
            organization_run.id,
            10,
            0,
        ),
        0,
        "wrong organization cannot list a dependency page"
    );
    assert_read_count!(
        count_workflow_step_dependencies_with_scope(&pool, global, global_run.id),
        1,
        "global scope counts global dependencies"
    );
    assert_read_count!(
        count_workflow_step_dependencies_with_scope(&pool, global, organization_run.id),
        0,
        "global scope rejects organization dependency count"
    );
    assert_read_count!(
        count_workflow_step_dependencies_with_scope(&pool, organization, organization_run.id),
        1,
        "organization scope counts its dependencies"
    );
    assert_read_count!(
        count_workflow_step_dependencies_with_scope(&pool, admin, organization_run.id),
        1,
        "admin scope counts organization dependencies"
    );
    assert_read_count!(
        count_workflow_step_dependencies_with_scope(&pool, wrong_organization, organization_run.id),
        0,
        "wrong organization cannot count dependencies"
    );

    assert!(
        get_workflow_run_by_id(&pool, None, organization_run.id)
            .await
            .expect("legacy get workflow run")
            .is_some(),
        "legacy None remains an admin wildcard"
    );
    assert_eq!(
        run_ids(
            list_workflow_runs(
                &pool,
                &runledger_postgres::jobs::WorkflowRunListFilter {
                    organization_id: None,
                    status: None,
                    workflow_type: Some(WORKFLOW_TYPE),
                    limit: 10,
                    offset: 0,
                },
            )
            .await
            .expect("legacy list workflow runs"),
        ),
        BTreeSet::from([
            global_run.id,
            organization_run.id,
            other_organization_run.id
        ]),
        "legacy None list remains an admin wildcard"
    );
    assert_read_count!(
        count_workflow_runs(
            &pool,
            &runledger_postgres::jobs::WorkflowRunCountFilter {
                organization_id: None,
                status: None,
                workflow_type: Some(WORKFLOW_TYPE),
            },
        ),
        3,
        "legacy None count remains an admin wildcard"
    );

    teardown_ephemeral_pool(pool, database).await;
}