keelson-gen 0.1.0

keelson's code generator: introspect a live schema, emit readable model .rs files against keelson-models.
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
//! The checked-in generated PostgreSQL models compiled and run through the
//! **same SQL-shape assertions the hand-written spec runs**
//! (`keelson-models/tests/spec_psql.rs`) — judged by the real PostgreSQL
//! grammar, no server required. With `--features live-docker`, the spec's
//! end-to-end transaction tests run too, against the containerised
//! PostgreSQL 17.

// `pub` throughout the generated files because that is what the generator
// emits into an application's models crate; this test binary has no
// external readers.
#[allow(unreachable_pub, dead_code)]
// The fixture is prettyplease-formatted by the generator; rustfmt must not
// rewrite it, or the byte-identical freshness test would fight `cargo fmt`.
#[rustfmt::skip]
#[path = "generated/psql/mod.rs"]
mod models;

use keelson_core::{Query as _, QueryExtensions as _, Value};
use keelson_models::{null, set};
use keelson_psql::select;
use keelson_sqlcheck::Dialect;

use models::{posts, users};

/// The application's hand-written hooks — the psql twin of the sqlite
/// behaviour test's, with the spec model's behaviour.
// `pub` because the generated delegations call through `crate::hooks::…`;
// this test binary has no external readers.
#[allow(unreachable_pub)]
mod hooks {
    pub mod users {
        use keelson_exec::{ExecError, ExecFuture, Execute as _, Executor};
        use keelson_models::Set;
        use keelson_psql::{arg, insert, quote};

        use crate::models::users::{Setter, User};

        pub fn before_insert<'a>(
            _db: &'a dyn Executor,
            setter: &'a mut Setter,
        ) -> ExecFuture<'a, Result<(), ExecError>> {
            Box::pin(async move {
                if let Set::Value(email) = &mut setter.email {
                    *email = email.to_lowercase();
                }
                Ok(())
            })
        }

        pub fn after_insert<'a>(
            db: &'a dyn Executor,
            rows: &'a [User],
        ) -> ExecFuture<'a, Result<(), ExecError>> {
            Box::pin(async move {
                for u in rows {
                    keelson_psql::insert((
                        insert::into(quote("tags")).columns(["id", "name"]),
                        insert::values((arg(u.id), arg(format!("audit-user-{}", u.id)))),
                    ))
                    .execute(db)
                    .await?;
                }
                Ok(())
            })
        }
    }
}

#[track_caller]
fn assert_psql(q: &impl keelson_core::Query, expected: &str) -> Vec<Value> {
    let (sql, args) = q.build().expect("build");
    keelson_sqlcheck::assert_sql(Dialect::Psql, &sql, expected);
    args
}

const USER_COLS: &str = concat!(
    r#""users"."id", "users"."name", "users"."email", "users"."age", "#,
    r#""users"."is_active", "users"."created_at""#
);

const POST_COLS: &str = concat!(
    r#""posts"."id", "posts"."user_id", "posts"."title", "posts"."status", "#,
    r#""posts"."views", "posts"."published_at""#
);

