rok-db-codegen 0.4.0

Generate a rok-db crate (models, typed queries and migrations) from .sql files
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
//! Turn the `.sql` files into the generated crate's files.

use std::collections::{BTreeMap, BTreeSet};
use std::fs;
use std::path::{Path, PathBuf};

use crate::ddl::{self, Rename};
use crate::describe::{self, Cache};
use crate::emit;
use crate::ir::Schema;
use crate::parse::{self, SourceFile};
use crate::queries;
use crate::types::Feature;
use crate::{Error, naming};

/// Where rok-db comes from in the generated `Cargo.toml`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Dependency {
    /// `rok-db = { version = "..." }`.
    Version(String),
    /// `rok-db = { path = "..." }`, relative to the project root.
    Path(PathBuf),
}

/// What to generate, and how.
#[derive(Debug, Clone)]
pub struct Options {
    /// Directory holding the `.sql` files.
    pub input: PathBuf,
    /// Directory of the generated crate.
    pub output: PathBuf,
    /// Package name of the generated crate.
    pub crate_name: String,
    /// The rok-db dependency of the generated crate.
    pub rok_db: Dependency,
    /// Treat warnings (queries that skip tenant or soft-delete filters) as errors.
    pub strict: bool,
    /// PostgreSQL server for checking queries that aren't in `queries.json`.
    pub database_url: Option<String>,
    /// Name for a new migration, when the schema changed.
    pub migration: Option<String>,
    /// Renames to apply instead of drop-and-add.
    pub renames: Vec<Rename>,
    /// Allow steps that can lose data or fail on existing rows.
    pub allow_destructive: bool,
}

impl Options {
    /// Defaults: `db/` to `db-gen/`, rok-db from crates.io.
    pub fn new() -> Self {
        Self {
            input: PathBuf::from("db"),
            output: PathBuf::from("db-gen"),
            crate_name: "db-gen".to_owned(),
            rok_db: Dependency::Version(default_version()),
            strict: false,
            database_url: None,
            migration: None,
            renames: Vec::new(),
            allow_destructive: false,
        }
    }
}

/// `0.4` for rok-db-codegen 0.4.x: the matching rok-db release.
fn default_version() -> String {
    env!("CARGO_PKG_VERSION")
        .splitn(3, '.')
        .take(2)
        .collect::<Vec<_>>()
        .join(".")
}

impl Default for Options {
    fn default() -> Self {
        Self::new()
    }
}

/// The generated files, relative to the output directory.
#[derive(Debug, Default)]
pub struct Generated {
    /// Files to write.
    pub files: BTreeMap<PathBuf, String>,
    /// Previously generated files that are no longer produced.
    pub stale: Vec<PathBuf>,
    /// The migration this run adds, if any.
    pub new_migration: Option<String>,
    /// Warnings to show.
    pub warnings: Vec<String>,
}

const HEADER: &str = "// @generated by rok-db-gen. Do not edit: change the .sql files and run `rok-db-gen generate`.";

