-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
nexus shattering 3 of 3: Extract nexus-db-model crate from nexus::db:…
…:model (#1478)
- Loading branch information
1 parent
4f86901
commit 5e835f1
Showing
71 changed files
with
524 additions
and
408 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
[package] | ||
name = "nexus-db-model" | ||
version = "0.1.0" | ||
edition = "2021" | ||
license = "MPL-2.0" | ||
|
||
[dependencies] | ||
anyhow = "1.0" | ||
chrono = { version = "0.4", features = ["serde"] } | ||
diesel = { version = "2.0.0-rc.1", features = ["postgres", "r2d2", "chrono", "serde_json", "network-address", "uuid"] } | ||
hex = "0.4.3" | ||
ipnetwork = "0.18" | ||
macaddr = { version = "1.0.1", features = [ "serde_std" ]} | ||
newtype_derive = "0.1.6" | ||
parse-display = "0.5.4" | ||
rand = "0.8.5" | ||
ref-cast = "1.0" | ||
schemars = { version = "0.8.10", features = ["chrono", "uuid1"] } | ||
serde = { version = "1.0", features = ["derive"] } | ||
serde_json = "1.0" | ||
uuid = { version = "1.1.0", features = ["serde", "v4"] } | ||
|
||
steno = { git = "https://github.com/oxidecomputer/steno", branch = "main" } | ||
|
||
db-macros = { path = "../db-macros" } | ||
omicron-common = { path = "../../common" } | ||
nexus-defaults = { path = "../defaults" } | ||
nexus-types = { path = "../types" } | ||
sled-agent-client = { path = "../../sled-agent-client" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
use diesel::pg::Pg; | ||
use diesel::Column; | ||
use diesel::ExpressionMethods; | ||
use diesel::Selectable; | ||
use std::fmt::Debug; | ||
|
||
/// Trait to be implemented by any structs representing a collection. | ||
/// For example, since Organizations have a one-to-many relationship with | ||
/// Projects, the Organization datatype should implement this trait. | ||
/// ``` | ||
/// # use diesel::prelude::*; | ||
/// # use nexus_db_model::DatastoreCollectionConfig; | ||
/// # use nexus_db_model::Generation; | ||
/// # | ||
/// # table! { | ||
/// # test_schema.organization (id) { | ||
/// # id -> Uuid, | ||
/// # time_deleted -> Nullable<Timestamptz>, | ||
/// # rcgen -> Int8, | ||
/// # } | ||
/// # } | ||
/// # | ||
/// # table! { | ||
/// # test_schema.project (id) { | ||
/// # id -> Uuid, | ||
/// # time_deleted -> Nullable<Timestamptz>, | ||
/// # organization_id -> Uuid, | ||
/// # } | ||
/// # } | ||
/// | ||
/// #[derive(Queryable, Insertable, Debug, Selectable)] | ||
/// #[diesel(table_name = project)] | ||
/// struct Project { | ||
/// pub id: uuid::Uuid, | ||
/// pub time_deleted: Option<chrono::DateTime<chrono::Utc>>, | ||
/// pub organization_id: uuid::Uuid, | ||
/// } | ||
/// | ||
/// #[derive(Queryable, Insertable, Debug, Selectable)] | ||
/// #[diesel(table_name = organization)] | ||
/// struct Organization { | ||
/// pub id: uuid::Uuid, | ||
/// pub time_deleted: Option<chrono::DateTime<chrono::Utc>>, | ||
/// pub rcgen: Generation, | ||
/// } | ||
/// | ||
/// impl DatastoreCollectionConfig<Project> for Organization { | ||
/// // Type of Organization::identity::id and Project::organization_id | ||
/// type CollectionId = uuid::Uuid; | ||
/// | ||
/// type GenerationNumberColumn = organization::dsl::rcgen; | ||
/// type CollectionTimeDeletedColumn = organization::dsl::time_deleted; | ||
/// | ||
/// type CollectionIdColumn = project::dsl::organization_id; | ||
/// } | ||
/// ``` | ||
pub trait DatastoreCollectionConfig<ResourceType> { | ||
/// The Rust type of the collection id (typically Uuid for us) | ||
type CollectionId: Copy + Debug; | ||
|
||
/// The column in the CollectionTable that acts as a generation number. | ||
/// This is the "child-resource-generation-number" in RFD 192. | ||
type GenerationNumberColumn: Column + Default; | ||
|
||
/// The time deleted column in the CollectionTable | ||
// We enforce that this column comes from the same table as | ||
// GenerationNumberColumn when defining insert_resource() below. | ||
type CollectionTimeDeletedColumn: Column + Default; | ||
|
||
/// The column in the ResourceTable that acts as a foreign key into | ||
/// the CollectionTable | ||
type CollectionIdColumn: Column; | ||
} | ||
|
||
/// Trait to be implemented by structs representing an attachable collection. | ||
/// | ||
/// For example, since Instances have a one-to-many relationship with | ||
/// Disks, the Instance datatype should implement this trait. | ||
/// ``` | ||
/// # use diesel::prelude::*; | ||
/// # use nexus_db_model::DatastoreAttachTargetConfig; | ||
/// # | ||
/// # table! { | ||
/// # test_schema.instance (id) { | ||
/// # id -> Uuid, | ||
/// # time_deleted -> Nullable<Timestamptz>, | ||
/// # } | ||
/// # } | ||
/// # | ||
/// # table! { | ||
/// # test_schema.disk (id) { | ||
/// # id -> Uuid, | ||
/// # time_deleted -> Nullable<Timestamptz>, | ||
/// # instance_id -> Nullable<Uuid>, | ||
/// # } | ||
/// # } | ||
/// | ||
/// #[derive(Queryable, Debug, Selectable)] | ||
/// #[diesel(table_name = disk)] | ||
/// struct Disk { | ||
/// pub id: uuid::Uuid, | ||
/// pub time_deleted: Option<chrono::DateTime<chrono::Utc>>, | ||
/// pub instance_id: Option<uuid::Uuid>, | ||
/// } | ||
/// | ||
/// #[derive(Queryable, Debug, Selectable)] | ||
/// #[diesel(table_name = instance)] | ||
/// struct Instance { | ||
/// pub id: uuid::Uuid, | ||
/// pub time_deleted: Option<chrono::DateTime<chrono::Utc>>, | ||
/// } | ||
/// | ||
/// impl DatastoreAttachTargetConfig<Disk> for Instance { | ||
/// // Type of instance::id and disk::id. | ||
/// type Id = uuid::Uuid; | ||
/// | ||
/// type CollectionIdColumn = instance::dsl::id; | ||
/// type CollectionTimeDeletedColumn = instance::dsl::time_deleted; | ||
/// | ||
/// type ResourceIdColumn = disk::dsl::id; | ||
/// type ResourceCollectionIdColumn = disk::dsl::instance_id; | ||
/// type ResourceTimeDeletedColumn = disk::dsl::time_deleted; | ||
/// } | ||
/// ``` | ||
pub trait DatastoreAttachTargetConfig<ResourceType>: | ||
Selectable<Pg> + Sized | ||
{ | ||
/// The Rust type of the collection and resource ids (typically Uuid). | ||
type Id: Copy + Debug + PartialEq + Send + 'static; | ||
|
||
/// The primary key column of the collection. | ||
type CollectionIdColumn: Column; | ||
|
||
/// The time deleted column in the CollectionTable | ||
type CollectionTimeDeletedColumn: Column<Table = <Self::CollectionIdColumn as Column>::Table> | ||
+ Default | ||
+ ExpressionMethods; | ||
|
||
/// The primary key column of the resource | ||
type ResourceIdColumn: Column; | ||
|
||
/// The column in the resource acting as a foreign key into the Collection | ||
type ResourceCollectionIdColumn: Column<Table = <Self::ResourceIdColumn as Column>::Table> | ||
+ Default | ||
+ ExpressionMethods; | ||
|
||
/// The time deleted column in the ResourceTable | ||
type ResourceTimeDeletedColumn: Column<Table = <Self::ResourceIdColumn as Column>::Table> | ||
+ Default | ||
+ ExpressionMethods; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.