drizzle 0.3.1

A type-safe SQL query builder for Rust
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
use crate::common::seed::{
    ConstantName, NameGenerator, Param, RelatedOptions, SeedContract, SimpleOptions, Statement,
};
use drizzle::postgres::prelude::*;
use drizzle_seed::{GeneratorKind, SeedConfig, SeedError};

#[PostgresTable(NAME = "seed_simple")]
struct ContractSimple {
    #[column(PRIMARY)]
    id: i32,
    name: String,
}

#[PostgresTable(NAME = "seed_parent")]
struct ContractParent {
    #[column(PRIMARY)]
    id: i32,
    name: String,
}

#[PostgresTable(NAME = "seed_child")]
struct ContractChild {
    #[column(PRIMARY)]
    id: i32,
    #[column(REFERENCES = ContractParent::id)]
    parent_id: i32,
    value: String,
}

#[PostgresTable(NAME = "seed_profile")]
struct ContractProfile {
    #[column(PRIMARY)]
    id: i32,
    email: String,
    name: String,
    description: String,
}

#[PostgresTable(NAME = "seed_self_reference")]
struct ContractSelfReference {
    #[column(PRIMARY)]
    id: i32,
    #[column(REFERENCES = ContractSelfReference::id)]
    parent_id: Option<i32>,
}

#[PostgresTable(SCHEMA = "seed_a", NAME = "duplicate")]
struct QualifiedA {
    #[column(PRIMARY)]
    id: i32,
}

#[PostgresTable(SCHEMA = "seed_b", NAME = "duplicate")]
struct QualifiedB {
    #[column(PRIMARY)]
    id: i32,
}

#[derive(PostgresSchema)]
struct ContractSimpleSchema {
    simple: ContractSimple,
}

#[derive(PostgresSchema)]
struct ContractRelatedSchema {
    parent: ContractParent,
    child: ContractChild,
}

#[derive(PostgresSchema)]
struct ContractProfileSchema {
    profile: ContractProfile,
}

#[derive(PostgresSchema)]
struct ContractAllSchema {
    simple: ContractSimple,
    parent: ContractParent,
    child: ContractChild,
    profile: ContractProfile,
}

#[derive(PostgresSchema)]
struct ContractSelfReferenceSchema {
    nodes: ContractSelfReference,
}

#[derive(PostgresSchema)]
struct QualifiedSchema {
    first: QualifiedA,
    second: QualifiedB,
}

struct PostgresSeedContract;

impl SeedContract for PostgresSeedContract {
    fn simple(options: SimpleOptions) -> Vec<Statement> {
        let schema = ContractSimpleSchema::new();
        let mut config = SeedConfig::postgres(&schema).seed(options.seed);
        if let Some(count) = options.count {
            config = config.count(&schema.simple, count);
        }
        if let Some(count) = options.default_count {
            config = config.default_count(count);
        }
        if let Some(max_params) = options.max_params {
            config = config.max_params(max_params);
        }
        config = match options.name_generator {
            NameGenerator::Inferred => config,
            NameGenerator::Email => config.kind(&ContractSimple::name, GeneratorKind::Email),
            NameGenerator::Constant => config.generator(&ContractSimple::name, ConstantName),
            NameGenerator::Column => config.generator(&ContractSimple::name, &ContractSimple::name),
        };
        config.generate().into_iter().map(normalize).collect()
    }

    fn related(options: RelatedOptions) -> Vec<Statement> {
        let schema = ContractRelatedSchema::new();
        let mut config = SeedConfig::postgres(&schema).seed(options.seed);
        if let Some(count) = options.parent_count {
            config = config.count(&schema.parent, count);
        }
        if let Some(count) = options.child_count {
            config = config.count(&schema.child, count);
        }
        if let Some(count) = options.children_per_parent {
            config = config.relation(&schema.parent, &schema.child, count);
        }
        if options.skip_parent {
            config = config.skip(&schema.parent);
        }
        if options.skip_child {
            config = config.skip(&schema.child);
        }
        config.generate().into_iter().map(normalize).collect()
    }

    fn reset_related() -> Vec<String> {
        let schema = ContractRelatedSchema::new();
        SeedConfig::postgres(&schema)
            .reset_plan()
            .unwrap()
            .into_iter()
            .map(|statement| statement.sql())
            .collect()
    }

    fn reset_self_referential() -> Vec<String> {
        let schema = ContractSelfReferenceSchema::new();
        SeedConfig::postgres(&schema)
            .reset_plan()
            .unwrap()
            .into_iter()
            .map(|statement| statement.sql())
            .collect()
    }

