Skip to main content

microsandbox_db/entity/
sandbox_rootfs.rs

1//! Entity definition for the `sandbox_rootfs` table.
2//!
3//! Pins each sandbox to a manifest digest and runtime mode.
4
5use sea_orm::entity::prelude::*;
6
7//--------------------------------------------------------------------------------------------------
8// Types
9//--------------------------------------------------------------------------------------------------
10
11/// The sandbox rootfs entity model.
12#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
13#[sea_orm(table_name = "sandbox_rootfs")]
14pub struct Model {
15    #[sea_orm(primary_key)]
16    pub id: i32,
17    #[sea_orm(unique)]
18    pub sandbox_id: i32,
19    pub manifest_id: Option<i32>,
20    pub mode: String,
21    pub upper_fstype: Option<String>,
22    pub created_at: Option<DateTime>,
23}
24
25/// Rootfs source mode stored in the `sandbox_rootfs.mode` column.
26#[derive(Debug, Clone, PartialEq, Eq)]
27pub enum SandboxRootfsMode {
28    /// EROFS fsmerge + VMDK (always 2 block devices).
29    Erofs,
30    /// Host directory bind mount.
31    Bind,
32    /// Pre-existing disk image (qcow2/raw/vmdk).
33    DiskImage,
34}
35
36//--------------------------------------------------------------------------------------------------
37// Methods
38//--------------------------------------------------------------------------------------------------
39
40impl SandboxRootfsMode {
41    /// Database string representation.
42    pub fn as_str(&self) -> &'static str {
43        match self {
44            Self::Erofs => "erofs",
45            Self::Bind => "bind",
46            Self::DiskImage => "disk_image",
47        }
48    }
49
50    /// Parse from database string.
51    pub fn parse_str(s: &str) -> Option<Self> {
52        match s {
53            "erofs" => Some(Self::Erofs),
54            "bind" => Some(Self::Bind),
55            "disk_image" => Some(Self::DiskImage),
56            _ => None,
57        }
58    }
59}
60
61//--------------------------------------------------------------------------------------------------
62// Types: Relations
63//--------------------------------------------------------------------------------------------------
64
65/// Relations for the sandbox_rootfs entity.
66#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
67pub enum Relation {
68    /// Belongs to a sandbox (1:1).
69    #[sea_orm(
70        belongs_to = "super::sandbox::Entity",
71        from = "Column::SandboxId",
72        to = "super::sandbox::Column::Id",
73        on_delete = "Cascade"
74    )]
75    Sandbox,
76
77    /// References a manifest (nullable for bind/disk-image).
78    #[sea_orm(
79        belongs_to = "super::manifest::Entity",
80        from = "Column::ManifestId",
81        to = "super::manifest::Column::Id",
82        on_delete = "Restrict"
83    )]
84    Manifest,
85}
86
87impl Related<super::sandbox::Entity> for Entity {
88    fn to() -> RelationDef {
89        Relation::Sandbox.def()
90    }
91}
92
93impl Related<super::manifest::Entity> for Entity {
94    fn to() -> RelationDef {
95        Relation::Manifest.def()
96    }
97}
98
99//--------------------------------------------------------------------------------------------------
100// Trait Implementations
101//--------------------------------------------------------------------------------------------------
102
103impl ActiveModelBehavior for ActiveModel {}