/// Generate everything in memory. Nothing is written.
pub fn generate(options: &Options) -> Result<Generated, Error> {
    let sources = read_sources(&options.input)?;
    let parsed = parse::parse(&sources)?;
    let output = &options.output;
    let mut generated = Generated::default();

    // Migrations: compare with the snapshot of the latest one.
    let snapshot_path = output.join("schema.json");
    let snapshot: Schema = match fs::read_to_string(&snapshot_path) {
        Ok(text) => serde_json::from_str(&text)
            .map_err(|e| Error::new(format!("{}: {e}", snapshot_path.display())))?,
        Err(_) => Schema::default(),
    };
    let mut migrations = existing_migrations(&output.join("migrations"))?;
    if migrations.is_empty() && snapshot != Schema::default() {
        return Err(Error::new(format!(
            "{} exists but {} has no migrations; restore them or delete the snapshot",
            snapshot_path.display(),
            output.join("migrations").display()
        )));
    }
    let ops = ddl::diff(&snapshot, &parsed.schema, &options.renames).map_err(Error::new)?;
    let name = match (&options.migration, ops.is_empty()) {
        (Some(_), true) if options.renames.is_empty() => {
            return Err(Error::new(
                "the schema hasn't changed since the last migration; nothing to migrate",
            ));
        }
        (Some(name), _) => Some(naming::snake_ident(name)),
        (None, false) if migrations.is_empty() => Some("init".to_owned()),
        (None, false) => {
            let preview: Vec<String> = ops
                .iter()
                .take(5)
                .map(|op| format!("  {}", first_line(&op.up)))
                .collect();
            return Err(Error::new(format!(
                "the .sql files changed since the last migration:\n{}{}\nrun `rok-db-gen generate --migration <name>` to add a migration",
                preview.join("\n"),
                if ops.len() > 5 { "\n  ..." } else { "" }
            )));
        }
        (None, true) => None,
    };
    if let Some(name) = name {
        let reasons: Vec<&str> = ops
            .iter()
            .filter_map(|op| op.destructive.as_deref())
            .collect();
        if !reasons.is_empty() && !options.allow_destructive {
            return Err(Error::new(format!(
                "migration `{name}` would:\n  - {}\nre-run with --allow-destructive if that is intended, or use --rename for renames",
                reasons.join("\n  - ")
            )));
        }
        let version = migrations.last().map_or(1, |m| m.version + 1);
        let (up, down) = ddl::scripts(&ops);
        let stem = format!("{version:04}_{name}");
        generated.files.insert(
            PathBuf::from(format!("migrations/{stem}.up.sql")),
            up.clone(),
        );
        if !down.is_empty() {
            generated.files.insert(
                PathBuf::from(format!("migrations/{stem}.down.sql")),
                down.clone(),
            );
        } else {
            generated.warnings.push(format!(
                "migration `{stem}` can't be reverted automatically (it adds enum values or removes statements without down SQL)"
            ));
        }
        migrations.push(MigrationFile {
            version,
            name: name.clone(),
            stem,
            up,
            reversible: !down.is_empty(),
        });
        generated.new_migration = Some(name);
    }
    generated
        .files
        .insert(PathBuf::from("schema.json"), json(&parsed.schema)?);

    // Queries: types from PostgreSQL (or the cache).
    let cache_path = output.join("queries.json");
    let cache: Cache = match fs::read_to_string(&cache_path) {
        Ok(text) => serde_json::from_str(&text).unwrap_or_default(),
        Err(_) => Cache::default(),
    };
    let up_scripts: Vec<String> = migrations.iter().map(|m| m.up.clone()).collect();
    let (descriptions, cache) = describe::describe_all(
        &parsed.queries,
        &up_scripts,
        &cache,
        options.database_url.as_deref(),
    )?;
    generated
        .files
        .insert(PathBuf::from("queries.json"), json(&cache)?);

    // Rust.
    let mut features = BTreeSet::new();
    let mut modules = emit::models(&parsed.schema, &mut features)?;
    let mut warnings = Vec::new();
    let all_modules: BTreeSet<String> = parsed
        .modules
        .iter()
        .chain(modules.keys())
        .cloned()
        .collect();
    for module in &all_modules {
        let mine: Vec<_> = parsed
            .queries
            .iter()
            .zip(&descriptions)
            .filter(|(q, _)| &q.module == module)
            .collect();
        let code = queries::functions(&mine, &parsed.schema, &mut features, &mut warnings)?;
        modules.entry(module.clone()).or_default().push_str(&code);
    }
    if options.strict && !warnings.is_empty() {
        return Err(Error::new(format!("--strict: {}", warnings.join("\n"))));
    }
    generated.warnings.extend(warnings);

    let input_display = options.input.file_name().map_or_else(
        || options.input.display().to_string(),
        |n| n.to_string_lossy().into_owned(),
    );
    for (module, code) in &modules {
        let source = parsed_source(&sources, module);
        let doc = format!("//! Generated from `{source}`.\n\n{code}");
        generated.files.insert(
            PathBuf::from(format!("src/{module}.rs")),
            emit::format(&doc, HEADER)?,
        );
    }
    generated.files.insert(
        PathBuf::from("src/lib.rs"),
        lib_rs(&modules, &input_display)?,
    );
    generated.files.insert(
        PathBuf::from("src/migrations.rs"),
        migrations_rs(&migrations)?,
    );
    generated
        .files
        .insert(PathBuf::from("src/up.rs"), emit::format(UP_RS, HEADER)?);
    generated
        .files
        .insert(PathBuf::from("src/down.rs"), emit::format(DOWN_RS, HEADER)?);
    generated
        .files
        .insert(PathBuf::from("Cargo.toml"), cargo_toml(options, &features));

    // Generated sources that are no longer produced (a removed .sql file).
    if let Ok(entries) = fs::read_dir(output.join("src")) {
        for entry in entries.flatten() {
            let rel = PathBuf::from("src").join(entry.file_name());
            let is_generated = fs::read_to_string(entry.path())
                .is_ok_and(|t| t.starts_with("// @generated by rok-db-gen"));
            if is_generated && !generated.files.contains_key(&rel) {
                generated.stale.push(rel);
            }
        }
    }
    Ok(generated)
}