    fn parameter_limit_error() -> SeedError {
        let schema = ContractSimpleSchema::new();
        SeedConfig::postgres(&schema)
            .count(&schema.simple, 1)
            .max_params(1)
            .try_generate()
            .unwrap_err()
    }

    fn unsafe_reset_error() -> SeedError {
        let schema = ContractRelatedSchema::new();
        SeedConfig::postgres(&schema)
            .skip(&schema.child)
            .reset_plan()
            .unwrap_err()
    }

    fn all_tables(seed: u64, count: usize) -> Vec<Statement> {
        let schema = ContractAllSchema::new();
        SeedConfig::postgres(&schema)
            .seed(seed)
            .default_count(count)
            .generate()
            .into_iter()
            .map(normalize)
            .collect()
    }

    fn profiles(seed: u64, count: usize) -> Vec<Statement> {
        let schema = ContractProfileSchema::new();
        SeedConfig::postgres(&schema)
            .seed(seed)
            .count(&schema.profile, count)
            .generate()
            .into_iter()
            .map(normalize)
            .collect()
    }
}

fn normalize(statement: drizzle_seed::PostgresSeedStatement) -> Statement {
    let (sql, params) = statement.build();
    Statement {
        sql,
        params: params
            .into_iter()
            .map(|param| match param {
                drizzle::postgres::values::OwnedPostgresValue::Smallint(value) => {
                    Param::Integer(i128::from(value))
                }
                drizzle::postgres::values::OwnedPostgresValue::Integer(value) => {
                    Param::Integer(i128::from(value))
                }
                drizzle::postgres::values::OwnedPostgresValue::Bigint(value) => {
                    Param::Integer(i128::from(value))
                }
                drizzle::postgres::values::OwnedPostgresValue::Text(value) => Param::Text(value),
                other => Param::Other(format!("{other:?}")),
            })
            .collect(),
    }
}

crate::common::seed::seed_contract_tests!(PostgresSeedContract);

#[test]
fn schema_qualified_tables_with_the_same_name_keep_distinct_counts() {
    let schema = QualifiedSchema::new();
    let statements = SeedConfig::postgres(&schema)
        .count(&schema.first, 1)
        .count(&schema.second, 2)
        .generate();

    assert_eq!(statements.len(), 2);
    assert!(
        statements
            .iter()
            .any(|statement| statement.sql().contains("seed_a"))
    );
    assert!(
        statements
            .iter()
            .any(|statement| statement.sql().contains("seed_b"))
    );

    let mut param_counts = statements
        .iter()
        .map(|statement| statement.build().1.len())
        .collect::<Vec<_>>();
    param_counts.sort_unstable();
    assert_eq!(param_counts, vec![1, 2]);
}

/// Seeds the column shapes that used to fail on a real server and reads the
/// rows back through the typed models.
#[cfg(all(feature = "uuid", feature = "serde", feature = "chrono"))]
mod executed {
    use drizzle::postgres::prelude::*;
    use drizzle_seed::SeedConfig;

    #[derive(Debug, Clone, Copy, PartialEq, Eq, Default, PostgresEnum)]
    pub enum SeedMood {
        #[default]
        Calm,
        Busy,
    }

    #[derive(Debug, Clone, Copy, PartialEq, Eq, Default, PostgresEnum)]
    #[repr(i64)]
    pub enum SeedLevel {
        #[default]
        Low = 1,
        High = 5,
    }

    #[PostgresTable(NAME = "seed_typed")]
    pub struct SeedTyped {
        #[column(PRIMARY, identity(always))]
        pub id: i32,
        #[column(UNIQUE, VARCHAR(12))]
        pub username: String,
        pub external_id: uuid::Uuid,
        #[column(JSONB)]
        pub settings: serde_json::Value,
        #[column(ENUM)]
        pub mood: SeedMood,
        #[column(ENUM)]
        pub level: SeedLevel,
        pub updated_at: chrono::NaiveDateTime,
        pub position: i32,
        pub email_count: i32,
        pub tags: Vec<String>,
        pub score: f32,
    }

    #[PostgresTable(NAME = "seed_typed_children")]
    pub struct SeedTypedChild {
        #[column(PRIMARY, SERIAL)]
        pub id: i32,
        #[column(REFERENCES = SeedTyped::id)]
        pub parent_id: i32,
        pub label: String,
    }

    #[derive(PostgresSchema)]
    pub struct SeedTypedSchema {
        pub mood: SeedMood,
        pub typed: SeedTyped,
        pub child: SeedTypedChild,
    }

