2025-08-04 13:59:42 -07:00
|
|
|
use derive_builder::Builder;
|
|
|
|
|
use sqlx::query_as;
|
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
2025-11-01 00:17:07 +00:00
|
|
|
use crate::{client::AppDbClient, cluster::Cluster, macros::with_id_query};
|
2025-08-04 13:59:42 -07:00
|
|
|
|
2025-09-14 16:19:44 -04:00
|
|
|
/// A workspace is 1:1 with a Postgres "database".
|
2025-08-04 13:59:42 -07:00
|
|
|
#[derive(Clone, Debug)]
|
2025-09-14 16:19:44 -04:00
|
|
|
pub struct Workspace {
|
|
|
|
|
/// Primary key (defaults to UUIDv7).
|
2025-08-04 13:59:42 -07:00
|
|
|
pub id: Uuid,
|
2025-09-14 16:19:44 -04:00
|
|
|
|
2025-11-01 00:17:07 +00:00
|
|
|
/// Cluster housing the backing database for this workspace.
|
|
|
|
|
pub cluster_id: Uuid,
|
|
|
|
|
|
|
|
|
|
/// Postgres database name.
|
|
|
|
|
pub db_name: String,
|
2025-09-14 16:19:44 -04:00
|
|
|
|
2025-11-01 00:17:07 +00:00
|
|
|
/// Human friendly name for the workspace.
|
|
|
|
|
pub display_name: String,
|
2025-09-14 16:19:44 -04:00
|
|
|
|
|
|
|
|
/// ID of the user account that created this workspace.
|
2025-08-04 13:59:42 -07:00
|
|
|
pub owner_id: Uuid,
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-14 16:19:44 -04:00
|
|
|
impl Workspace {
|
|
|
|
|
/// Build an insert statement to create a new workspace.
|
2025-11-01 00:17:07 +00:00
|
|
|
pub fn insert<'a>() -> InsertBuilder<'a> {
|
2025-10-22 00:43:53 -07:00
|
|
|
InsertBuilder::default()
|
2025-08-04 13:59:42 -07:00
|
|
|
}
|
|
|
|
|
|
2025-11-01 00:17:07 +00:00
|
|
|
pub async fn fetch_cluster(&self, app_db: &mut AppDbClient) -> sqlx::Result<Cluster> {
|
|
|
|
|
Cluster::with_id(self.cluster_id).fetch_one(app_db).await
|
2025-08-04 13:59:42 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-01 00:17:07 +00:00
|
|
|
with_id_query!(Workspace, sql = "select * from workspaces where id = $1");
|
2025-08-04 13:59:42 -07:00
|
|
|
|
2025-11-01 00:17:07 +00:00
|
|
|
#[derive(Builder, Clone, Copy, Debug)]
|
|
|
|
|
pub struct Insert<'a> {
|
|
|
|
|
cluster_id: Uuid,
|
|
|
|
|
db_name: &'a str,
|
2025-08-04 13:59:42 -07:00
|
|
|
owner_id: Uuid,
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-01 00:17:07 +00:00
|
|
|
impl<'a> Insert<'a> {
|
|
|
|
|
pub async fn insert(self, app_db: &mut AppDbClient) -> sqlx::Result<Workspace> {
|
2025-08-04 13:59:42 -07:00
|
|
|
query_as!(
|
2025-09-14 16:19:44 -04:00
|
|
|
Workspace,
|
2025-08-04 13:59:42 -07:00
|
|
|
"
|
2025-09-14 16:19:44 -04:00
|
|
|
insert into workspaces
|
2025-11-01 00:17:07 +00:00
|
|
|
(cluster_id, db_name, owner_id)
|
|
|
|
|
values ($1, $2, $3)
|
2025-08-04 13:59:42 -07:00
|
|
|
returning *
|
|
|
|
|
",
|
2025-11-01 00:17:07 +00:00
|
|
|
self.cluster_id,
|
|
|
|
|
self.db_name,
|
2025-08-04 13:59:42 -07:00
|
|
|
self.owner_id
|
|
|
|
|
)
|
|
|
|
|
.fetch_one(&mut *app_db.conn)
|
|
|
|
|
.await
|
|
|
|
|
}
|
|
|
|
|
}
|