prax-postgres 0.10.0

PostgreSQL driver for the Prax ORM with connection pooling
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
//! Live Postgres integration for phase-6 aggregate operations.
//! Gated #[ignore] — requires PRAX_E2E=1 and POSTGRES_URL.
//! Run: PRAX_E2E=1 POSTGRES_URL=... cargo test -p prax-postgres --test aggregate_macros -- --ignored
//!
//! Exercises the runtime AggregateOperation / GroupByOperation (what the
//! aggregate!/group_by!/count! macros lower to) against real Postgres.
//! The macro front-end is covered by trybuild fixtures (compile-level)
//! and the codegen unit tests; the schema-path relation_helpers bug
//! prevents end-to-end macro use in a test crate (see
//! tests/aggregate_macros_e2e.rs).
//!
//! Because Model::TABLE_NAME is a `&'static str` const we cannot set it to a
//! runtime-generated unique table name.  Tests therefore call
//! `AggregateOperation::build_sql` / `GroupByOperation::build_sql` for the
//! SQL shape and then execute via the raw `QueryEngine::aggregate_query` path,
//! swapping the static TABLE_NAME for the dynamic table name in the SQL
//! string.  This is the same execution path that `exec()` takes internally.

#![cfg(test)]

use std::collections::HashMap;
use std::sync::atomic::{AtomicU32, Ordering};
use std::time::Duration;

use prax_postgres::{PgEngine, PgPool, PgPoolBuilder};
use prax_query::filter::FilterValue;
use prax_query::operations::having;
use prax_query::operations::{
    AggregateOperation, AggregateResult, GroupByOperation, GroupByResult,
};
use prax_query::traits::{Model, QueryEngine};
use prax_query::types::OrderByField;

// =============================================================================
// Harness (mirrors computed_fields.rs)
// =============================================================================

static TABLE_COUNTER: AtomicU32 = AtomicU32::new(0);

fn unique_table(prefix: &str) -> String {
    let n = TABLE_COUNTER.fetch_add(1, Ordering::SeqCst);
    let pid = std::process::id();
    format!("agg_{prefix}_{pid}_{n}")
}

fn skip_unless_e2e() -> Option<String> {
    if std::env::var("PRAX_E2E").ok().as_deref() != Some("1") {
        return None;
    }
    std::env::var("POSTGRES_URL").ok()
}

async fn pool() -> PgPool {
    let url = skip_unless_e2e().expect("PRAX_E2E=1 and POSTGRES_URL required");
    PgPoolBuilder::new()
        .url(url)
        .max_connections(4)
        .connection_timeout(Duration::from_secs(10))
        .build()
        .await
        .expect("connect to postgres")
}

async fn drop_table(pool: &PgPool, table: &str) {
    let conn = pool.get().await.expect("acquire conn for cleanup");
    let _ = conn
        .batch_execute(&format!("DROP TABLE IF EXISTS {table}"))
        .await;
}

// =============================================================================
// Minimal Model stubs required by the typed operation builders.
// =============================================================================

struct CountModel;
impl Model for CountModel {
    const MODEL_NAME: &'static str = "CountModel";
    const TABLE_NAME: &'static str = "count_model_placeholder";
    const PRIMARY_KEY: &'static [&'static str] = &["id"];
    const COLUMNS: &'static [&'static str] = &["id", "email"];
}

struct ScoreModel;
impl Model for ScoreModel {
    const MODEL_NAME: &'static str = "ScoreModel";
    const TABLE_NAME: &'static str = "score_model_placeholder";
    const PRIMARY_KEY: &'static [&'static str] = &["id"];
    const COLUMNS: &'static [&'static str] = &["id", "score"];
}

struct TeamModel;
impl Model for TeamModel {
    const MODEL_NAME: &'static str = "TeamModel";
    const TABLE_NAME: &'static str = "team_model_placeholder";
    const PRIMARY_KEY: &'static [&'static str] = &["id"];
    const COLUMNS: &'static [&'static str] = &["id", "team_id", "score"];
}

struct RegionModel;
impl Model for RegionModel {
    const MODEL_NAME: &'static str = "RegionModel";
    const TABLE_NAME: &'static str = "region_model_placeholder";
    const PRIMARY_KEY: &'static [&'static str] = &["id"];
    const COLUMNS: &'static [&'static str] = &["id", "region"];
}

