Skip to content

Add review context comments#10075

Merged
mergeconflict merged 2 commits intomainfrom
mergeconflict/fm-comments
Mar 24, 2026
Merged

Add review context comments#10075
mergeconflict merged 2 commits intomainfrom
mergeconflict/fm-comments

Conversation

@mergeconflict
Copy link
Copy Markdown
Contributor

@mergeconflict mergeconflict commented Mar 17, 2026

Capture a few comments from the conversation between @hawkw and @smklein in #9552 and previous PRs so I don't mess things up later.

@mergeconflict mergeconflict requested review from hawkw and smklein March 17, 2026 19:58
@mergeconflict mergeconflict force-pushed the mergeconflict/fm-comments branch from d2bf8fc to b6ee77e Compare March 17, 2026 20:23
Comment thread nexus/db-model/src/fm.rs Outdated
Comment thread nexus/db-queries/src/db/datastore/fm.rs Outdated
Comment thread nexus/db-queries/src/db/datastore/fm.rs Outdated
Comment on lines +85 to +97
// (not allocating) but insert one at a time? Note that a batched insert
// would need to use `ON CONFLICT DO NOTHING` rather than checking for
// `Conflict` errors from individual inserts, since multiple Nexus
// instances may run this task concurrently.
//
// Currently, these `alert_create` calls have no guard against a stale
// Nexus inserting alerts from an outdated sitrep. This is fine for now
// because alert requests are carried forward into newer sitreps, so a
// stale insert is redundant rather than incorrect. However, if alerts
// are ever hard-deleted (e.g. when a case is closed), a lagging Nexus
// could re-create "zombie" alert records after deletion. At that point,
// the INSERT should be guarded by a CTE that checks the sitrep
// generation matches the current one.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for writing this down. I kinda wonder if we ought to write up an issue for some of this...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's captured in #9592, yeah?

Comment thread nexus/db-queries/src/lib.rs
@mergeconflict mergeconflict force-pushed the mergeconflict/fm-comments branch from cdb8cb5 to 2dbbfa3 Compare March 18, 2026 00:33
@mergeconflict mergeconflict requested a review from hawkw March 18, 2026 00:35
@mergeconflict mergeconflict force-pushed the mergeconflict/fm-comments branch from 2dbbfa3 to c463d46 Compare March 18, 2026 00:40
Move DB model conventions (field ordering, Diesel derives) from
nexus/db-model/src/fm.rs to the nexus-db-model crate-level docs in
lib.rs, with a vmm example showing schema-to-model type mapping.

