rustlavel-db 0.2.2

Rustlavel database layer: PostgreSQL driver, query builder, migrations, and ORM
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
//! Does the SQL we generate actually work?
//!
//! The dialect tests assert what the strings look like. These run those exact
//! strings against a real server, which is a different question — and the only
//! one that matters. Every check here exists because a live database rejected
//! something the unit tests were perfectly happy with:
//!
//! - MySQL refuses an `auto_increment` column that is not a key.
//! - SQL Server's `OUTPUT` goes after the column list, not before it.
//! - SQL Server rejects `add column` while requiring `drop column`.
//! - MySQL parses an inline `references` clause and silently creates no
//!   foreign key at all, then accepts orphan rows.
//!
//! Set any of `DATABASE_URL`, `MYSQL_URL` or `SQLSERVER_URL` and the matching
//! database is checked; unset ones are skipped with a note.

use rustlavel_db::prelude::*;
use rustlavel_db::{Migration, Migrator, migration};

/// Every configured database, with a prefix so two of them can share a server.
async fn databases() -> Vec<(String, Database)> {
    let mut out = Vec::new();

    for variable in ["DATABASE_URL", "MYSQL_URL", "SQLSERVER_URL"] {
        let Ok(url) = std::env::var(variable) else {
            eprintln!("skipped {variable}: not set");
            continue;
        };
        if url.is_empty() {
            continue;
        }

        match Database::connect(&url).await {
            Ok(db) => out.push((db.dialect().name().to_string(), db)),
            Err(e) => panic!("{variable} is set but connecting failed: {e}"),
        }
    }

    if out.is_empty() {
        eprintln!("skipped: set DATABASE_URL, MYSQL_URL or SQLSERVER_URL to run the conformance tests");
    }
    out
}

/// A table name nothing else in the suite uses.
fn table(test: &str, dialect: &str) -> String {
    format!("conf_{test}_{dialect}")
}

async fn drop_if_present(db: &Database, name: &str) {
    // Straight through the dialect, so this is itself a check that dropping works.
    let sql = db.dialect().drop_table_sql(name);
    db.run(&sql).await.ok();
}

#[tokio::test]
async fn the_generated_create_table_is_accepted_and_round_trips() {
    for (dialect, db) in databases().await {
        let name = table("create", &dialect);
        drop_if_present(&db, &name).await;

        Schema::new(&db)
            .create(&name, |t| {
                t.id();
                t.string("email").unique();
                t.text("bio").nullable();
                t.integer("visits").default_int(0);
                t.boolean("active").default_bool(true);
                t.decimal("balance", 12, 2).default_int(0);
                t.timestamps();
            })
            .await
            .unwrap_or_else(|e| panic!("{dialect}: create failed: {e}"));

        db.table(&name)
            .insert_without_id(&db, &[("email", Value::from("ada@example.com"))])
            .await
            .unwrap_or_else(|e| panic!("{dialect}: insert failed: {e}"));

        let rows = db.table(&name).get(&db).await.unwrap();
        assert_eq!(rows.len(), 1, "{dialect}");
        assert_eq!(rows[0].get::<String>("email").unwrap(), "ada@example.com", "{dialect}");

        // The defaults have to have been applied by the database, not by us.
        assert_eq!(rows[0].get::<i64>("visits").unwrap(), 0, "{dialect}");
        assert!(!rows[0].value("created_at").unwrap().is_null(), "{dialect}: no timestamp");

        drop_if_present(&db, &name).await;
    }
}

#[tokio::test]
async fn a_unique_constraint_is_real() {
    for (dialect, db) in databases().await {
        let name = table("unique", &dialect);
        drop_if_present(&db, &name).await;

        Schema::new(&db)
            .create(&name, |t| {
                t.id();
                t.string("email").unique();
            })
            .await
            .unwrap();

        let insert = |value: &'static str| {
            let db = db.clone();
            let name = name.clone();
            async move {
                db.table(&name).insert_without_id(&db, &[("email", Value::from(value))]).await
            }
        };

        insert("taken@example.com").await.unwrap();
        assert!(
            insert("taken@example.com").await.is_err(),
            "{dialect}: a duplicate was accepted, so the constraint is not there"
        );

        drop_if_present(&db, &name).await;
    }
}