struct ViewsModel;
impl Model for ViewsModel {
    const MODEL_NAME: &'static str = "ViewsModel";
    const TABLE_NAME: &'static str = "views_model_placeholder";
    const PRIMARY_KEY: &'static [&'static str] = &["id"];
    const COLUMNS: &'static [&'static str] = &["id", "team_id", "views"];
}

// =============================================================================
// Test 1 — COUNT(*) round-trip
//
// Creates a table with 5 rows and verifies COUNT(*) returns 5.
// =============================================================================

#[tokio::test]
#[ignore = "requires running PostgreSQL via docker-compose (PRAX_E2E=1 + POSTGRES_URL)"]
async fn count_select_round_trip() {
    if skip_unless_e2e().is_none() {
        eprintln!("skipping: PRAX_E2E not set");
        return;
    }
    let pool = pool().await;
    let table = unique_table("count");
    drop_table(&pool, &table).await;

    {
        let conn = pool.get().await.expect("conn");
        conn.batch_execute(&format!(
            "CREATE TABLE {table} (id SERIAL PRIMARY KEY, email TEXT)"
        ))
        .await
        .expect("create table");

        // 3 rows with email, 2 with NULL
        conn.batch_execute(&format!(
            "INSERT INTO {table} (email) VALUES \
             ('a@example.com'), ('b@example.com'), ('c@example.com'), (NULL), (NULL)"
        ))
        .await
        .expect("insert rows");
    }

    let engine = PgEngine::new(pool.clone());
    let dialect = engine.dialect();

    // Build SQL via the operation builder, then swap in the real table name.
    let op: AggregateOperation<CountModel, PgEngine> = AggregateOperation::new().count();
    let (sql, params) = op.build_sql(dialect);
    let sql = sql.replace(CountModel::TABLE_NAME, &table);

    let mut rows = engine
        .aggregate_query(&sql, params)
        .await
        .expect("aggregate_query");

    let result = AggregateResult::from_row(rows.pop().unwrap_or_default());

    assert_eq!(
        result.count,
        Some(5),
        "COUNT(*) should be 5 (includes NULLs)"
    );

    drop_table(&pool, &table).await;
}

// =============================================================================
// Test 2 — SUM / AVG / COUNT(*) round-trip
//
// Inserts scores 10, 20, 30 and validates aggregate results.
// =============================================================================

#[tokio::test]
#[ignore = "requires running PostgreSQL via docker-compose (PRAX_E2E=1 + POSTGRES_URL)"]
async fn aggregate_sum_avg_count_round_trip() {
    if skip_unless_e2e().is_none() {
        eprintln!("skipping: PRAX_E2E not set");
        return;
    }
    let pool = pool().await;
    let table = unique_table("score");
    drop_table(&pool, &table).await;

    {
        let conn = pool.get().await.expect("conn");
        conn.batch_execute(&format!(
            "CREATE TABLE {table} (id SERIAL PRIMARY KEY, score INT NOT NULL)"
        ))
        .await
        .expect("create table");

        conn.batch_execute(&format!(
            "INSERT INTO {table} (score) VALUES (10), (20), (30)"
        ))
        .await
        .expect("insert rows");
    }

    let engine = PgEngine::new(pool.clone());
    let dialect = engine.dialect();

    let op: AggregateOperation<ScoreModel, PgEngine> =
        AggregateOperation::new().count().sum("score").avg("score");
    let (sql, params) = op.build_sql(dialect);
    let sql = sql.replace(ScoreModel::TABLE_NAME, &table);

    let mut rows = engine
        .aggregate_query(&sql, params)
        .await
        .expect("aggregate_query");

    let result = AggregateResult::from_row(rows.pop().unwrap_or_default());

    assert_eq!(result.count, Some(3), "COUNT(*) should be 3");

    let sum = result
        .sum_as_f64("score")
        .expect("sum(score) should be present");
    assert!(
        (sum - 60.0).abs() < 0.001,
        "SUM(score) should be 60, got {sum}"
    );

    let avg = result
        .avg_as_f64("score")
        .expect("avg(score) should be present");
    assert!(
        (avg - 20.0).abs() < 0.001,
        "AVG(score) should be 20.0, got {avg}"
    );

    drop_table(&pool, &table).await;
}

// =============================================================================
// Test 3 — GROUP BY + HAVING round-trip
//
// team 1 → 2 rows, team 2 → 4 rows.
// HAVING COUNT(*) > 3 should return only team 2 with count == 4.
// =============================================================================