    #[drizzle::test]
    fn seeded_rows_insert_and_decode(db: &mut TestDb<SeedTypedSchema>) {
        let SeedTypedSchema { typed, child, .. } = schema;
        for statement in SeedConfig::postgres(&schema)
            .seed(3)
            .count(&typed, 40)
            .relation(&typed, &child, 2)
            .generate()
        {
            db.execute(statement);
        }

        let rows: Vec<SelectSeedTyped> = db.select(()).from(typed).all();
        assert_eq!(rows.len(), 40);
        let mut usernames: Vec<&str> = rows.iter().map(|row| row.username.as_str()).collect();
        assert!(usernames.iter().all(|name| name.chars().count() <= 12));
        usernames.sort_unstable();
        usernames.dedup();
        assert_eq!(usernames.len(), 40, "UNIQUE username values repeat");

        let children: Vec<SelectSeedTypedChild> = db.select(()).from(child).all();
        assert_eq!(children.len(), 80);

        // The SERIAL sequence was moved past the seeded ids.
        db.insert(child)
            .values([InsertSeedTypedChild::new(rows[0].id, "after seed")])
            .execute();
        let children: Vec<SelectSeedTypedChild> = db.select(()).from(child).all();
        assert_eq!(children.len(), 81);
    }

    /// `SeedTypedSchema`, described at runtime instead of with the macros.
    fn runtime_schema() -> drizzle_seed::schema::Schema {
        use drizzle_seed::schema::{Column, Schema, Table};
        Schema::postgres()
            .table(
                Table::new("seed_typed")
                    .column(Column::new("id", "INTEGER").primary_key().identity_always())
                    .column(Column::new("username", "VARCHAR(12)").not_null().unique())
                    .column(Column::new("external_id", "UUID").not_null())
                    .column(Column::new("settings", "JSONB").not_null())
                    .column(
                        // Quoted, as the macro creates the type.
                        Column::new("mood", "\"SeedMood\"")
                            .not_null()
                            .enum_values(["Calm", "Busy"]),
                    )
                    .column(Column::new("level", "integer").not_null())
                    .column(Column::new("updated_at", "TIMESTAMP").not_null())
                    .column(Column::new("position", "INTEGER").not_null())
                    .column(Column::new("email_count", "INTEGER").not_null())
                    .column(Column::new("tags", "TEXT[]").not_null())
                    .column(Column::new("score", "REAL").not_null()),
            )
            .table(
                Table::new("seed_typed_children")
                    .column(Column::new("id", "SERIAL").primary_key())
                    .column(
                        Column::new("parent_id", "INTEGER")
                            .not_null()
                            .references("seed_typed", "id"),
                    )
                    .column(Column::new("label", "TEXT").not_null()),
            )
    }

    #[test]
    fn runtime_schema_and_names_match_the_macro_schema() {
        use drizzle_seed::generators;
        type Built = Vec<(String, Vec<drizzle::postgres::values::OwnedPostgresValue>)>;

        let schema = SeedTypedSchema::new();
        let typed: Built = SeedConfig::postgres(&schema)
            .seed(3)
            .count(&schema.typed, 20)
            .relation(&schema.typed, &schema.child, 2)
            .generator(&schema.typed.level, generators::one_of([1, 5]))
            .generate()
            .iter()
            .map(|statement| statement.build())
            .collect();

        let runtime = runtime_schema();
        let named: Built = SeedConfig::postgres(&runtime)
            .seed(3)
            .count_by_name("seed_typed", 20)
            .relation_by_name("public.seed_typed", "seed_typed_children", 2)
            .generator_by_name("seed_typed", "level", generators::one_of([1, 5]))
            .generate()
            .iter()
            .map(|statement| statement.build())
            .collect();

        assert!(!typed.is_empty());
        assert_eq!(typed, named);
    }

    #[drizzle::test]
    fn runtime_schema_rows_insert_and_decode(db: &mut TestDb<SeedTypedSchema>) {
        let SeedTypedSchema { typed, child, .. } = schema;
        let runtime = runtime_schema();
        for statement in SeedConfig::postgres(&runtime)
            .seed(8)
            .count_by_name("seed_typed", 25)
            .relation_by_name("seed_typed", "seed_typed_children", 3)
            .generator_by_name(
                "seed_typed",
                "level",
                drizzle_seed::generators::one_of([1, 5]),
            )
            .generate()
        {
            db.execute(statement);
        }

        let rows: Vec<SelectSeedTyped> = db.select(()).from(typed).all();
        assert_eq!(rows.len(), 25);
        let children: Vec<SelectSeedTypedChild> = db.select(()).from(child).all();
        assert_eq!(children.len(), 75);

        // The SERIAL sequence was moved past the seeded ids.
        db.insert(child)
            .values([InsertSeedTypedChild::new(rows[0].id, "after seed")])
            .execute();
    }