/// Write the files that changed and remove stale ones. Returns the paths
/// written.
pub fn write(options: &Options, generated: &Generated) -> Result<Vec<PathBuf>, Error> {
    let io = |path: &Path, e: std::io::Error| Error::new(format!("{}: {e}", path.display()));
    let mut written = Vec::new();
    for (rel, content) in &generated.files {
        let path = options.output.join(rel);
        if fs::read_to_string(&path).is_ok_and(|old| &old == content) {
            continue;
        }
        if let Some(dir) = path.parent() {
            fs::create_dir_all(dir).map_err(|e| io(dir, e))?;
        }
        fs::write(&path, content).map_err(|e| io(&path, e))?;
        written.push(rel.clone());
    }
    for rel in &generated.stale {
        let path = options.output.join(rel);
        fs::remove_file(&path).map_err(|e| io(&path, e))?;
        written.push(rel.clone());
    }
    Ok(written)
}

/// Files whose content on disk differs from `generated` (CI check).
pub fn differences(options: &Options, generated: &Generated) -> Vec<PathBuf> {
    let mut out: Vec<PathBuf> = generated
        .files
        .iter()
        .filter(|(rel, content)| {
            fs::read_to_string(options.output.join(rel)).map_or(true, |old| &old != *content)
        })
        .map(|(rel, _)| rel.clone())
        .collect();
    out.extend(generated.stale.iter().cloned());
    out
}

fn read_sources(dir: &Path) -> Result<Vec<SourceFile>, Error> {
    let entries = fs::read_dir(dir).map_err(|e| Error::new(format!("{}: {e}", dir.display())))?;
    let mut paths: Vec<PathBuf> = entries
        .flatten()
        .map(|e| e.path())
        .filter(|p| p.extension().is_some_and(|x| x == "sql"))
        .collect();
    paths.sort();
    if paths.is_empty() {
        return Err(Error::new(format!("no .sql files in {}", dir.display())));
    }
    paths
        .into_iter()
        .map(|path| {
            let text = fs::read_to_string(&path)
                .map_err(|e| Error::new(format!("{}: {e}", path.display())))?;
            let stem = path
                .file_stem()
                .map(|s| s.to_string_lossy().into_owned())
                .unwrap_or_default();
            let module = if stem.starts_with('_') {
                format!("_{}", naming::snake_ident(&stem))
            } else {
                naming::snake_ident(&stem)
            };
            // Relative to the input directory's parent, so the generated
            // files don't depend on where the command runs.
            let shown = match (dir.file_name(), path.file_name()) {
                (Some(d), Some(f)) => format!("{}/{}", d.to_string_lossy(), f.to_string_lossy()),
                _ => path.display().to_string(),
            };
            Ok(SourceFile {
                path: shown,
                module,
                text,
            })
        })
        .collect()
}

fn parsed_source(sources: &[SourceFile], module: &str) -> String {
    let paths: Vec<&str> = sources
        .iter()
        .filter(|s| s.module == module || (module == "types" && s.module.starts_with('_')))
        .map(|s| s.path.as_str())
        .collect();
    paths.join("`, `")
}

/// A migration on disk: `migrations/<version>_<name>.up.sql`.
struct MigrationFile {
    version: i64,
    name: String,
    stem: String,
    up: String,
    reversible: bool,
}

fn existing_migrations(dir: &Path) -> Result<Vec<MigrationFile>, Error> {
    let Ok(entries) = fs::read_dir(dir) else {
        return Ok(Vec::new());
    };
    let mut out = Vec::new();
    for entry in entries.flatten() {
        let file = entry.file_name().to_string_lossy().into_owned();
        let Some(stem) = file.strip_suffix(".up.sql") else {
            continue;
        };
        let Some((version, name)) = stem.split_once('_') else {
            continue;
        };
        let Ok(version) = version.parse::<i64>() else {
            continue;
        };
        let up =
            fs::read_to_string(entry.path()).map_err(|e| Error::new(format!("{file}: {e}")))?;
        out.push(MigrationFile {
            version,
            name: name.to_owned(),
            stem: stem.to_owned(),
            up,
            reversible: dir.join(format!("{stem}.down.sql")).exists(),
        });
    }
    out.sort_by_key(|m| m.version);
    Ok(out)
}

/// Resolve `.` and `..` without touching the file system.
fn normalize(path: &Path) -> PathBuf {
    let mut out = PathBuf::new();
    for component in path.components() {
        match component {
            std::path::Component::CurDir => {}
            std::path::Component::ParentDir => {
                if matches!(
                    out.components().next_back(),
                    Some(std::path::Component::Normal(_))
                ) {
                    out.pop();
                } else {
                    out.push("..");
                }
            }
            other => out.push(other),
        }
    }
    out
}