#[tokio::test]
#[ignore = "requires running PostgreSQL via docker-compose (PRAX_E2E=1 + POSTGRES_URL)"]
async fn group_by_with_having_round_trip() {
    if skip_unless_e2e().is_none() {
        eprintln!("skipping: PRAX_E2E not set");
        return;
    }
    let pool = pool().await;
    let table = unique_table("team");
    drop_table(&pool, &table).await;

    {
        let conn = pool.get().await.expect("conn");
        conn.batch_execute(&format!(
            "CREATE TABLE {table} (id SERIAL PRIMARY KEY, team_id INT NOT NULL, score INT NOT NULL)"
        ))
        .await
        .expect("create table");

        // team 1 → 2 rows, team 2 → 4 rows
        conn.batch_execute(&format!(
            "INSERT INTO {table} (team_id, score) VALUES \
             (1, 10), (1, 20), \
             (2, 30), (2, 40), (2, 50), (2, 60)"
        ))
        .await
        .expect("insert rows");
    }

    let engine = PgEngine::new(pool.clone());
    let dialect = engine.dialect();

    let op: GroupByOperation<TeamModel, PgEngine> =
        GroupByOperation::new(vec!["team_id".to_string()])
            .count()
            .having(having::count_gt(3.0));
    let (sql, params) = op.build_sql(dialect);
    let sql = sql.replace(TeamModel::TABLE_NAME, &table);

    let raw_rows = engine
        .aggregate_query(&sql, params)
        .await
        .expect("aggregate_query for group_by");

    // Split raw rows into GroupByResult (same logic as GroupByOperation::exec).
    let group_columns = ["team_id"];
    let results: Vec<GroupByResult> = raw_rows
        .into_iter()
        .map(|row| {
            let mut group_values: HashMap<String, serde_json::Value> = HashMap::new();
            let mut agg_map: HashMap<String, FilterValue> = HashMap::new();
            for (k, v) in row {
                if group_columns.contains(&k.as_str()) {
                    let json_val = match &v {
                        FilterValue::Int(n) => serde_json::Value::from(*n),
                        FilterValue::Float(f) => serde_json::json!(*f),
                        FilterValue::String(s) => serde_json::Value::String(s.clone()),
                        FilterValue::Bool(b) => serde_json::Value::Bool(*b),
                        _ => serde_json::Value::Null,
                    };
                    group_values.insert(k, json_val);
                } else {
                    agg_map.insert(k, v);
                }
            }
            GroupByResult {
                group_values,
                aggregates: AggregateResult::from_row(agg_map),
            }
        })
        .collect();

    assert_eq!(
        results.len(),
        1,
        "HAVING COUNT(*) > 3 should return exactly one group"
    );

    let team_id = results[0]
        .group_values
        .get("team_id")
        .and_then(serde_json::Value::as_i64)
        .expect("team_id should be present as integer");
    assert_eq!(team_id, 2, "the surviving group should be team 2");

    let count = results[0]
        .aggregates
        .count
        .expect("COUNT(*) should be present in aggregates");
    assert_eq!(count, 4, "team 2 has 4 rows");

    drop_table(&pool, &table).await;
}

// =============================================================================
// Test 4 — COUNT(DISTINCT col) round-trip
//
// 5 rows with regions 'a','a','b','b','c'.
// count_column("region") == 5 (non-NULL), count_distinct("region") == 3.
// =============================================================================