#[test]
fn the_agreed_call_site_shape_builds_the_agreed_sql() {
    let q = users::table().query((
        users::age().gte(21), // typed: `users::age().gte("x")` does not compile
        select::limit(20),    // Layer 1 mods mix in directly
    ));
    let args = assert_psql(
        &q,
        &format!(r#"SELECT {USER_COLS} FROM "users" WHERE ("users"."age" >= $1) LIMIT 20"#),
    );
    assert_eq!(args, vec![Value::I32(21)]);
}

#[test]
fn a_partial_setter_inserts_only_the_set_columns() {
    use keelson_models::Table as _;
    let q = models::users::Users::insert_query(users::Setter {
        name: set("Stephen"),
        email: set("stephen@example.com"),
        ..Default::default()
    });
    let args = assert_psql(
        &q,
        &format!(r#"INSERT INTO "users" ("name", "email") VALUES ($1, $2) RETURNING {USER_COLS}"#),
    );
    assert_eq!(
        args,
        vec![
            Value::Text("Stephen".into()),
            Value::Text("stephen@example.com".into())
        ]
    );
}

#[test]
fn null_and_unset_are_different_statements() {
    use keelson_models::Table as _;
    let q = models::users::Users::insert_query(users::Setter {
        name: set("kay"),
        email: null(),
        ..Default::default()
    });
    let args = assert_psql(
        &q,
        &format!(r#"INSERT INTO "users" ("name", "email") VALUES ($1, $2) RETURNING {USER_COLS}"#),
    );
    assert_eq!(args, vec![Value::Text("kay".into()), Value::Null]);
}

#[test]
fn an_all_unset_setter_is_default_values() {
    use keelson_models::Table as _;
    let q = models::users::Users::insert_query(users::Setter::default());
    assert_psql(
        &q,
        &format!(r#"INSERT INTO "users" DEFAULT VALUES RETURNING {USER_COLS}"#),
    );
}

#[test]
fn update_sets_only_the_set_fields_and_filters_typed() {
    use keelson_models::Table as _;
    use keelson_psql::Mod as _;
    let mut q = models::users::Users::update_query();
    users::id().eq(7).apply(&mut q);
    models::users::Users::apply_setter(
        users::Setter {
            email: null(),
            age: set(30),
            ..Default::default()
        },
        &mut q,
    );
    let args = assert_psql(
        &q,
        r#"UPDATE "users" SET "email" = $1, "age" = $2 WHERE ("users"."id" = $3)"#,
    );
    assert_eq!(args, vec![Value::Null, Value::I32(30), Value::I32(7)]);
}

#[test]
fn delete_takes_the_same_typed_filters() {
    use keelson_models::Table as _;
    use keelson_psql::Mod as _;
    let mut q = models::users::Users::delete_query();
    users::id().in_([1, 2]).apply(&mut q);
    let args = assert_psql(
        &q,
        r#"DELETE FROM "users" WHERE ("users"."id" IN ($1, $2))"#,
    );
    assert_eq!(args.len(), 2);
}

#[test]
fn a_preload_is_one_left_joined_query_with_prefixed_columns() {
    let q = posts::table().query((posts::preload::user(), posts::views().gte(10)));
    let args = assert_psql(
        &q,
        &format!(
            concat!(
                r#"SELECT {}, "#,
                r#""users"."id" AS "user.id", "users"."name" AS "user.name", "#,
                r#""users"."email" AS "user.email", "users"."age" AS "user.age", "#,
                r#""users"."is_active" AS "user.is_active", "#,
                r#""users"."created_at" AS "user.created_at" "#,
                r#"FROM "posts" LEFT JOIN "users" ON ("users"."id" = "posts"."user_id") "#,
                r#"WHERE ("posts"."views" >= $1)"#
            ),
            POST_COLS
        ),
    );
    assert_eq!(args, vec![Value::I32(10)]);
}

#[test]
fn raw_fragments_and_dialect_mods_mix_into_a_model_query() {
    // The spec runs this on its hand-written SELECT-only projection; the
    // generated set has no such projection over `users`, so the same mixing
    // is asserted on the full model.
    let q = users::table().query((
        users::email().is_not_null(),
        select::where_(r#""users"."age" IS NOT NULL"#),
        select::order_by(users::id()).desc(),
        select::limit(5),
    ));
    assert_psql(
        &q,
        &format!(
            concat!(
                r#"SELECT {} FROM "users" "#,
                r#"WHERE ("users"."email" IS NOT NULL) AND "users"."age" IS NOT NULL "#,
                r#"ORDER BY "users"."id" DESC LIMIT 5"#
            ),
            USER_COLS
        ),
    );
}

#[test]
fn aliased_as_follows_a_table_alias() {
    use keelson_psql::quote;
    let q = keelson_psql::select((
        select::columns(users::id().aliased_as("u")),
        select::from(quote("users")).as_("u"),
        select::where_(users::age().aliased_as("u").gte(21)),
    ));
    let args = assert_psql(
        &q,
        r#"SELECT "u"."id" FROM "users" AS "u" WHERE ("u"."age" >= $1)"#,
    );
    assert_eq!(args, vec![Value::I32(21)]);
}

#[test]
fn the_query_extensions_wiring_is_observable() {
    let q = posts::table().query((posts::preload::user(), posts::then_load::user()));
    assert_eq!(q.mapper_mods().len(), 1);
    assert_eq!(q.loaders().len(), 1);
    assert!(q.hooks().is_empty());
    assert_eq!(q.query_type(), keelson_core::QueryType::Select);
}

// ─────────────────── end-to-end against PostgreSQL 17 ───────────────────

#[cfg(feature = "live-docker")]
mod live {
    use std::sync::OnceLock;
    use std::sync::atomic::{AtomicI32, Ordering};

    use keelson_exec::{BeginExt as _, ExecError, Execute as _, Executor};
    use keelson_models::{null, set};
    use keelson_psql::{Chain as _, arg, quote, select};

    use super::models::{messages, post_authors, posts, threads, user_emails, users};

    /// Process-unique positive i32 keys, so runs against a shared or
    /// persistent server never collide.
    fn key() -> i32 {
        static NEXT: AtomicI32 = AtomicI32::new(0);
        static BASE: OnceLock<i32> = OnceLock::new();
        let base = *BASE.get_or_init(|| {
            let nanos = std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_nanos();
            ((nanos as i64) & 0x3fff_ff00) as i32
        });
        base + NEXT.fetch_add(1, Ordering::Relaxed)
    }

    async fn pool() -> keelson_sqlx::psql::Pool {
        let url = tokio::task::spawn_blocking(|| keelson_sqlcheck::live::psql_url().to_owned())
            .await
            .unwrap();
        keelson_sqlx::psql::Pool::connect(&url)
            .await
            .expect("connecting to the live PostgreSQL")
    }

    async fn audit_tag_count(db: &dyn Executor, user_id: i32) -> i64 {
        keelson_psql::select((
            select::columns("count(*)"),
            select::from(quote("tags")),
            select::where_(quote("name").eq(arg(format!("audit-user-{user_id}")))),
        ))
        .fetch_scalar(db)
        .await
        .unwrap()
    }

    /// The spec's whole-model-flow transaction test, on the generated
    /// models: partial setter insert with RETURNING defaults, the
    /// before-insert rewrite, the after-insert write observed inside the
    /// same transaction, preload and then-load — then rolled back, hook
    /// write included.
    #[tokio::test]
    async fn the_model_flow_runs_inside_the_callers_transaction() {
        let db = pool().await;
        let uid = key();
        let uid2 = key();
        let pid = key();
        let pid2 = key();

        let out: Result<(), ExecError> = db
            .within(async |tx| {
                let u = users::table()
                    .insert(users::Setter {
                        id: set(uid),
                        name: set("Stephen"),
                        email: set("STEPHEN@Example.COM"),
                        ..Default::default()
                    })
                    .one(tx)
                    .await?;
                assert_eq!(u.email.as_deref(), Some("stephen@example.com"));
                assert!(u.is_active);
                assert_eq!(u.age, None);
                assert_eq!(audit_tag_count(tx, uid).await, 1);

                users::table()
                    .insert(users::Setter {
                        id: set(uid2),
                        name: set("Ada"),
                        age: set(36),
                        ..Default::default()
                    })
                    .one(tx)
                    .await?;

                posts::table()
                    .insert(posts::Setter {
                        id: set(pid),
                        user_id: set(uid),
                        title: set("keel laid"),
                        ..Default::default()
                    })
                    .one(tx)
                    .await?;
                posts::table()
                    .insert(posts::Setter {
                        id: set(pid2),
                        user_id: set(uid),
                        title: set("second"),
                        status: null(),
                        ..Default::default()
                    })
                    .one(tx)
                    .await?;

                let adults = users::table()
                    .query((
                        users::age().gte(21),
                        users::id().in_([uid, uid2]),
                        select::limit(20),
                    ))
                    .all(tx)
                    .await?;
                assert_eq!(adults.len(), 1);
                assert_eq!(adults[0].name, "Ada");

                let loaded = posts::table()
                    .query((posts::preload::user(), posts::id().eq(pid)))
                    .one(tx)
                    .await?;
                let author = loaded.rel.user.as_ref().expect("preloaded user");
                assert_eq!(author.id, uid);
                assert_eq!(author.email.as_deref(), Some("stephen@example.com"));

                let with_posts = users::table()
                    .query((users::id().eq(uid), users::then_load::posts()))
                    .one(tx)
                    .await?;
                assert_eq!(with_posts.rel.posts.len(), 2);

                let with_user = posts::table()
                    .query((posts::id().eq(pid2), posts::then_load::user()))
                    .one(tx)
                    .await?;
                assert_eq!(with_user.rel.user.as_ref().unwrap().id, uid);

                let done = users::table()
                    .update(
                        users::Setter {
                            age: set(41),
                            ..Default::default()
                        },
                        users::id().eq(uid),
                    )
                    .exec(tx)
                    .await?;
                assert_eq!(done.rows_affected, 1);

                let done = posts::table().delete(posts::id().eq(pid2)).exec(tx).await?;
                assert_eq!(done.rows_affected, 1);

                Err(ExecError::other("deliberate rollback"))
            })
            .await;
        assert_eq!(out.unwrap_err().to_string(), "deliberate rollback");

        assert_eq!(audit_tag_count(&db, uid).await, 0);
        let ghosts = users::table()
            .query(users::id().in_([uid, uid2]))
            .all(&db)
            .await
            .unwrap();
        assert!(ghosts.is_empty());
    }

    /// The commit half: on a plain pool the hook's write persists with the
    /// insert.
    #[tokio::test]
    async fn hooks_commit_with_the_caller() {
        let db = pool().await;
        let uid = key();

        users::table()
            .insert(users::Setter {
                id: set(uid),
                name: set("committed"),
                ..Default::default()
            })
            .one(&db)
            .await
            .unwrap();
        assert_eq!(audit_tag_count(&db, uid).await, 1);

        // Clean up after ourselves — the server may be shared and persistent.
        keelson_psql::delete((
            keelson_psql::delete::from(quote("tags")),
            keelson_psql::delete::where_(quote("name").eq(arg(format!("audit-user-{uid}")))),
        ))
        .execute(&db)
        .await
        .unwrap();
        users::table()
            .delete(users::id().eq(uid))
            .exec(&db)
            .await
            .unwrap();
    }

    /// Relations involving views, against the real server: `post_authors` and
    /// `user_emails` are `SELECT`-only models, every relation below came from
    /// a `[[relationships]]` block with its `cardinality` declared, and both
    /// loading strategies have to bring back the same rows. Rolled back, so a
    /// shared server keeps nothing.
    #[tokio::test]
    async fn view_relations_load_against_the_server() {
        let db = pool().await;
        let uid = key();
        let pid = key();

        let out: Result<(), ExecError> = db
            .within(async |tx| {
                users::table()
                    .insert(users::Setter {
                        id: set(uid),
                        name: set("Grace"),
                        email: set("grace@example.com"),
                        ..Default::default()
                    })
                    .one(tx)
                    .await?;
                posts::table()
                    .insert(posts::Setter {
                        id: set(pid),
                        user_id: set(uid),
                        title: set("compiler"),
                        ..Default::default()
                    })
                    .one(tx)
                    .await?;

                // The view as the target of a to-one relation, both ways.
                let preloaded = posts::table()
                    .query((posts::preload::authorship(), posts::id().eq(pid)))
                    .one(tx)
                    .await?;
                let a = preloaded.rel.authorship.as_ref().expect("view row");
                assert_eq!(a.user_name.as_deref(), Some("Grace"));

                let then_loaded = posts::table()
                    .query((posts::then_load::authorship(), posts::id().eq(pid)))
                    .one(tx)
                    .await?;
                assert_eq!(then_loaded.rel.authorship, preloaded.rel.authorship);

                // The view as the holder of a relation.
                let from_view = post_authors::view()
                    .query((
                        post_authors::then_load::user(),
                        post_authors::post_id().eq(pid),
                    ))
                    .one(tx)
                    .await?;
                assert_eq!(from_view.rel.user.as_ref().unwrap().id, uid);

                // The declared cardinality decides the back-reference shape:
                // a `Vec` for many_to_one, a boxed `Option` for one_to_one.
                let back = users::table()
                    .query((
                        users::id().eq(uid),
                        users::then_load::post_authors(),
                        users::then_load::user_emails(),
                    ))
                    .one(tx)
                    .await?;
                assert_eq!(back.rel.post_authors.len(), 1);
                assert_eq!(
                    back.rel
                        .user_emails
                        .as_deref()
                        .and_then(|e| e.email.clone()),
                    Some("grace@example.com".to_owned())
                );

                // PostgreSQL calls `user_emails` auto-updatable, but the
                // config declares no key for it, so it is still SELECT-only:
                // updatability is permission to write, not an identity to
                // write by.
                let rows = user_emails::view()
                    .query(user_emails::id().eq(uid))
                    .all(tx)
                    .await?;
                assert_eq!(rows.len(), 1);

                Err(ExecError::other("deliberate rollback"))
            })
            .await;
        assert_eq!(out.unwrap_err().to_string(), "deliberate rollback");
    }

    /// The mutually referencing base tables against the real server:
    /// `threads.first_message_id → messages` and `messages.thread_id →
    /// threads`, so `Thread.rel.first_message` and `Message.rel.thread`
    /// refer to each other. That this module compiles at all is the boxing
    /// rule working; this test is that the relation still loads, both ways
    /// and around the cycle. Rolled back, so a shared server keeps nothing.
    #[tokio::test]
    async fn the_mutually_referencing_pair_loads_against_the_server() {
        let db = pool().await;
        let tid = key();
        let mid = key();

        let out: Result<(), ExecError> = db
            .within(async |tx| {
                threads::table()
                    .insert(threads::Setter {
                        id: set(tid),
                        title: set("keel laid"),
                        ..Default::default()
                    })
                    .one(tx)
                    .await?;
                messages::table()
                    .insert(messages::Setter {
                        id: set(mid),
                        thread_id: set(tid),
                        body: set("first"),
                    })
                    .one(tx)
                    .await?;
                threads::table()
                    .update(
                        threads::Setter {
                            first_message_id: set(mid),
                            ..Default::default()
                        },
                        threads::id().eq(tid),
                    )
                    .exec(tx)
                    .await?;

                // Same-query preload, thread → its opening message.
                let thread = threads::table()
                    .query((threads::preload::first_message(), threads::id().eq(tid)))
                    .one(tx)
                    .await?;
                assert_eq!(
                    thread
                        .rel
                        .first_message
                        .as_deref()
                        .expect("the opening message")
                        .body,
                    "first"
                );

                // Then-load, the other way: message → its thread.
                let message = messages::table()
                    .query((messages::then_load::thread(), messages::id().eq(mid)))
                    .one(tx)
                    .await?;
                assert_eq!(
                    message.rel.thread.as_deref().expect("the thread").title,
                    "keel laid"
                );

                // And once around the cycle as a two-level path.
                let round = messages::table()
                    .query((
                        messages::then_load::thread().then(threads::then_load::first_message()),
                        messages::id().eq(mid),
                    ))
                    .one(tx)
                    .await?;
                assert_eq!(
                    round
                        .rel
                        .thread
                        .as_ref()
                        .expect("the thread")
                        .rel
                        .first_message
                        .as_ref()
                        .expect("its opening message")
                        .id,
                    mid,
                );

                Err(ExecError::other("deliberate rollback"))
            })
            .await;
        assert_eq!(out.unwrap_err().to_string(), "deliberate rollback");
    }
}