/// `to` relative to the directory `from` (both relative to the same place,
/// or both absolute).
fn relative_path(from: &Path, to: &Path) -> PathBuf {
    let from = normalize(from);
    let to = normalize(to);
    let from: Vec<_> = from.components().collect();
    let to: Vec<_> = to.components().collect();
    let common = from.iter().zip(&to).take_while(|(a, b)| a == b).count();
    let mut out = PathBuf::new();
    for _ in common..from.len() {
        out.push("..");
    }
    for c in &to[common..] {
        out.push(c);
    }
    out
}

fn first_line(sql: &str) -> &str {
    sql.lines().next().unwrap_or_default()
}

fn json<T: serde::Serialize>(value: &T) -> Result<String, Error> {
    serde_json::to_string_pretty(value)
        .map(|s| s + "\n")
        .map_err(|e| Error::new(e.to_string()))
}

fn lib_rs(modules: &BTreeMap<String, String>, input: &str) -> Result<String, Error> {
    // `#[rustfmt::skip]` keeps `cargo fmt` from reformatting generated
    // modules (which would make `rok-db-gen check` fail); declarations are
    // sorted the way rustfmt sorts them.
    let mut mods: Vec<(String, &str)> = modules.keys().map(|m| (m.clone(), "pub ")).collect();
    mods.push(("migrations".to_owned(), "pub "));
    mods.push(("down".to_owned(), ""));
    mods.push(("up".to_owned(), ""));
    mods.sort();
    let mods: String = mods
        .iter()
        .map(|(m, vis)| format!("#[rustfmt::skip]\n{vis}mod {m};\n"))
        .collect();
    let source = format!(
        "//! Database code generated by rok-db-gen from `{input}/*.sql`: one module\n\
         //! per file with its models, enums and queries, and the migrations.\n\
         //!\n\
         //! Apply pending migrations with [`up`] and revert them with [`down`].\n\
         #![allow(clippy::all, clippy::pedantic, clippy::nursery)]\n\n\
         {mods}\n\
         pub use down::down;\n\
         pub use up::up;\n"
    );
    emit::format(&source, HEADER)
}

fn migrations_rs(migrations: &[MigrationFile]) -> Result<String, Error> {
    let mut items = String::new();
    for m in migrations {
        let down = if m.reversible {
            format!("include_str!(\"../migrations/{}.down.sql\")", m.stem)
        } else {
            "\"\"".to_owned()
        };
        items.push_str(&format!(
            "rok_db::migration::Migration {{ version: {}, name: {:?}, up: include_str!(\"../migrations/{}.up.sql\"), down: {down} }},\n",
            m.version, m.name, m.stem
        ));
    }
    let source = format!(
        "//! Every migration, oldest first. Each one is a pair of files in\n\
         //! `migrations/`; never edit a migration that was applied somewhere.\n\n\
         /// Every migration, oldest first.\n\
         pub const MIGRATIONS: &[rok_db::migration::Migration] = &[\n{items}];\n"
    );
    emit::format(&source, HEADER)
}

const UP_RS: &str = "/// Apply every pending migration, oldest first, each in its own transaction.\n\
/// Returns the versions applied.\n\
pub async fn up(db: &rok_db::Db) -> rok_db::Result<Vec<i64>> {\n\
    rok_db::migration::apply(db, crate::migrations::MIGRATIONS).await\n\
}\n";

const DOWN_RS: &str = "/// Revert the latest `steps` applied migrations, newest first. Returns the\n\
/// versions reverted.\n\
pub async fn down(db: &rok_db::Db, steps: usize) -> rok_db::Result<Vec<i64>> {\n\
    rok_db::migration::revert(db, crate::migrations::MIGRATIONS, steps).await\n\
}\n";

fn cargo_toml(options: &Options, features: &BTreeSet<Feature>) -> String {
    let features: Vec<String> = features.iter().map(|f| format!("{:?}", f.name())).collect();
    let features = if features.is_empty() {
        String::new()
    } else {
        format!(", features = [{}]", features.join(", "))
    };
    let source = match &options.rok_db {
        Dependency::Version(v) => format!("version = {v:?}"),
        Dependency::Path(p) => {
            let relative = relative_path(&options.output, p);
            format!(
                "path = {:?}, version = {:?}",
                relative.display().to_string().replace('\\', "/"),
                default_version()
            )
        }
    };
    format!(
        "# @generated by rok-db-gen. Do not edit: settings live in rok-db.toml.\n\
         [package]\n\
         name = {:?}\n\
         version = \"0.1.0\"\n\
         edition = \"2024\"\n\
         publish = false\n\
         description = \"Database models, queries and migrations generated by rok-db-gen\"\n\n\
         [dependencies]\n\
         rok-db = {{ {source}{features} }}\n",
        options.crate_name
    )
}