Move _on_conn/_in_txn naming conventions from the FM datastore module
to nexus/db-queries/src/db/datastore/mod.rs. Fix the framing: connection
sharing is primarily about pool efficiency, not consistency. Keep the
FM-specific correctness detail (concurrent delete detection, #9594)
inline with a pointer to the general docs.
@mergeconflict mergeconflict force-pushed the mergeconflict/fm-comments branch from c463d46 to b65d715 Compare March 18, 2026 01:18
Comment thread nexus/db-model/src/lib.rs
//! column list, so the result columns are always in the order `Queryable`
//! expects. **Always derive both.**
//!
//! # Field ordering convention
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we care: #9927 catches the ordering if it's incorrect.

Comment on lines +511 to +513
// sitrep_id (which is inserted first above). If we crash partway
// through, orphaned child records will be cleaned up when the orphaned
// sitrep is garbage collected.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is #10131 relevant here?

@mergeconflict mergeconflict merged commit 9cca298 into main Mar 24, 2026
16 checks passed
@mergeconflict mergeconflict deleted the mergeconflict/fm-comments branch March 24, 2026 17:10

//! Primary control plane interface for database read and write operations
//!
//! # Method naming conventions
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: i believe the typical markdown convention is that a single # is the title of the entire markdown file/document/doc comment block, so I think this should be:

Suggested change
//! # Method naming conventions
//! ## Method naming conventions

//! Primary control plane interface for database read and write operations
//!
//! # Method naming conventions
//!
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another high-level note on naming conventions that I think might be worth writing down is that it's important to remember that almost all the database queries in this crate are defined as methods on the DataStore type, so everything is datastore.method() rather than namespaced in a module. Therefore, we tend to follow a naming convention with the name of the resource/object/type of data that the query operates on first, followed by the verb, even if this feels less like a grammatically correct English sentence than verb-first. For example, we tend to write things like ereports_insert or instance_delete, rather than insert_ereports or delete_instance. One reason for doing this is that it's a lot more friendly to search and IDE tab-completion; if you type datastore.ereport_, your editor shows you all the methods that operate on ereports, which sis typically more useful than datastore.insert_ suggesting every single insert operation for stuff that you don't care about at the moment.

Comment thread nexus/db-model/src/lib.rs
//! Rust types. `nexus_db_queries` then uses both to build and execute
//! queries in its `DataStore` methods.
//!
//! # How the mapping works
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a level 2 heading since it's not the title of the entire doc comment.

Suggested change
//! # How the mapping works
//! ## How the mapping works

Comment thread nexus/db-model/src/lib.rs
Comment on lines +21 to +22
//! ```text
//! // in nexus-db-schema
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think it wouldn't be too hard to turn this into rust, we'd just need to import a few types first. that way, the compiler could check it...

Comment thread nexus/db-model/src/lib.rs
//! queries in its `DataStore` methods.
//!
//! # How the mapping works
//!
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there's probably some Diesel documentation we could also link to here?

Comment thread nexus/db-model/src/lib.rs
//!
//! The model struct provides the Rust representation of a row:
//!
//! ```text
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similarly, if we added some imports, we could probably turn this into rust and get the compiler to check it?

Comment thread nexus/db-model/src/lib.rs
Comment on lines +69 to +76
//! # Field ordering convention
//!
//! Fields in `Queryable`/`Insertable` structs should be ordered to match the
//! column order in `dbinit.sql`. This keeps the Rust types easy to
//! cross-reference with the schema definition and prevents subtle bugs if a
//! query ever omits `Selectable`.
//!
//! # Representing enums
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these should probably be level 2 or level 3 headings

Suggested change
//! # Field ordering convention
//!
//! Fields in `Queryable`/`Insertable` structs should be ordered to match the
//! column order in `dbinit.sql`. This keeps the Rust types easy to
//! cross-reference with the schema definition and prevents subtle bugs if a
//! query ever omits `Selectable`.
//!
//! # Representing enums
//! ## Field ordering convention
//!
//! Fields in `Queryable`/`Insertable` structs should be ordered to match the
//! column order in `dbinit.sql`. This keeps the Rust types easy to
//! cross-reference with the schema definition and prevents subtle bugs if a
//! query ever omits `Selectable`.
//!
//! ## Representing enums

Comment thread nexus/db-model/src/lib.rs
Comment on lines +52 to +67
//! - **Type translation.** Each field's Rust type must implement Diesel's
//! [`FromSql`](diesel::deserialize::FromSql) and
//! [`ToSql`](diesel::serialize::ToSql) traits for the corresponding column's
//! SQL type. Diesel provides these impls for common types (`Uuid`,
//! `DateTime<Utc>`, `String`, etc.); this crate defines wrapper types like
//! [`DbTypedUuid`] for domain-specific conversions
//! (e.g. distinguishing a sled UUID from any other UUID).
//! - **Column renaming.** Field names must match column names by default, but
//! `#[diesel(column_name = ...)]` allows the Rust name to differ (e.g.
//! `generation` for the `state_generation` column).
//! - **Positional mapping.** [`diesel::Queryable`] maps SQL result columns to
//! struct fields by position, not by name. If the field order doesn't match
//! the column order in the query, fields will silently receive wrong values.
//! Deriving [`diesel::Selectable`] mitigates this by generating an explicit
//! column list, so the result columns are always in the order `Queryable`
//! expects. **Always derive both.**
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I feel like these might be nicer as level 3 subheadings rather than bullet points, at least because then we can link to them from elsewhere?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants