|
| 1 | +// Copyright (c) 2025 Gitpod GmbH. All rights reserved. |
| 2 | +// Licensed under the GNU Affero General Public License (AGPL). |
| 3 | +// See License.AGPL.txt in the project root for license information. |
| 4 | + |
| 5 | +use anyhow::Result; |
| 6 | +use chrono::{DateTime, Utc}; |
| 7 | +use serde::{Deserialize, Serialize}; |
| 8 | +use sqlx::{PgPool, Row}; |
| 9 | +use uuid::Uuid; |
| 10 | + |
| 11 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 12 | +pub struct Workspace { |
| 13 | + pub id: Uuid, |
| 14 | + pub user_id: Uuid, |
| 15 | + pub name: String, |
| 16 | + pub image: Option<String>, |
| 17 | + pub repository_url: Option<String>, |
| 18 | + pub status: WorkspaceStatus, |
| 19 | + pub url: Option<String>, |
| 20 | + pub created_at: DateTime<Utc>, |
| 21 | + pub updated_at: DateTime<Utc>, |
| 22 | +} |
| 23 | + |
| 24 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 25 | +pub enum WorkspaceStatus { |
| 26 | + Creating, |
| 27 | + Running, |
| 28 | + Stopping, |
| 29 | + Stopped, |
| 30 | + Failed, |
| 31 | +} |
| 32 | + |
| 33 | +pub struct Database { |
| 34 | + pool: PgPool, |
| 35 | +} |
| 36 | + |
| 37 | +impl Database { |
| 38 | + pub async fn new(database_url: &str) -> Result<Self> { |
| 39 | + let pool = PgPool::connect(database_url).await?; |
| 40 | + |
| 41 | + // Run migrations |
| 42 | + sqlx::migrate!("./migrations").run(&pool).await?; |
| 43 | + |
| 44 | + Ok(Self { pool }) |
| 45 | + } |
| 46 | + |
| 47 | + pub async fn create_workspace( |
| 48 | + &self, |
| 49 | + user_id: Uuid, |
| 50 | + name: String, |
| 51 | + image: Option<String>, |
| 52 | + repository_url: Option<String>, |
| 53 | + ) -> Result<Workspace> { |
| 54 | + let id = Uuid::new_v4(); |
| 55 | + let now = Utc::now(); |
| 56 | + |
| 57 | + let workspace = Workspace { |
| 58 | + id, |
| 59 | + user_id, |
| 60 | + name: name.clone(), |
| 61 | + image: image.clone(), |
| 62 | + repository_url: repository_url.clone(), |
| 63 | + status: WorkspaceStatus::Creating, |
| 64 | + url: None, |
| 65 | + created_at: now, |
| 66 | + updated_at: now, |
| 67 | + }; |
| 68 | + |
| 69 | + sqlx::query!( |
| 70 | + r#" |
| 71 | + INSERT INTO workspaces (id, user_id, name, image, repository_url, status, created_at, updated_at) |
| 72 | + VALUES ($1, $2, $3, $4, $5, $6, $7, $8) |
| 73 | + "#, |
| 74 | + id, |
| 75 | + user_id, |
| 76 | + name, |
| 77 | + image, |
| 78 | + repository_url, |
| 79 | + "Creating", |
| 80 | + now, |
| 81 | + now |
| 82 | + ) |
| 83 | + .execute(&self.pool) |
| 84 | + .await?; |
| 85 | + |
| 86 | + Ok(workspace) |
| 87 | + } |
| 88 | + |
| 89 | + pub async fn get_workspace(&self, id: Uuid) -> Result<Option<Workspace>> { |
| 90 | + let row = sqlx::query!( |
| 91 | + "SELECT id, user_id, name, image, repository_url, status, url, created_at, updated_at FROM workspaces WHERE id = $1", |
| 92 | + id |
| 93 | + ) |
| 94 | + .fetch_optional(&self.pool) |
| 95 | + .await?; |
| 96 | + |
| 97 | + if let Some(row) = row { |
| 98 | + Ok(Some(Workspace { |
| 99 | + id: row.id, |
| 100 | + user_id: row.user_id, |
| 101 | + name: row.name, |
| 102 | + image: row.image, |
| 103 | + repository_url: row.repository_url, |
| 104 | + status: parse_status(&row.status), |
| 105 | + url: row.url, |
| 106 | + created_at: row.created_at, |
| 107 | + updated_at: row.updated_at, |
| 108 | + })) |
| 109 | + } else { |
| 110 | + Ok(None) |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + pub async fn list_workspaces(&self) -> Result<Vec<Workspace>> { |
| 115 | + let rows = sqlx::query!( |
| 116 | + "SELECT id, user_id, name, image, repository_url, status, url, created_at, updated_at FROM workspaces ORDER BY created_at DESC" |
| 117 | + ) |
| 118 | + .fetch_all(&self.pool) |
| 119 | + .await?; |
| 120 | + |
| 121 | + let workspaces = rows |
| 122 | + .into_iter() |
| 123 | + .map(|row| Workspace { |
| 124 | + id: row.id, |
| 125 | + user_id: row.user_id, |
| 126 | + name: row.name, |
| 127 | + image: row.image, |
| 128 | + repository_url: row.repository_url, |
| 129 | + status: parse_status(&row.status), |
| 130 | + url: row.url, |
| 131 | + created_at: row.created_at, |
| 132 | + updated_at: row.updated_at, |
| 133 | + }) |
| 134 | + .collect(); |
| 135 | + |
| 136 | + Ok(workspaces) |
| 137 | + } |
| 138 | + |
| 139 | + pub async fn start_workspace(&self, id: Uuid) -> Result<Workspace> { |
| 140 | + let now = Utc::now(); |
| 141 | + let url = format!("https://{}.gitpod.example.com", id); |
| 142 | + |
| 143 | + sqlx::query!( |
| 144 | + "UPDATE workspaces SET status = $1, url = $2, updated_at = $3 WHERE id = $4", |
| 145 | + "Running", |
| 146 | + url, |
| 147 | + now, |
| 148 | + id |
| 149 | + ) |
| 150 | + .execute(&self.pool) |
| 151 | + .await?; |
| 152 | + |
| 153 | + self.get_workspace(id).await?.ok_or_else(|| anyhow::anyhow!("Workspace not found")) |
| 154 | + } |
| 155 | + |
| 156 | + pub async fn stop_workspace(&self, id: Uuid) -> Result<Workspace> { |
| 157 | + let now = Utc::now(); |
| 158 | + |
| 159 | + sqlx::query!( |
| 160 | + "UPDATE workspaces SET status = $1, url = NULL, updated_at = $2 WHERE id = $3", |
| 161 | + "Stopped", |
| 162 | + now, |
| 163 | + id |
| 164 | + ) |
| 165 | + .execute(&self.pool) |
| 166 | + .await?; |
| 167 | + |
| 168 | + self.get_workspace(id).await?.ok_or_else(|| anyhow::anyhow!("Workspace not found")) |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +fn parse_status(status: &str) -> WorkspaceStatus { |
| 173 | + match status { |
| 174 | + "Creating" => WorkspaceStatus::Creating, |
| 175 | + "Running" => WorkspaceStatus::Running, |
| 176 | + "Stopping" => WorkspaceStatus::Stopping, |
| 177 | + "Stopped" => WorkspaceStatus::Stopped, |
| 178 | + "Failed" => WorkspaceStatus::Failed, |
| 179 | + _ => WorkspaceStatus::Failed, |
| 180 | + } |
| 181 | +} |
0 commit comments