steid

@jamesgill /

steid/src/domain/instance_settings.rs
Rust2.5 KBCode·Blame·History·Raw
1use super::OrgId;
2
3/// Settings that belong to the instance rather than to anything in it.
4///
5/// Exactly one of these exists, which is why it has no id: an instance is one tenant
6/// ([0010](../../plans/decisions/0010-single-tenant-vcs-not-portfolio.md)), so a table
7/// keyed by anything would be inviting a second row that nothing could choose between.
8/// The single row is enforced in the migration with a `check (id = 1)`.
9///
10/// It holds one field today. That is deliberate — the alternative was a `home_org_id`
11/// column bolted onto `orgs`, where it would have to be null on every row but one and
12/// would make "which organisation is home" a scan rather than a read.
13#[derive(Debug, Clone, PartialEq, Eq, Default)]
14pub struct InstanceSettings {
15 /// The organisation the root URL lands on.
16 ///
17 /// `None` on an instance nobody has chosen for — the root then falls back to the
18 /// sole owner's handle, which is how every instance behaved before this existed.
19 /// See [`home_handle`](crate::application::home_handle).
20 pub home: Option<OrgId>,
21}
22
23impl InstanceSettings {
24 /// The settings of an instance nobody has configured: no home.
25 #[must_use]
26 pub fn new() -> Self {
27 Self::default()
28 }
29
30 /// Reassembles the row from storage. Nothing to validate — an `OrgId` is a
31 /// foreign key the database already enforces.
32 #[must_use]
33 pub fn from_trusted(home: Option<OrgId>) -> Self {
34 Self { home }
35 }
36
37 /// Points the root at an organisation.
38 pub fn set_home(&mut self, org_id: OrgId) {
39 self.home = Some(org_id);
40 }
41
42 /// Whether the root has been pointed anywhere.
43 #[must_use]
44 pub fn has_home(&self) -> bool {
45 self.home.is_some()
46 }
47}
48
49#[cfg(test)]
50mod tests {
51 use super::*;
52
53 #[test]
54 fn a_fresh_instance_has_no_home() {
55 let settings = InstanceSettings::new();
56
57 assert_eq!(settings.home, None);
58 assert!(!settings.has_home());
59 }
60
61 #[test]
62 fn setting_home_points_at_an_organisation() {
63 let org = OrgId::generate();
64 let mut settings = InstanceSettings::new();
65
66 settings.set_home(org.clone());
67
68 assert_eq!(settings.home, Some(org));
69 assert!(settings.has_home());
70 }
71
72 #[test]
73 fn setting_home_again_replaces_it() {
74 let first = OrgId::generate();
75 let second = OrgId::generate();
76 let mut settings = InstanceSettings::new();
77
78 settings.set_home(first);
79 settings.set_home(second.clone());
80
81 assert_eq!(settings.home, Some(second));
82 }
83}