Skip to content

use CockroachDB enums in more places in database schema #312

Open

Description

@davepacheco

There are a few places in the schema where we'd like to use CockroachDB enums, but we were prevented by cockroachdb/cockroach#57411 / cockroachdb/cockroach#58084. This was supposed to be resolved in v21.1. So with #195 in, we should be able to switch to the enums. (This is worth confirming before doing too much work here.)

Examples where we want to use this:

  • "Instance" table, "state" field.
  • "Disk" table, "disk_state" field.
  • "Saga" table, "saga_state" field.
  • "SagaNodeEvent" table, "event_type" field.

I skimmed the schema to look for other candidates and I think that about covers it.

I expect this change will involve, for each of the above cases:

  • update the schema SQL to create the enum (many of these have a commented-out version already -- that needs to be compared to the actual Rust enum to make sure these are in sync)
  • presumably there's some change we need to make to the Diesel table! macro invocations, but I'm not sure what that looks like yet

I think that's it, but I'm not sure if there's something else we need to do on the Diesel side.

Activity

  1. added
    database
    on Oct 19, 2021
  2. teisenbe commented on Nov 10, 2021

    @teisenbe
    Contributor

    It looks like we're already using enums, just with some manual conversion back and forth

    table! {
    vpc_router (id) {
    id -> Uuid,
    name -> Text,
    description -> Text,
    time_created -> Timestamptz,
    time_modified -> Timestamptz,
    time_deleted -> Nullable<Timestamptz>,
    kind -> crate::db::model::VpcRouterKindEnum,
    vpc_id -> Uuid,
    }
    }

    omicron/nexus/src/db/model.rs

    Lines 982 to 1033 in b9b0ea9

    /// Custom Enum type for Diesel. Note that the type_name _must_ be all lowercase
    /// or it'll fail.
    #[derive(SqlType, Debug)]
    #[postgres(type_name = "vpc_router_kind", type_schema = "public")]
    pub struct VpcRouterKindEnum;
    #[derive(Clone, Debug, AsExpression, FromSqlRow)]
    #[sql_type = "VpcRouterKindEnum"]
    pub struct VpcRouterKind(pub external::VpcRouterKind);
    impl VpcRouterKind {
    pub fn new(state: external::VpcRouterKind) -> Self {
    Self(state)
    }
    pub fn state(&self) -> &external::VpcRouterKind {
    &self.0
    }
    }
    impl<DB> ToSql<VpcRouterKindEnum, DB> for VpcRouterKind
    where
    DB: Backend,
    {
    fn to_sql<W: std::io::Write>(
    &self,
    out: &mut serialize::Output<W, DB>,
    ) -> serialize::Result {
    match *self.state() {
    external::VpcRouterKind::System => out.write_all(b"system")?,
    external::VpcRouterKind::Custom => out.write_all(b"custom")?,
    }
    Ok(IsNull::No)
    }
    }
    impl<DB> FromSql<VpcRouterKindEnum, DB> for VpcRouterKind
    where
    DB: Backend + for<'a> BinaryRawValue<'a>,
    {
    fn from_sql(bytes: RawValue<DB>) -> deserialize::Result<Self> {
    match DB::as_bytes(bytes) {
    b"system" => {
    Ok(VpcRouterKind::new(external::VpcRouterKind::System))
    }
    b"custom" => {
    Ok(VpcRouterKind::new(external::VpcRouterKind::Custom))
    }
    _ => Err("Unrecognized enum variant for VpcRouterKind".into()),
    }
    }
    }

    As part of the firewall work I'm doing in a branch, I needed some more enums so I followed the pattern there and made a declarative macro to reduce the pile of boilerplate (9f667f7). Does this seem like a fine workaround for now?

  3. smklein commented on Nov 10, 2021

    @smklein
    Collaborator

    FYI @zephraph , regarding the macro for enum generation.

    Personally I think having helpers to reduce the boilerplate here would be great, as long as the interface for those helpers is clear.

  4. just-be-dev commented on Nov 10, 2021

    @just-be-dev
    Contributor

    Heh, that pattern is hot off the presses (and may need to be cleaned up a little). I do love the macro to dry it up though! Thanks @teisenbe.

  5. davepacheco commented on Nov 15, 2021

    @davepacheco
    CollaboratorAuthor

    FWIW in this issue I was talking about using CockroachDB enums, not Rust enums.

  6. changed the title [-]use CockroachDB enums in database schema[/-] [+]use CockroachDB enums in more places in database schema[/+] on Nov 8, 2022
  7. davepacheco commented on Nov 8, 2022

    @davepacheco
    CollaboratorAuthor

    Update: we've done this for most of the enums, but not the data-carrying ones (like DiskState).

  8. added
    obsolete
    on Oct 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    databaseobsolete

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions