diff --git a/crates/sourceorigin-standard/README.md b/crates/sourceorigin-standard/README.md index e265c38..a7d079d 100644 --- a/crates/sourceorigin-standard/README.md +++ b/crates/sourceorigin-standard/README.md @@ -62,6 +62,7 @@ A repository adopts the standard in four steps: - `docs-summary`: With a `book.toml`, every Markdown page under its `src` is linked from its `SUMMARY.md`, or included in a page with `{{#include …}}`. - `docs-images`: Every image in a Markdown file, outside code blocks, has alt text: `![what it shows](…)`, or an `` with a non-empty `alt`. - `wording`: No Markdown file, outside code, holds as a word a spelling `standard.toml`'s `[wording]` rejects, such as `"Source Origin" = "SourceOrigin"`. +- `migration-header`: Every file matching `standard.toml`'s `migrations` globs, such as `crates/*/migrations/*.sql`, starts with a `--` comment. Each rule is a variant of [`Rule`]: its name is the one `standard.toml` and @@ -87,6 +88,9 @@ anyhow-crates = ["my-cli"] # whole, such as a check on the minimum supported Rust version. cargo-in-ci = ["cargo +1.85 check --locked --workspace"] +# The database migrations, each of which starts with a `--` comment. +migrations = ["crates/*/migrations/*.sql"] + # Rules the repository departs from, each with its reason: everywhere, or # only in the files `paths` matches and the crates `crates` names. A # deviation, or an entry of its scope, that excuses no violation is one. diff --git a/crates/sourceorigin-standard/src/config/mod.rs b/crates/sourceorigin-standard/src/config/mod.rs index 42b55c3..f23a8a9 100644 --- a/crates/sourceorigin-standard/src/config/mod.rs +++ b/crates/sourceorigin-standard/src/config/mod.rs @@ -84,6 +84,9 @@ pub(crate) struct Config { /// The `[wording]` table: each rejected spelling, with its line, and /// the one to write instead. pub(crate) wording: Vec<(Listed, String)>, + /// The globs of the database migrations, such as + /// `crates/*/migrations/*.sql`. + pub(crate) migrations: Vec, } /// The `[comments]` table: what comments the rules allow. @@ -235,6 +238,7 @@ struct File { anyhow_crates: Vec>, cargo_in_ci: Vec>, wording: BTreeMap, String>, + migrations: Vec, } /// A deviation as written: its reason alone, for everywhere, or a table @@ -365,6 +369,7 @@ impl Config { comments: file.comments, anyhow_crates: listed(file.anyhow_crates), cargo_in_ci: listed(file.cargo_in_ci), + migrations: file.migrations, wording: file .wording .into_iter() diff --git a/crates/sourceorigin-standard/src/rules/migration_header.rs b/crates/sourceorigin-standard/src/rules/migration_header.rs new file mode 100644 index 0000000..b24cc7f --- /dev/null +++ b/crates/sourceorigin-standard/src/rules/migration_header.rs @@ -0,0 +1,21 @@ +//! `migration-header`: each migration says what it changes, and what +//! happens to the data already there. + +use super::{Finding, Repository}; + +impl Repository { + /// Each migration whose first line isn't a `--` comment. + pub(super) fn migration_header(&self) -> Vec { + self.migrations + .iter() + .filter(|migration| !migration.text.trim_start().starts_with("--")) + .map(|migration| Finding { + path: migration.path.clone(), + line: 1, + message: "the migration doesn't start with a `--` comment: say what it changes, \ + and what happens to the data already there" + .to_owned(), + }) + .collect() + } +} diff --git a/crates/sourceorigin-standard/src/rules/mod.rs b/crates/sourceorigin-standard/src/rules/mod.rs index 0749911..62ebce8 100644 --- a/crates/sourceorigin-standard/src/rules/mod.rs +++ b/crates/sourceorigin-standard/src/rules/mod.rs @@ -25,6 +25,7 @@ mod justfile; mod layout; mod level; mod manifest; +mod migration_header; mod modules; mod nextest_config; mod pr_template; @@ -262,6 +263,9 @@ rules! { /// `standard.toml`'s `[wording]` rejects, such as /// `"Source Origin" = "SourceOrigin"`. Wording = "wording" => wording, + /// Every file matching `standard.toml`'s `migrations` globs, such as + /// `crates/*/migrations/*.sql`, starts with a `--` comment. + MigrationHeader = "migration-header" => migration_header, } /// Why the rules couldn't be checked. @@ -470,6 +474,8 @@ struct Repository { book: Option>, /// Every Markdown file. markdown: Vec, + /// The files `migrations` matches. + migrations: Vec, } impl Repository { @@ -479,8 +485,8 @@ impl Repository { let metadata = Metadata::load(Workspace::new(root))?; let (sources, unparsed) = Source::load_all(root)?; let root_manifest = TomlFile::::load_root(root)?; + let config = Config::load(root)?; Ok(Self { - config: Config::load(root)?, manifests: TomlFile::load_all(&metadata)?, lint_levels: LintLevel::all(&root_manifest), root_manifest, @@ -501,6 +507,8 @@ impl Repository { pr_template: TextFile::load_all(root, &[pr_template::PATH])?.pop(), book: TomlFile::load(root, docs_summary::BOOK)?, markdown: TextFile::load_all(root, &["*.md"])?, + migrations: TextFile::load_globs(root, &config.migrations)?, + config, metadata, sources, unparsed, diff --git a/crates/sourceorigin-standard/src/rules/tests/migration_header.rs b/crates/sourceorigin-standard/src/rules/tests/migration_header.rs new file mode 100644 index 0000000..8eb9acd --- /dev/null +++ b/crates/sourceorigin-standard/src/rules/tests/migration_header.rs @@ -0,0 +1,30 @@ +use super::{Fixture, INHERITS}; +use crate::rules::Rule; + +#[test] +fn every_migration_says_what_it_changes() { + let fixture = Fixture::new(); + fixture + .add_crate("core", INHERITS) + .write( + "standard.toml", + "migrations = [\"crates/*/migrations/*.sql\"]\n", + ) + .write( + "crates/core/migrations/0001_apps.sql", + "-- Adds the apps table; no data exists yet.\nCREATE TABLE apps (id INTEGER);\n", + ) + .write( + "crates/core/migrations/0002_names.sql", + "ALTER TABLE apps ADD COLUMN name TEXT;\n", + ) + .write("crates/core/src/query.sql", "SELECT 1;\n"); + assert_eq!( + fixture.violations(Rule::MigrationHeader), + [ + "crates/core/migrations/0002_names.sql:1: the migration doesn't start with a `--` \ + comment: say what it changes, and what happens to the data already there \ + (migration-header)" + ] + ); +} diff --git a/crates/sourceorigin-standard/src/rules/tests/mod.rs b/crates/sourceorigin-standard/src/rules/tests/mod.rs index aa0e1d2..c011563 100644 --- a/crates/sourceorigin-standard/src/rules/tests/mod.rs +++ b/crates/sourceorigin-standard/src/rules/tests/mod.rs @@ -19,6 +19,7 @@ mod framework; mod generated_lints; mod job_timeouts; mod layout; +mod migration_header; mod nextest_config; mod pr_template; mod recipes; diff --git a/crates/sourceorigin-standard/src/rules/text_file.rs b/crates/sourceorigin-standard/src/rules/text_file.rs index 05f67b3..eb88114 100644 --- a/crates/sourceorigin-standard/src/rules/text_file.rs +++ b/crates/sourceorigin-standard/src/rules/text_file.rs @@ -33,4 +33,17 @@ impl TextFile { }) .collect() } + + /// Every file git tracks or would track under `root` matching any of + /// `globs`, such as `crates/*/migrations/*.sql`; none without globs. + pub(super) fn load_globs(root: &Path, globs: &[String]) -> Result, Error> { + if globs.is_empty() { + return Ok(Vec::new()); + } + let pathspecs: Vec = globs.iter().map(|glob| format!(":(glob){glob}")).collect(); + Self::load_all( + root, + &pathspecs.iter().map(String::as_str).collect::>(), + ) + } }