#[tokio::test]
async fn a_foreign_key_is_enforced_rather_than_merely_written() {
    // The one that matters most: MySQL parses an inline `references` clause,
    // creates nothing, and accepts orphans. Nothing but a real insert catches it.
    for (dialect, db) in databases().await {
        let children = table("fk_children", &dialect);
        let parents = table("fk_parent", &dialect);

        drop_if_present(&db, &children).await;
        drop_if_present(&db, &parents).await;

        let schema = Schema::new(&db);
        schema
            .create(&parents, |t| {
                t.id();
                t.string("name");
            })
            .await
            .unwrap();

        // `foreign_id` names the table by pluralising, so the column is built
        // by hand here to point at this test's own parent table.
        schema
            .create(&children, |t| {
                t.id();
                t.big_integer("parent_id").references(&parents, "id").cascade_on_delete();
            })
            .await
            .unwrap_or_else(|e| panic!("{dialect}: create failed: {e}"));

        let orphan = db
            .table(&children)
            .insert_without_id(&db, &[("parent_id", Value::from(999_999i64))])
            .await;

        assert!(
            orphan.is_err(),
            "{dialect}: a row pointing at a parent that does not exist was accepted — \
             the foreign key was written but not created"
        );

        drop_if_present(&db, &children).await;
        drop_if_present(&db, &parents).await;
    }
}

#[tokio::test]
async fn a_generated_key_comes_back_from_an_insert() {
    // Three different mechanisms: RETURNING, OUTPUT, and last_insert_id.
    for (dialect, db) in databases().await {
        let name = table("keys", &dialect);
        drop_if_present(&db, &name).await;

        Schema::new(&db)
            .create(&name, |t| {
                t.id();
                t.string("name");
            })
            .await
            .unwrap();

        let first =
            db.table(&name).insert(&db, &[("name", Value::from("one"))]).await.unwrap_or_else(
                |e| panic!("{dialect}: insert did not return a key: {e}"),
            );
        let second =
            db.table(&name).insert(&db, &[("name", Value::from("two"))]).await.unwrap();

        assert!(first > 0, "{dialect}: the key was {first}");
        assert!(second > first, "{dialect}: keys did not advance ({first} then {second})");

        drop_if_present(&db, &name).await;
    }
}

#[tokio::test]
async fn a_column_can_be_added_and_dropped() {
    for (dialect, db) in databases().await {
        let name = table("alter", &dialect);
        drop_if_present(&db, &name).await;

        let schema = Schema::new(&db);
        schema
            .create(&name, |t| {
                t.id();
                t.string("keep");
            })
            .await
            .unwrap();

        schema
            .alter(&name, |t| {
                t.string("nickname").nullable();
            })
            .await
            .unwrap_or_else(|e| panic!("{dialect}: adding a column failed: {e}"));
        assert!(schema.has_column(&name, "nickname").await.unwrap(), "{dialect}");

        schema
            .alter(&name, |t| {
                t.drop_column("nickname");
            })
            .await
            .unwrap_or_else(|e| panic!("{dialect}: dropping a column failed: {e}"));
        assert!(!schema.has_column(&name, "nickname").await.unwrap(), "{dialect}");

        drop_if_present(&db, &name).await;
    }
}

#[tokio::test]
async fn paging_returns_the_right_slice() {
    for (dialect, db) in databases().await {
        let name = table("paging", &dialect);
        drop_if_present(&db, &name).await;

        Schema::new(&db)
            .create(&name, |t| {
                t.id();
                t.integer("position");
            })
            .await
            .unwrap();

        for position in 1..=25 {
            db.table(&name)
                .insert_without_id(&db, &[("position", Value::from(position))])
                .await
                .unwrap();
        }

        let second = db
            .table(&name)
            .order_by("position", rustlavel_db::Direction::Asc)
            .page(2, 10)
            .get(&db)
            .await
            .unwrap_or_else(|e| panic!("{dialect}: paging failed: {e}"));

        assert_eq!(second.len(), 10, "{dialect}");
        assert_eq!(second[0].get::<i64>("position").unwrap(), 11, "{dialect}");

        // SQL Server cannot page without an ordering; the builder supplies one,
        // so this must work too.
        let unordered = db.table(&name).limit(5).get(&db).await.unwrap_or_else(|e| {
            panic!("{dialect}: paging without an order by failed: {e}")
        });
        assert_eq!(unordered.len(), 5, "{dialect}");

        drop_if_present(&db, &name).await;
    }
}

migration!(
    ConformanceMigration,
    "2026_08_29_000099_conformance_check",
    up: |schema| {
        schema
            .create("conf_migrated", |t| {
                t.id();
                t.string("name");
                t.timestamps();
            })
            .await
    },
    down: |schema| { schema.drop("conf_migrated").await },
);

