Skip to content

Commit 1a1c2dd

Browse files
committed
Add schema changes for quiescing
1 parent e15b5d9 commit 1a1c2dd

File tree

10 files changed

+712
-68
lines changed

10 files changed

+712
-68
lines changed

nexus/db-model/src/db_metadata.rs

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,95 @@ use chrono::{DateTime, Utc};
77
use nexus_db_schema::schema::db_metadata;
88
use serde::{Deserialize, Serialize};
99

10-
/// Internal database metadata
10+
/// These fields of "db_metadata" have been stable since the initial
11+
/// release of the database.
12+
///
13+
/// For backwards-compatibility purposes, they can be loaded separately
14+
/// from DbMetadata.
1115
#[derive(
1216
Queryable, Insertable, Debug, Clone, Selectable, Serialize, Deserialize,
1317
)]
1418
#[diesel(table_name = db_metadata)]
15-
pub struct DbMetadata {
19+
pub struct DbMetadataBase {
1620
singleton: bool,
1721
time_created: DateTime<Utc>,
1822
time_modified: DateTime<Utc>,
1923
version: SemverVersion,
2024
target_version: Option<SemverVersion>,
2125
}
2226

27+
impl DbMetadataBase {
28+
pub fn version(&self) -> &SemverVersion {
29+
&self.version
30+
}
31+
}
32+
33+
/// Internal database metadata
34+
#[derive(
35+
Queryable, Insertable, Debug, Clone, Selectable, Serialize, Deserialize,
36+
)]
37+
#[diesel(table_name = db_metadata)]
38+
pub struct DbMetadata {
39+
#[diesel(embed)]
40+
base: DbMetadataBase,
41+
quiesce_started: bool,
42+
quiesce_completed: bool,
43+
}
44+
2345
impl DbMetadata {
46+
pub fn from_base(base: DbMetadataBase, quiesced: bool) -> Self {
47+
Self { base, quiesce_started: quiesced, quiesce_completed: quiesced }
48+
}
49+
2450
pub fn time_created(&self) -> &DateTime<Utc> {
25-
&self.time_created
51+
&self.base.time_created
2652
}
2753

2854
pub fn time_modified(&self) -> &DateTime<Utc> {
29-
&self.time_modified
55+
&self.base.time_modified
3056
}
3157

3258
pub fn version(&self) -> &SemverVersion {
33-
&self.version
59+
&self.base.version
60+
}
61+
62+
pub fn target_version(&self) -> Option<&SemverVersion> {
63+
self.base.target_version.as_ref()
64+
}
65+
66+
pub fn quiesce_started(&self) -> bool {
67+
self.quiesce_started
68+
}
69+
70+
pub fn quiesce_completed(&self) -> bool {
71+
self.quiesce_completed
72+
}
73+
}
74+
75+
#[derive(AsChangeset)]
76+
#[diesel(table_name = db_metadata)]
77+
pub struct DbMetadataUpdate {
78+
time_modified: DateTime<Utc>,
79+
version: String,
80+
#[diesel(treat_none_as_null = true)]
81+
target_version: Option<String>,
82+
quiesce_started: Option<bool>,
83+
quiesce_completed: Option<bool>,
84+
}
85+
86+
impl DbMetadataUpdate {
87+
pub fn update_to_version(version: semver::Version) -> Self {
88+
Self {
89+
time_modified: Utc::now(),
90+
version: version.to_string(),
91+
target_version: None,
92+
quiesce_started: None,
93+
quiesce_completed: None,
94+
}
95+
}
96+
97+
pub fn clear_quiesce(&mut self) {
98+
self.quiesce_started = Some(false);
99+
self.quiesce_completed = Some(false);
34100
}
35101
}

nexus/db-model/src/schema_versions.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use std::{collections::BTreeMap, sync::LazyLock};
1616
///
1717
/// This must be updated when you change the database schema. Refer to
1818
/// schema/crdb/README.adoc in the root of this repository for details.
19-
pub const SCHEMA_VERSION: Version = Version::new(174, 0, 0);
19+
pub const SCHEMA_VERSION: Version = Version::new(175, 0, 0);
2020

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

222+
/// The version of the schema where "quiesce" fields were added
223+
/// to db_metadata.
224+
///
225+
/// omicron.public.db_metadata is read a part of performing schema changes,
226+
/// so this version is treated specially for backwards compatibiliy.
227+
pub const QUIESCE_VERSION: Version = Version::new(175, 0, 0);
228+
221229
/// Describes one version of the database schema
222230
#[derive(Debug, Clone)]
223231
struct KnownVersion {

0 commit comments

Comments
 (0)