    const LIVE_TABLES: [&str; 4] = [
        r#"CREATE TYPE "LiveMood" AS ENUM ('calm', 'busy')"#,
        r#"CREATE TABLE live_accounts (
            id serial PRIMARY KEY,
            external_id uuid NOT NULL UNIQUE,
            handle varchar(16) NOT NULL UNIQUE,
            plan varchar(8) NOT NULL CHECK (plan IN ('free', 'pro')),
            mood "LiveMood" NOT NULL,
            settings jsonb NOT NULL,
            tags text[] NOT NULL,
            balance numeric(10, 2) NOT NULL,
            ratio real,
            avatar bytea,
            is_active boolean NOT NULL,
            born_on date,
            wakes_at time,
            created_at timestamptz NOT NULL DEFAULT now(),
            updated_at timestamp NOT NULL,
            handle_length integer GENERATED ALWAYS AS (length(handle)) STORED
        )"#,
        r"CREATE TABLE live_events (
            id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
            account_id integer NOT NULL REFERENCES live_accounts (id),
            kind text NOT NULL,
            happened_at timestamp NOT NULL
        )",
        r"CREATE UNIQUE INDEX live_events_account_kind ON live_events (account_id, kind)",
    ];

    #[derive(Debug, PostgresFromRow)]
    struct Text(String);

    /// Every row of `table`, as one comparable string, without
    /// `created_at`, whose `DEFAULT now()` differs between two runs.
    fn fingerprint_sql(table: &str) -> String {
        format!(
            "SELECT string_agg((to_jsonb(t) - 'created_at')::text, '|' ORDER BY (to_jsonb(t) - 'created_at')::text) FROM {table} t"
        )
    }

    /// A database not described in Rust at all: introspect it, seed it, and
    /// check the inline script inserts exactly what the bound statements do.
    #[drizzle::test]
    fn introspected_database_seeds_with_bound_and_inline_values(db: &mut TestDb<SeedTypedSchema>) {
        use drizzle_seed::schema::Schema;

        for statement in LIVE_TABLES {
            db.execute(SQL::raw(statement));
        }
        let Text(namespace) = result!(db.get(SQL::raw("SELECT current_schema()::text"))).unwrap();
        let snapshot = result!(db.introspect_schemas(&[namespace.as_str()])).expect("introspect");
        let schema = Schema::from_snapshot(&snapshot)
            .unwrap()
            .retain(|table| table.name().starts_with("live_"));
        assert_eq!(schema.tables().len(), 2);

        let config = SeedConfig::postgres(&schema)
            .seed(11)
            .count_by_name("live_accounts", 15)
            .relation_by_name("live_accounts", "live_events", 2);
        let tables = ["live_accounts", "live_events"];

        for statement in config.generate() {
            db.execute(statement);
        }
        let mut bound = Vec::new();
        for table in tables {
            let Text(rows) = result!(db.get(SQL::raw(fingerprint_sql(table)))).unwrap();
            bound.push(rows);
        }
        let Text(plans) = result!(db.get(SQL::raw(
            "SELECT string_agg(DISTINCT plan, ',') FROM live_accounts",
        )))
        .unwrap();
        assert!(
            plans.split(',').all(|plan| plan == "free" || plan == "pro"),
            "{plans}"
        );

        for statement in config.reset_plan().unwrap() {
            db.execute(statement);
        }
        for statement in config.generate() {
            db.execute(SQL::raw(statement.inline_sql().unwrap()));
        }
        let mut inline = Vec::new();
        for table in tables {
            let Text(rows) = result!(db.get(SQL::raw(fingerprint_sql(table)))).unwrap();
            inline.push(rows);
        }
        assert_eq!(inline, bound);

        // The serial and identity sequences were moved past the seeded ids.
        db.execute(SQL::raw(
            "INSERT INTO live_accounts (external_id, handle, plan, mood, settings, tags, balance, is_active, updated_at) \
             VALUES (gen_random_uuid(), 'after', 'free', 'calm', '{}', '{}', 1, true, now())",
        ));
        db.execute(SQL::raw(
            "INSERT INTO live_events (account_id, kind, happened_at) VALUES (1, 'after seed', now())",
        ));
    }
}