Skip to main content

microsandbox_db/entity/
sandbox.rs

1//! Entity definition for the `sandboxes` table.
2
3use sea_orm::entity::prelude::*;
4
5//--------------------------------------------------------------------------------------------------
6// Types
7//--------------------------------------------------------------------------------------------------
8
9/// The status of a sandbox.
10#[derive(Debug, Clone, Copy, PartialEq, Eq, EnumIter, DeriveActiveEnum)]
11#[sea_orm(rs_type = "String", db_type = "Text")]
12pub enum SandboxStatus {
13    /// The sandbox has been created but not yet started.
14    ///
15    /// Cloud-only today: msb-cloud's create-without-start state. Local
16    /// sandboxes transition straight to `Running` after create.
17    #[sea_orm(string_value = "Created")]
18    Created,
19
20    /// A start request has been submitted but the sandbox is not yet running.
21    ///
22    /// Cloud-only today: covers the gap between accepting a start request
23    /// and the runtime reporting the VM as live.
24    #[sea_orm(string_value = "Starting")]
25    Starting,
26
27    /// The sandbox is running.
28    #[sea_orm(string_value = "Running")]
29    Running,
30
31    /// The sandbox is draining (shutting down gracefully).
32    #[sea_orm(string_value = "Draining")]
33    Draining,
34
35    /// The sandbox is paused.
36    #[sea_orm(string_value = "Paused")]
37    Paused,
38
39    /// The sandbox is stopped.
40    #[sea_orm(string_value = "Stopped")]
41    Stopped,
42
43    /// The sandbox crashed.
44    #[sea_orm(string_value = "Crashed")]
45    Crashed,
46}
47
48/// The sandbox entity model.
49#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
50#[sea_orm(table_name = "sandbox")]
51pub struct Model {
52    #[sea_orm(primary_key)]
53    pub id: i32,
54    #[sea_orm(unique)]
55    pub name: String,
56    pub config: String,
57    pub status: SandboxStatus,
58    /// Denormalized copy of `config.policy.ephemeral`.
59    ///
60    /// Indexed alongside `status` so host-runtime lifecycle maintenance can
61    /// find terminal ephemeral cleanup candidates without scanning and
62    /// parsing every stopped sandbox's serialized config.
63    pub ephemeral: bool,
64    pub created_at: Option<DateTime>,
65    pub updated_at: Option<DateTime>,
66}
67
68//--------------------------------------------------------------------------------------------------
69// Types: Relations
70//--------------------------------------------------------------------------------------------------
71
72/// Relations for the sandbox entity.
73#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
74pub enum Relation {
75    /// A sandbox has many runs.
76    #[sea_orm(has_many = "super::run::Entity")]
77    Run,
78}
79
80impl Related<super::run::Entity> for Entity {
81    fn to() -> RelationDef {
82        Relation::Run.def()
83    }
84}
85
86//--------------------------------------------------------------------------------------------------
87// Trait Implementations
88//--------------------------------------------------------------------------------------------------
89
90impl ActiveModelBehavior for ActiveModel {}