Skip to content

Commit df8e542

Browse files
committed
add migration table
1 parent 450f906 commit df8e542

File tree

8 files changed

+122
-2
lines changed

8 files changed

+122
-2
lines changed

nexus/db-model/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ pub mod ipv6;
4242
mod ipv6net;
4343
mod l4_port_range;
4444
mod macaddr;
45+
mod migration;
46+
mod migration_state;
4547
mod name;
4648
mod network_interface;
4749
mod oximeter_info;
@@ -151,6 +153,8 @@ pub use ipv4net::*;
151153
pub use ipv6::*;
152154
pub use ipv6net::*;
153155
pub use l4_port_range::*;
156+
pub use migration::*;
157+
pub use migration_state::*;
154158
pub use name::*;
155159
pub use network_interface::*;
156160
pub use oximeter_info::*;

nexus/db-model/src/migration.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// This Source Code Form is subject to the terms of the Mozilla Public
2+
// License, v. 2.0. If a copy of the MPL was not distributed with this
3+
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4+
5+
use crate::schema::migration;
6+
use crate::MigrationState;
7+
use serde::Deserialize;
8+
use serde::Serialize;
9+
use uuid::Uuid;
10+
11+
/// The state of a migration as understood by Nexus.
12+
#[derive(
13+
Clone, Debug, Queryable, Insertable, Selectable, Serialize, Deserialize,
14+
)]
15+
#[diesel(table_name = migration)]
16+
pub struct Migration {
17+
/// The migration's UUID.
18+
///
19+
/// This is the primary key of the migration table and is referenced by the
20+
/// `instance` table's `migration_id` field.
21+
pub id: Uuid,
22+
23+
/// The state of the migration source VMM.
24+
pub source_state: MigrationState,
25+
26+
/// The ID of the migration source VMM.
27+
pub source_propolis_id: Uuid,
28+
29+
/// The state of the migration target VMM.
30+
pub target_state: MigrationState,
31+
32+
/// The ID of the migration target VMM.
33+
pub target_propolis_id: Uuid,
34+
}

nexus/db-model/src/migration_state.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// This Source Code Form is subject to the terms of the Mozilla Public
2+
// License, v. 2.0. If a copy of the MPL was not distributed with this
3+
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4+
5+
//! Database representation of a migration's state as understood by Nexus.
6+
7+
use super::impl_enum_type;
8+
use serde::{Deserialize, Serialize};
9+
use std::fmt;
10+
11+
impl_enum_type!(
12+
#[derive(Clone, SqlType, Debug, QueryId)]
13+
#[diesel(postgres_type(name = "migration_state", schema = "public"))]
14+
pub struct MigrationStateEnum;
15+
16+
#[derive(Clone, Copy, Debug, AsExpression, FromSqlRow, Serialize, Deserialize, PartialEq, Eq)]
17+
#[diesel(sql_type = MigrationStateEnum)]
18+
// match the database representation when serializing
19+
#[serde(rename_all = "snake_case")]
20+
pub enum MigrationState;
21+
22+
// Enum values
23+
InProgress => b"in_progress"
24+
Completed => b"completed"
25+
Failed => b"failed"
26+
);
27+
28+
impl MigrationState {
29+
pub fn label(&self) -> &'static str {
30+
match self {
31+
Self::InProgress => "in_progress",
32+
Self::Completed => "completed",
33+
Self::Failed => "failed",
34+
}
35+
}
36+
}
37+
38+
impl fmt::Display for MigrationState {
39+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40+
f.write_str(self.label())
41+
}
42+
}

nexus/db-model/src/schema.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1751,6 +1751,19 @@ table! {
17511751
}
17521752
}
17531753

1754+
table! {
1755+
migration (id) {
1756+
id -> Uuid,
1757+
source_state -> crate::MigrationStateEnum,
1758+
source_propolis_id -> Uuid,
1759+
target_state -> crate::MigrationStateEnum,
1760+
target_propolis_id -> Uuid,
1761+
}
1762+
}
1763+
1764+
allow_tables_to_appear_in_same_query!(instance, migration);
1765+
joinable!(instance -> migration (migration_id));
1766+
17541767
allow_tables_to_appear_in_same_query!(
17551768
ip_pool_range,
17561769
ip_pool,

nexus/db-model/src/schema_versions.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use std::collections::BTreeMap;
1717
///
1818
/// This must be updated when you change the database schema. Refer to
1919
/// schema/crdb/README.adoc in the root of this repository for details.
20-
pub const SCHEMA_VERSION: SemverVersion = SemverVersion::new(68, 0, 0);
20+
pub const SCHEMA_VERSION: SemverVersion = SemverVersion::new(69, 0, 0);
2121

2222
/// List of all past database schema versions, in *reverse* order
2323
///
@@ -29,6 +29,7 @@ static KNOWN_VERSIONS: Lazy<Vec<KnownVersion>> = Lazy::new(|| {
2929
// | leaving the first copy as an example for the next person.
3030
// v
3131
// KnownVersion::new(next_int, "unique-dirname-with-the-sql-files"),
32+
KnownVersion::new(69, "add-migration-state-enum"),
3233
KnownVersion::new(68, "filter-v2p-mapping-by-instance-state"),
3334
KnownVersion::new(67, "add-instance-updater-lock"),
3435
KnownVersion::new(66, "blueprint-crdb-preserve-downgrade"),
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE TYPE IF NOT EXISTS omicron.public.migration_state AS ENUM (
2+
'in_progress',
3+
'failed',
4+
'completed'
5+
);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CREATE TABLE IF NOT EXISTS omicron.public.migration (
2+
id UUID PRIMARY KEY,
3+
source_state omicron.public.migration_state NOT NULL,
4+
source_propolis_id UUID NOT NULL,
5+
target_state omicron.public.migration_state NOT NULL,
6+
target_propolis_id UUID NOT NULL
7+
);

schema/crdb/dbinit.sql

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4008,6 +4008,20 @@ VALUES (
40084008
ON CONFLICT (id)
40094009
DO NOTHING;
40104010

4011+
CREATE TYPE IF NOT EXISTS omicron.public.migration_state AS ENUM (
4012+
'in_progress',
4013+
'failed',
4014+
'completed'
4015+
);
4016+
4017+
-- A table of the states of current migrations.
4018+
CREATE TABLE IF NOT EXISTS omicron.public.migration (
4019+
id UUID PRIMARY KEY,
4020+
source_state omicron.public.migration_state NOT NULL,
4021+
source_propolis_id UUID NOT NULL,
4022+
target_state omicron.public.migration_state NOT NULL,
4023+
target_propolis_id UUID NOT NULL
4024+
);
40114025

40124026
/*
40134027
* Keep this at the end of file so that the database does not contain a version
@@ -4020,7 +4034,7 @@ INSERT INTO omicron.public.db_metadata (
40204034
version,
40214035
target_version
40224036
) VALUES
4023-
(TRUE, NOW(), NOW(), '68.0.0', NULL)
4037+
(TRUE, NOW(), NOW(), '69.0.0', NULL)
40244038
ON CONFLICT DO NOTHING;
40254039

40264040
COMMIT;

0 commit comments

Comments
 (0)