#[tokio::test]
async fn migrations_run_and_roll_back_on_every_database() {
    for (dialect, db) in databases().await {
        let tracking = format!("conf_migrations_{dialect}");

        // Both tables go first. Leaving the tracking table behind would make a
        // second run of this test see the migration as already applied, and
        // then find nothing to roll back.
        drop_if_present(&db, "conf_migrated").await;
        drop_if_present(&db, &tracking).await;

        // Its own tracking table, so this cannot disturb another suite sharing
        // the same server.
        let migrations: Vec<&dyn Migration> = vec![&ConformanceMigration];
        let migrator = Migrator::new(&db, migrations).with_table(&tracking).unwrap();

        // The tracking table's own DDL is the first thing that has to be valid.
        migrator.prepare().await.unwrap_or_else(|e| {
            panic!("{dialect}: the migrations table could not be created: {e}")
        });

        let applied = migrator.run().await.unwrap_or_else(|e| panic!("{dialect}: migrate: {e}"));
        assert_eq!(applied.applied.len(), 1, "{dialect}");
        assert!(Schema::new(&db).has_table("conf_migrated").await.unwrap(), "{dialect}");

        // Running again must do nothing, which is what makes a deploy repeatable.
        assert!(migrator.run().await.unwrap().applied.is_empty(), "{dialect}");

        let rolled = migrator.rollback().await.unwrap_or_else(|e| panic!("{dialect}: rollback: {e}"));
        assert_eq!(rolled.rolled_back.len(), 1, "{dialect}");
        assert!(!Schema::new(&db).has_table("conf_migrated").await.unwrap(), "{dialect}");

        drop_if_present(&db, &tracking).await;
    }
}

#[tokio::test]
async fn a_hostile_value_stays_a_value_on_every_database() {
    for (dialect, db) in databases().await {
        let name = table("injection", &dialect);
        drop_if_present(&db, &name).await;

        Schema::new(&db)
            .create(&name, |t| {
                t.id();
                t.string("note");
            })
            .await
            .unwrap();

        let hostile = format!("'); drop table {name}; --");
        db.table(&name)
            .insert_without_id(&db, &[("note", Value::from(hostile.as_str()))])
            .await
            .unwrap();

        // The table is still there, and holds the string verbatim.
        let rows = db.table(&name).get(&db).await.unwrap();
        assert_eq!(rows.len(), 1, "{dialect}");
        assert_eq!(rows[0].get::<String>("note").unwrap(), hostile, "{dialect}");

        drop_if_present(&db, &name).await;
    }
}

#[tokio::test]
async fn a_transaction_commits_and_rolls_back_on_every_database() {
    // `begin` is a syntax error in T-SQL, so this went untested for exactly as
    // long as it was broken.
    for (dialect, db) in databases().await {
        let name = table("tx", &dialect);
        drop_if_present(&db, &name).await;

        Schema::new(&db)
            .create(&name, |t| {
                t.id();
                t.string("note");
            })
            .await
            .unwrap();

        let insert = format!(
            "insert into {} ({}) values ({})",
            db.dialect().quote(&name),
            db.dialect().quote("note"),
            db.dialect().placeholder(1)
        );

        // Rolled back: nothing is written.
        let mut tx = db.begin().await.unwrap_or_else(|e| panic!("{dialect}: begin failed: {e}"));
        tx.execute(&insert, &[Value::from("discarded")]).await.unwrap();
        tx.rollback().await.unwrap_or_else(|e| panic!("{dialect}: rollback failed: {e}"));
        assert_eq!(db.table(&name).count(&db).await.unwrap(), 0, "{dialect}");

        // Committed: it is.
        let mut tx = db.begin().await.unwrap();
        tx.execute(&insert, &[Value::from("kept")]).await.unwrap();
        tx.commit().await.unwrap_or_else(|e| panic!("{dialect}: commit failed: {e}"));
        assert_eq!(db.table(&name).count(&db).await.unwrap(), 1, "{dialect}");

        drop_if_present(&db, &name).await;
    }
}

#[tokio::test]
async fn a_savepoint_undoes_only_part_of_a_transaction_everywhere() {
    for (dialect, db) in databases().await {
        let name = table("savepoint", &dialect);
        drop_if_present(&db, &name).await;

        Schema::new(&db)
            .create(&name, |t| {
                t.id();
                t.string("note");
            })
            .await
            .unwrap();

        let insert = format!(
            "insert into {} ({}) values ({})",
            db.dialect().quote(&name),
            db.dialect().quote("note"),
            db.dialect().placeholder(1)
        );

        let mut tx = db.begin().await.unwrap();
        tx.execute(&insert, &[Value::from("keep")]).await.unwrap();
        tx.savepoint("sp1").await.unwrap_or_else(|e| panic!("{dialect}: savepoint failed: {e}"));
        tx.execute(&insert, &[Value::from("discard")]).await.unwrap();
        tx.rollback_to("sp1")
            .await
            .unwrap_or_else(|e| panic!("{dialect}: rollback to savepoint failed: {e}"));
        tx.commit().await.unwrap();

        let rows = db.table(&name).get(&db).await.unwrap();
        assert_eq!(rows.len(), 1, "{dialect}");
        assert_eq!(rows[0].get::<String>("note").unwrap(), "keep", "{dialect}");

        drop_if_present(&db, &name).await;
    }
}