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 /// Desired sandbox configuration used for the next start.
57 pub config: String,
58 /// Configuration actually used by the currently running VM, when active.
59 pub active_config: Option<String>,
60 pub status: SandboxStatus,
61 /// Denormalized copy of `config.policy.ephemeral`.
62 ///
63 /// Indexed alongside `status` so host-runtime lifecycle maintenance can
64 /// find terminal ephemeral cleanup candidates without scanning and
65 /// parsing every stopped sandbox's serialized config.
66 pub ephemeral: bool,
67 pub created_at: Option<DateTime>,
68 pub updated_at: Option<DateTime>,
69}
70
71//--------------------------------------------------------------------------------------------------
72// Types: Relations
73//--------------------------------------------------------------------------------------------------
74
75/// Relations for the sandbox entity.
76#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
77pub enum Relation {
78 /// A sandbox has many runs.
79 #[sea_orm(has_many = "super::run::Entity")]
80 Run,
81}
82
83impl Related<super::run::Entity> for Entity {
84 fn to() -> RelationDef {
85 Relation::Run.def()
86 }
87}
88
89//--------------------------------------------------------------------------------------------------
90// Trait Implementations
91//--------------------------------------------------------------------------------------------------
92
93impl ActiveModelBehavior for ActiveModel {}