#[tokio::test]
#[ignore = "requires running PostgreSQL via docker-compose (PRAX_E2E=1 + POSTGRES_URL)"]
async fn distinct_count_round_trip() {
    if skip_unless_e2e().is_none() {
        eprintln!("skipping: PRAX_E2E not set");
        return;
    }
    let pool = pool().await;
    let table = unique_table("region");
    drop_table(&pool, &table).await;

    {
        let conn = pool.get().await.expect("conn");
        conn.batch_execute(&format!(
            "CREATE TABLE {table} (id SERIAL PRIMARY KEY, region TEXT NOT NULL)"
        ))
        .await
        .expect("create table");

        conn.batch_execute(&format!(
            "INSERT INTO {table} (region) VALUES ('a'), ('a'), ('b'), ('b'), ('c')"
        ))
        .await
        .expect("insert rows");
    }

    let engine = PgEngine::new(pool.clone());
    let dialect = engine.dialect();

    let op: AggregateOperation<RegionModel, PgEngine> = AggregateOperation::new()
        .count_column("region")
        .count_distinct("region");
    let (sql, params) = op.build_sql(dialect);
    let sql = sql.replace(RegionModel::TABLE_NAME, &table);

    let mut rows = engine
        .aggregate_query(&sql, params)
        .await
        .expect("aggregate_query");

    let result = AggregateResult::from_row(rows.pop().unwrap_or_default());

    assert_eq!(
        result.count_of("region"),
        Some(5),
        "COUNT(region) should be 5 (5 non-NULL rows)"
    );
    assert_eq!(
        result.count_distinct_of("region"),
        Some(3),
        "COUNT(DISTINCT region) should be 3 (a, b, c)"
    );

    drop_table(&pool, &table).await;
}

// =============================================================================
// Test 5 — GROUP BY + ORDER BY round-trip
//
// team 1 → sum(views)=100, team 2 → sum(views)=300.
// ORDER BY _sum_views DESC → team 2 must be first.
// =============================================================================

#[tokio::test]
#[ignore = "requires running PostgreSQL via docker-compose (PRAX_E2E=1 + POSTGRES_URL)"]
async fn group_by_order_by_round_trip() {
    if skip_unless_e2e().is_none() {
        eprintln!("skipping: PRAX_E2E not set");
        return;
    }
    let pool = pool().await;
    let table = unique_table("views");
    drop_table(&pool, &table).await;

    {
        let conn = pool.get().await.expect("conn");
        conn.batch_execute(&format!(
            "CREATE TABLE {table} (id SERIAL PRIMARY KEY, team_id INT NOT NULL, views INT NOT NULL)"
        ))
        .await
        .expect("create table");

        // team 1 → views 40+60=100, team 2 → views 100+200=300
        conn.batch_execute(&format!(
            "INSERT INTO {table} (team_id, views) VALUES \
             (1, 40), (1, 60), \
             (2, 100), (2, 200)"
        ))
        .await
        .expect("insert rows");
    }

    let engine = PgEngine::new(pool.clone());
    let dialect = engine.dialect();

    let op: GroupByOperation<ViewsModel, PgEngine> =
        GroupByOperation::new(vec!["team_id".to_string()])
            .sum("views")
            .order_by(OrderByField::desc("_sum_views"));
    let (sql, params) = op.build_sql(dialect);
    let sql = sql.replace(ViewsModel::TABLE_NAME, &table);

    let raw_rows = engine
        .aggregate_query(&sql, params)
        .await
        .expect("aggregate_query for group_by order_by");

    let group_columns = ["team_id"];
    let results: Vec<GroupByResult> = raw_rows
        .into_iter()
        .map(|row| {
            let mut group_values: HashMap<String, serde_json::Value> = HashMap::new();
            let mut agg_map: HashMap<String, FilterValue> = HashMap::new();
            for (k, v) in row {
                if group_columns.contains(&k.as_str()) {
                    let json_val = match &v {
                        FilterValue::Int(n) => serde_json::Value::from(*n),
                        FilterValue::Float(f) => serde_json::json!(*f),
                        FilterValue::String(s) => serde_json::Value::String(s.clone()),
                        FilterValue::Bool(b) => serde_json::Value::Bool(*b),
                        _ => serde_json::Value::Null,
                    };
                    group_values.insert(k, json_val);
                } else {
                    agg_map.insert(k, v);
                }
            }
            GroupByResult {
                group_values,
                aggregates: AggregateResult::from_row(agg_map),
            }
        })
        .collect();

    assert_eq!(results.len(), 2, "two groups expected (team 1 and team 2)");

    // With ORDER BY _sum_views DESC, team 2 (sum=300) must come first.
    let first_team_id = results[0]
        .group_values
        .get("team_id")
        .and_then(serde_json::Value::as_i64)
        .expect("team_id present in first group");
    assert_eq!(first_team_id, 2, "team 2 should be first (highest sum)");

    let first_sum = results[0]
        .aggregates
        .sum_as_f64("views")
        .expect("SUM(views) present in first group");
    assert!(
        (first_sum - 300.0).abs() < 0.001,
        "team 2 sum should be 300, got {first_sum}"
    );

    drop_table(&pool, &table).await;
}