Skip to content

Commit 7bafa74

Browse files
committed
Create db_metadata_nexus_state table
1 parent ba17a4f commit 7bafa74

File tree

18 files changed

+1602
-78
lines changed

18 files changed

+1602
-78
lines changed

nexus/db-model/src/db_metadata.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@
33
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
44

55
use crate::SemverVersion;
6+
use crate::impl_enum_type;
7+
use crate::typed_uuid::DbTypedUuid;
68
use chrono::{DateTime, Utc};
79
use nexus_db_schema::schema::db_metadata;
10+
use nexus_db_schema::schema::db_metadata_nexus;
11+
use omicron_uuid_kinds::{
12+
BlueprintKind, BlueprintUuid, OmicronZoneKind, OmicronZoneUuid,
13+
};
814
use serde::{Deserialize, Serialize};
915

1016
/// Internal database metadata
@@ -33,3 +39,47 @@ impl DbMetadata {
3339
&self.version
3440
}
3541
}
42+
43+
impl_enum_type!(
44+
DbMetadataNexusStateEnum:
45+
46+
#[derive(Clone, Copy, Debug, AsExpression, FromSqlRow, PartialEq, Serialize, Deserialize)]
47+
pub enum DbMetadataNexusState;
48+
49+
// Enum values
50+
Active => b"active"
51+
NotYet => b"not_yet"
52+
Inactive => b"inactive"
53+
);
54+
55+
#[derive(
56+
Queryable, Insertable, Debug, Clone, Selectable, Serialize, Deserialize,
57+
)]
58+
#[diesel(table_name = db_metadata_nexus)]
59+
pub struct DbMetadataNexus {
60+
nexus_id: DbTypedUuid<OmicronZoneKind>,
61+
last_drained_blueprint_id: Option<DbTypedUuid<BlueprintKind>>,
62+
state: DbMetadataNexusState,
63+
}
64+
65+
impl DbMetadataNexus {
66+
pub fn new(nexus_id: OmicronZoneUuid, state: DbMetadataNexusState) -> Self {
67+
Self {
68+
nexus_id: nexus_id.into(),
69+
last_drained_blueprint_id: None,
70+
state,
71+
}
72+
}
73+
74+
pub fn state(&self) -> DbMetadataNexusState {
75+
self.state
76+
}
77+
78+
pub fn nexus_id(&self) -> OmicronZoneUuid {
79+
self.nexus_id.into()
80+
}
81+
82+
pub fn last_drained_blueprint_id(&self) -> Option<BlueprintUuid> {
83+
self.last_drained_blueprint_id.map(|id| id.into())
84+
}
85+
}

nexus/db-model/src/schema_versions.rs

Lines changed: 5 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(180, 0, 0);
19+
pub const SCHEMA_VERSION: Version = Version::new(181, 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(181, "populate-db-metadata-nexus"),
3132
KnownVersion::new(180, "sled-cpu-family"),
3233
KnownVersion::new(179, "add-pending-mgs-updates-host-phase-1"),
3334
KnownVersion::new(178, "change-lldp-management-ip-to-inet"),
@@ -224,6 +225,9 @@ static KNOWN_VERSIONS: LazyLock<Vec<KnownVersion>> = LazyLock::new(|| {
224225
/// The earliest supported schema version.
225226
pub const EARLIEST_SUPPORTED_VERSION: Version = Version::new(1, 0, 0);
226227

228+
/// The version where "db_metadata_nexus" was added.
229+
pub const DB_METADATA_NEXUS_SCHEMA_VERSION: Version = Version::new(181, 0, 0);
230+
227231
/// Describes one version of the database schema
228232
#[derive(Debug, Clone)]
229233
struct KnownVersion {

0 commit comments

Comments
 (0)