Skip to content

[nexus] Add schema changes for quiescing #8533

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 71 additions & 5 deletions nexus/db-model/src/db_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,95 @@ use chrono::{DateTime, Utc};
use nexus_db_schema::schema::db_metadata;
use serde::{Deserialize, Serialize};

/// Internal database metadata
/// These fields of "db_metadata" have been stable since the initial
/// release of the database.
///
/// For backwards-compatibility purposes, they can be loaded separately
/// from DbMetadata.
#[derive(
Queryable, Insertable, Debug, Clone, Selectable, Serialize, Deserialize,
)]
#[diesel(table_name = db_metadata)]
pub struct DbMetadata {
pub struct DbMetadataBase {
singleton: bool,
time_created: DateTime<Utc>,
time_modified: DateTime<Utc>,
version: SemverVersion,
target_version: Option<SemverVersion>,
}

impl DbMetadataBase {
pub fn version(&self) -> &SemverVersion {
&self.version
}
}

/// Internal database metadata
#[derive(
Queryable, Insertable, Debug, Clone, Selectable, Serialize, Deserialize,
)]
#[diesel(table_name = db_metadata)]
pub struct DbMetadata {
#[diesel(embed)]
base: DbMetadataBase,
quiesce_started: bool,
quiesce_completed: bool,
}

impl DbMetadata {
pub fn from_base(base: DbMetadataBase, quiesced: bool) -> Self {
Self { base, quiesce_started: quiesced, quiesce_completed: quiesced }
}

pub fn time_created(&self) -> &DateTime<Utc> {
&self.time_created
&self.base.time_created
}

pub fn time_modified(&self) -> &DateTime<Utc> {
&self.time_modified
&self.base.time_modified
}

pub fn version(&self) -> &SemverVersion {
&self.version
&self.base.version
}

pub fn target_version(&self) -> Option<&SemverVersion> {
self.base.target_version.as_ref()
}

pub fn quiesce_started(&self) -> bool {
self.quiesce_started
}

pub fn quiesce_completed(&self) -> bool {
self.quiesce_completed
}
}

#[derive(AsChangeset)]
#[diesel(table_name = db_metadata)]
pub struct DbMetadataUpdate {
time_modified: DateTime<Utc>,
version: String,
#[diesel(treat_none_as_null = true)]
target_version: Option<String>,
quiesce_started: Option<bool>,
quiesce_completed: Option<bool>,
}

impl DbMetadataUpdate {
pub fn update_to_version(version: semver::Version) -> Self {
Self {
time_modified: Utc::now(),
version: version.to_string(),
target_version: None,
quiesce_started: None,
quiesce_completed: None,
}
}

pub fn clear_quiesce(&mut self) {
self.quiesce_started = Some(false);
self.quiesce_completed = Some(false);
}
}
10 changes: 9 additions & 1 deletion nexus/db-model/src/schema_versions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::{collections::BTreeMap, sync::LazyLock};
///
/// This must be updated when you change the database schema. Refer to
/// schema/crdb/README.adoc in the root of this repository for details.
pub const SCHEMA_VERSION: Version = Version::new(174, 0, 0);
pub const SCHEMA_VERSION: Version = Version::new(175, 0, 0);

/// List of all past database schema versions, in *reverse* order
///
Expand All @@ -28,6 +28,7 @@ static KNOWN_VERSIONS: LazyLock<Vec<KnownVersion>> = LazyLock::new(|| {
// | leaving the first copy as an example for the next person.
// v
// KnownVersion::new(next_int, "unique-dirname-with-the-sql-files"),
KnownVersion::new(175, "db-metadata-quiesce"),
KnownVersion::new(174, "add-tuf-rot-by-sign"),
KnownVersion::new(173, "inv-internal-dns"),
KnownVersion::new(172, "add-zones-with-mupdate-override"),
Expand Down Expand Up @@ -218,6 +219,13 @@ static KNOWN_VERSIONS: LazyLock<Vec<KnownVersion>> = LazyLock::new(|| {
/// The earliest supported schema version.
pub const EARLIEST_SUPPORTED_VERSION: Version = Version::new(1, 0, 0);

/// The version of the schema where "quiesce" fields were added
/// to db_metadata.
///
/// omicron.public.db_metadata is read a part of performing schema changes,
/// so this version is treated specially for backwards compatibiliy.
pub const QUIESCE_VERSION: Version = Version::new(175, 0, 0);

/// Describes one version of the database schema
#[derive(Debug, Clone)]
struct KnownVersion {
Expand Down
Loading
Loading