| 1 | |
|
| 2 | |
|
| 3 | | use super::{now_ms, Pool}; |
| 4 | | use sqlx::FromRow; |
| 5 | |
|
| 6 | | #[derive(Clone, Debug, FromRow)] |
| 7 | | pub struct Project { |
| 8 | | pub id: i64, |
| 9 | | pub owner: String, |
| 10 | | pub number: i64, |
| 11 | | pub title: String, |
| 12 | | pub description: String, |
| 13 | | pub public: bool, |
| 14 | | pub closed: bool, |
| 15 | | pub created_ms: i64, |
| 16 | | pub updated_ms: i64, |
| 17 | | } |
| 18 | |
|
| 19 | | #[derive(Clone, Debug, FromRow)] |
| 20 | | pub struct Column { |
| 21 | | pub id: i64, |
| 22 | | pub project_id: i64, |
| 23 | | pub name: String, |
| 24 | | pub position: i64, |
| 25 | | |
| 26 | | pub archive_after_days: Option<i64>, |
| 27 | | |
| 28 | | |
| 29 | | pub takes_finished: bool, |
| 30 | | } |
| 31 | |
|
| 32 | | #[derive(Clone, Debug, FromRow)] |
| 33 | | pub struct Card { |
| 34 | | pub id: i64, |
| 35 | | pub project_id: i64, |
| 36 | | pub column_id: i64, |
| 37 | | pub position: i64, |
| 38 | | pub kind: String, |
| 39 | | pub repo_id: Option<i64>, |
| 40 | | pub number: Option<i64>, |
| 41 | | pub note: String, |
| 42 | | pub added_by: String, |
| 43 | | pub created_ms: i64, |
| 44 | | |
| 45 | | pub archived_ms: Option<i64>, |
| 46 | | |
| 47 | | pub moved_ms: i64, |
| 48 | | |
| 49 | | pub iteration_id: Option<i64>, |
| 50 | | } |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | | #[derive(Clone, Debug, FromRow)] |
| 55 | | pub struct Iteration { |
| 56 | | pub id: i64, |
| 57 | | pub project_id: i64, |
| 58 | | pub name: String, |
| 59 | | pub start_ms: i64, |
| 60 | | |
| 61 | | pub end_ms: i64, |
| 62 | | pub closed_ms: Option<i64>, |
| 63 | | } |
| 64 | |
|
| 65 | |
|
| 66 | | const DAY_MS: i64 = 86_400_000; |
| 67 | |
|
| 68 | | pub const DEFAULT_COLUMNS: [&str; 3] = ["Backlog", "Doing", "Done"]; |
| 69 | |
|
| 70 | 11× | pub async fn create(pool: &Pool, owner: &str, title: &str, description: &str, public: bool) -> sqlx::Result<Project> { |
| 71 | 11× | let mut tx = super::begin_write(pool).await?; |
| 72 | 11× | let number: i64 = |
| 73 | 11× | sqlx::query_scalar("SELECT COALESCE(MAX(number), 0) + 1 FROM projects WHERE owner = $1").bind(owner).fetch_one(&mut *tx).await?; |
| 74 | 11× | let now = now_ms(); |
| 75 | 11× | let p: Project = sqlx::query_as( |
| 76 | 11× | "INSERT INTO projects (owner, number, title, description, public, created_ms, updated_ms) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *", |
| 77 | 11× | ) |
| 78 | 11× | .bind(owner) |
| 79 | 11× | .bind(number) |
| 80 | 11× | .bind(title) |
| 81 | 11× | .bind(description) |
| 82 | 11× | .bind(public) |
| 83 | 11× | .bind(now) |
| 84 | 11× | .bind(now) |
| 85 | 11× | .fetch_one(&mut *tx) |
| 86 | 11× | .await?; |
| 87 | | |
| 88 | 11× | sqlx::query("DELETE FROM project_redirects WHERE owner = $1 AND number = $2").bind(owner).bind(number).execute(&mut *tx).await?; |
| 89 | 33× | for (i, name) in DEFAULT_COLUMNS.iter().enumerate() { |
| 90 | 33× | sqlx::query("INSERT INTO project_columns (project_id, name, position, takes_finished) VALUES ($1, $2, $3, $4)") |
| 91 | 33× | .bind(p.id) |
| 92 | 33× | .bind(name) |
| 93 | 33× | .bind(i as i64) |
| 94 | 33× | .bind(*name == "Done") |
| 95 | 33× | .execute(&mut *tx) |
| 96 | 33× | .await?; |
| 97 | | } |
| 98 | 11× | tx.commit().await?; |
| 99 | 11× | Ok(p) |
| 100 | 11× | } |
| 101 | |
|
| 102 | 76× | pub async fn of_owner(pool: &Pool, owner: &str) -> sqlx::Result<Vec<Project>> { |
| 103 | 76× | sqlx::query_as("SELECT * FROM projects WHERE owner = $1 ORDER BY closed, updated_ms DESC").bind(owner).fetch_all(pool).await |
| 104 | 76× | } |
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | 1× | pub async fn transfer(pool: &Pool, id: i64, to: &str) -> sqlx::Result<Project> { |
| 109 | 1× | let mut tx = super::begin_write(pool).await?; |
| 110 | 1× | let old: Project = sqlx::query_as("SELECT * FROM projects WHERE id = $1").bind(id).fetch_one(&mut *tx).await?; |
| 111 | 1× | let number: i64 = |
| 112 | 1× | sqlx::query_scalar("SELECT COALESCE(MAX(number), 0) + 1 FROM projects WHERE owner = $1").bind(to).fetch_one(&mut *tx).await?; |
| 113 | 1× | sqlx::query("INSERT INTO project_redirects (owner, number, project_id) VALUES ($1, $2, $3) ON CONFLICT (owner, number) DO UPDATE SET project_id = excluded.project_id") |
| 114 | 1× | .bind(&old.owner) |
| 115 | 1× | .bind(old.number) |
| 116 | 1× | .bind(id) |
| 117 | 1× | .execute(&mut *tx) |
| 118 | 1× | .await?; |
| 119 | 1× | sqlx::query("DELETE FROM project_redirects WHERE owner = $1 AND number = $2").bind(to).bind(number).execute(&mut *tx).await?; |
| 120 | 1× | let moved: Project = sqlx::query_as("UPDATE projects SET owner = $1, number = $2, updated_ms = $3 WHERE id = $4 RETURNING *") |
| 121 | 1× | .bind(to) |
| 122 | 1× | .bind(number) |
| 123 | 1× | .bind(now_ms()) |
| 124 | 1× | .bind(id) |
| 125 | 1× | .fetch_one(&mut *tx) |
| 126 | 1× | .await?; |
| 127 | 1× | tx.commit().await?; |
| 128 | 1× | Ok(moved) |
| 129 | 1× | } |
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | 2× | pub async fn redirected(pool: &Pool, owner: &str, number: i64) -> sqlx::Result<Option<Project>> { |
| 134 | 2× | sqlx::query_as("SELECT p.* FROM project_redirects r JOIN projects p ON p.id = r.project_id WHERE r.owner = $1 AND r.number = $2") |
| 135 | 2× | .bind(owner) |
| 136 | 2× | .bind(number) |
| 137 | 2× | .fetch_optional(pool) |
| 138 | 2× | .await |
| 139 | 2× | } |
| 140 | |
|
| 141 | 194× | pub async fn get(pool: &Pool, owner: &str, number: i64) -> sqlx::Result<Option<Project>> { |
| 142 | 194× | sqlx::query_as("SELECT * FROM projects WHERE owner = $1 AND number = $2").bind(owner).bind(number).fetch_optional(pool).await |
| 143 | 194× | } |
| 144 | |
|
| 145 | 6× | pub async fn set(pool: &Pool, id: i64, title: &str, description: &str, public: bool, closed: bool) -> sqlx::Result<()> { |
| 146 | 6× | sqlx::query("UPDATE projects SET title = $1, description = $2, public = $3, closed = $4, updated_ms = $5 WHERE id = $6") |
| 147 | 6× | .bind(title) |
| 148 | 6× | .bind(description) |
| 149 | 6× | .bind(public) |
| 150 | 6× | .bind(closed) |
| 151 | 6× | .bind(now_ms()) |
| 152 | 6× | .bind(id) |
| 153 | 6× | .execute(pool) |
| 154 | 6× | .await?; |
| 155 | 6× | Ok(()) |
| 156 | 6× | } |
| 157 | |
|
| 158 | 1× | pub async fn delete(pool: &Pool, id: i64) -> sqlx::Result<()> { |
| 159 | 1× | sqlx::query("DELETE FROM projects WHERE id = $1").bind(id).execute(pool).await?; |
| 160 | 1× | Ok(()) |
| 161 | 1× | } |
| 162 | |
|
| 163 | 39× | async fn touch(pool: &Pool, id: i64) -> sqlx::Result<()> { |
| 164 | 39× | sqlx::query("UPDATE projects SET updated_ms = $1 WHERE id = $2").bind(now_ms()).bind(id).execute(pool).await?; |
| 165 | 39× | Ok(()) |
| 166 | 39× | } |
| 167 | |
|
| 168 | 154× | pub async fn columns(pool: &Pool, project_id: i64) -> sqlx::Result<Vec<Column>> { |
| 169 | 154× | sqlx::query_as("SELECT * FROM project_columns WHERE project_id = $1 ORDER BY position, id").bind(project_id).fetch_all(pool).await |
| 170 | 154× | } |
| 171 | |
|
| 172 | 86× | pub async fn cards(pool: &Pool, project_id: i64) -> sqlx::Result<Vec<Card>> { |
| 173 | 86× | sqlx::query_as("SELECT * FROM project_cards WHERE project_id = $1 ORDER BY position, id").bind(project_id).fetch_all(pool).await |
| 174 | 86× | } |
| 175 | |
|
| 176 | 1× | pub async fn add_column(pool: &Pool, project_id: i64, name: &str) -> sqlx::Result<()> { |
| 177 | 1× | let pos: i64 = sqlx::query_scalar("SELECT COALESCE(MAX(position), -1) + 1 FROM project_columns WHERE project_id = $1") |
| 178 | 1× | .bind(project_id) |
| 179 | 1× | .fetch_one(pool) |
| 180 | 1× | .await?; |
| 181 | 1× | sqlx::query("INSERT INTO project_columns (project_id, name, position) VALUES ($1, $2, $3)") |
| 182 | 1× | .bind(project_id) |
| 183 | 1× | .bind(name) |
| 184 | 1× | .bind(pos) |
| 185 | 1× | .execute(pool) |
| 186 | 1× | .await?; |
| 187 | 1× | touch(pool, project_id).await |
| 188 | 1× | } |
| 189 | |
|
| 190 | 1× | pub async fn rename_column(pool: &Pool, project_id: i64, column_id: i64, name: &str) -> sqlx::Result<()> { |
| 191 | 1× | sqlx::query("UPDATE project_columns SET name = $1 WHERE id = $2 AND project_id = $3") |
| 192 | 1× | .bind(name) |
| 193 | 1× | .bind(column_id) |
| 194 | 1× | .bind(project_id) |
| 195 | 1× | .execute(pool) |
| 196 | 1× | .await?; |
| 197 | 1× | touch(pool, project_id).await |
| 198 | 1× | } |
| 199 | |
|
| 200 | |
|
| 201 | 0× | pub async fn shift_column(pool: &Pool, project_id: i64, column_id: i64, by: i64) -> sqlx::Result<()> { |
| 202 | 0× | let cols = columns(pool, project_id).await?; |
| 203 | 0× | let Some(i) = cols.iter().position(|c| c.id == column_id) else { return Ok(()) }; |
| 204 | 0× | let j = i as i64 + by; |
| 205 | 0× | if j < 0 || j as usize >= cols.len() { |
| 206 | 0× | return Ok(()); |
| 207 | 0× | } |
| 208 | 0× | let mut ids: Vec<i64> = cols.iter().map(|c| c.id).collect(); |
| 209 | 0× | ids.swap(i, j as usize); |
| 210 | 0× | for (pos, id) in ids.iter().enumerate() { |
| 211 | 0× | sqlx::query("UPDATE project_columns SET position = $1 WHERE id = $2").bind(pos as i64).bind(id).execute(pool).await?; |
| 212 | | } |
| 213 | 0× | touch(pool, project_id).await |
| 214 | 0× | } |
| 215 | |
|
| 216 | |
|
| 217 | 2× | pub async fn place_column(pool: &Pool, project_id: i64, column_id: i64, position: i64) -> sqlx::Result<()> { |
| 218 | 2× | let cols = columns(pool, project_id).await?; |
| 219 | 5× | let Some(i) = cols.iter().position(|c| c.id == column_id) else { return Ok(()) }; |
| 220 | 2× | let mut ids: Vec<i64> = cols.iter().map(|c| c.id).collect(); |
| 221 | 2× | let id = ids.remove(i); |
| 222 | 2× | ids.insert((position.max(0) as usize).min(ids.len()), id); |
| 223 | 8× | for (pos, id) in ids.iter().enumerate() { |
| 224 | 8× | sqlx::query("UPDATE project_columns SET position = $1 WHERE id = $2").bind(pos as i64).bind(id).execute(pool).await?; |
| 225 | | } |
| 226 | 2× | touch(pool, project_id).await |
| 227 | 2× | } |
| 228 | |
|
| 229 | |
|
| 230 | 4× | pub async fn remove_column(pool: &Pool, project_id: i64, column_id: i64) -> sqlx::Result<bool> { |
| 231 | 4× | let cols = columns(pool, project_id).await?; |
| 232 | 4× | let Some(to) = cols.iter().find(|c| c.id != column_id) else { return Ok(false) }; |
| 233 | 8× | if !cols.iter().any(|c| c.id == column_id) { |
| 234 | 0× | return Ok(false); |
| 235 | 3× | } |
| 236 | 3× | let mut tx = super::begin_write(pool).await?; |
| 237 | 3× | let base: i64 = sqlx::query_scalar("SELECT COALESCE(MAX(position), -1) + 1 FROM project_cards WHERE column_id = $1") |
| 238 | 3× | .bind(to.id) |
| 239 | 3× | .fetch_one(&mut *tx) |
| 240 | 3× | .await?; |
| 241 | 3× | sqlx::query("UPDATE project_cards SET column_id = $1, position = position + $2 WHERE column_id = $3") |
| 242 | 3× | .bind(to.id) |
| 243 | 3× | .bind(base) |
| 244 | 3× | .bind(column_id) |
| 245 | 3× | .execute(&mut *tx) |
| 246 | 3× | .await?; |
| 247 | 3× | sqlx::query("DELETE FROM project_columns WHERE id = $1").bind(column_id).execute(&mut *tx).await?; |
| 248 | 3× | tx.commit().await?; |
| 249 | 3× | touch(pool, project_id).await?; |
| 250 | 3× | Ok(true) |
| 251 | 4× | } |
| 252 | |
|
| 253 | | pub struct NewCard<'a> { |
| 254 | | pub column_id: i64, |
| 255 | | pub kind: &'a str, |
| 256 | | pub repo_id: Option<i64>, |
| 257 | | pub number: Option<i64>, |
| 258 | | pub note: &'a str, |
| 259 | | pub added_by: &'a str, |
| 260 | | } |
| 261 | |
|
| 262 | |
|
| 263 | |
|
| 264 | 19× | pub async fn add_card(pool: &Pool, project_id: i64, c: NewCard<'_>) -> sqlx::Result<bool> { |
| 265 | 19× | let pos: i64 = sqlx::query_scalar("SELECT COALESCE(MAX(position), -1) + 1 FROM project_cards WHERE column_id = $1") |
| 266 | 19× | .bind(c.column_id) |
| 267 | 19× | .fetch_one(pool) |
| 268 | 19× | .await?; |
| 269 | 19× | let r = sqlx::query( |
| 270 | 19× | "INSERT INTO project_cards (project_id, column_id, position, kind, repo_id, number, note, added_by, created_ms, moved_ms) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $9) ON CONFLICT DO NOTHING", |
| 271 | 19× | ) |
| 272 | 19× | .bind(project_id) |
| 273 | 19× | .bind(c.column_id) |
| 274 | 19× | .bind(pos) |
| 275 | 19× | .bind(c.kind) |
| 276 | 19× | .bind(c.repo_id) |
| 277 | 19× | .bind(c.number) |
| 278 | 19× | .bind(c.note) |
| 279 | 19× | .bind(c.added_by) |
| 280 | 19× | .bind(now_ms()) |
| 281 | 19× | .execute(pool) |
| 282 | 19× | .await?; |
| 283 | 19× | touch(pool, project_id).await?; |
| 284 | 19× | Ok(r.rows_affected() == 1) |
| 285 | 19× | } |
| 286 | |
|
| 287 | |
|
| 288 | 5× | pub async fn move_card(pool: &Pool, project_id: i64, card_id: i64, column_id: i64) -> sqlx::Result<()> { |
| 289 | 5× | let ok: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM project_columns WHERE id = $1 AND project_id = $2") |
| 290 | 5× | .bind(column_id) |
| 291 | 5× | .bind(project_id) |
| 292 | 5× | .fetch_one(pool) |
| 293 | 5× | .await?; |
| 294 | 5× | if ok == 0 { |
| 295 | 0× | return Ok(()); |
| 296 | 5× | } |
| 297 | 5× | let pos: i64 = sqlx::query_scalar("SELECT COALESCE(MAX(position), -1) + 1 FROM project_cards WHERE column_id = $1") |
| 298 | 5× | .bind(column_id) |
| 299 | 5× | .fetch_one(pool) |
| 300 | 5× | .await?; |
| 301 | 5× | sqlx::query("UPDATE project_cards SET moved_ms = CASE WHEN column_id = $1 THEN moved_ms ELSE $3 END, column_id = $1, position = $2 WHERE id = $4 AND project_id = $5") |
| 302 | 5× | .bind(column_id) |
| 303 | 5× | .bind(pos) |
| 304 | 5× | .bind(now_ms()) |
| 305 | 5× | .bind(card_id) |
| 306 | 5× | .bind(project_id) |
| 307 | 5× | .execute(pool) |
| 308 | 5× | .await?; |
| 309 | 5× | touch(pool, project_id).await |
| 310 | 5× | } |
| 311 | |
|
| 312 | |
|
| 313 | |
|
| 314 | 1× | pub async fn move_cards(pool: &Pool, project_id: i64, card_ids: &[i64], column_id: i64) -> sqlx::Result<()> { |
| 315 | 1× | let mut tx = pool.begin().await?; |
| 316 | 1× | let pos: i64 = sqlx::query_scalar("SELECT COALESCE(MAX(position), -1) + 1 FROM project_cards WHERE column_id = $1") |
| 317 | 1× | .bind(column_id) |
| 318 | 1× | .fetch_one(&mut *tx) |
| 319 | 1× | .await?; |
| 320 | 1× | let now = now_ms(); |
| 321 | 1× | for (i, id) in card_ids.iter().enumerate() { |
| 322 | 1× | sqlx::query("UPDATE project_cards SET column_id = $1, position = $2, moved_ms = $3 WHERE id = $4 AND project_id = $5") |
| 323 | 1× | .bind(column_id) |
| 324 | 1× | .bind(pos + i as i64) |
| 325 | 1× | .bind(now) |
| 326 | 1× | .bind(id) |
| 327 | 1× | .bind(project_id) |
| 328 | 1× | .execute(&mut *tx) |
| 329 | 1× | .await?; |
| 330 | | } |
| 331 | 1× | sqlx::query("UPDATE projects SET updated_ms = $1 WHERE id = $2").bind(now).bind(project_id).execute(&mut *tx).await?; |
| 332 | 1× | tx.commit().await |
| 333 | 1× | } |
| 334 | |
|
| 335 | |
|
| 336 | 3× | pub async fn archive_cards(pool: &Pool, project_id: i64, card_ids: &[i64]) -> sqlx::Result<u64> { |
| 337 | 3× | let mut tx = pool.begin().await?; |
| 338 | 3× | let now = now_ms(); |
| 339 | 3× | let mut n = 0; |
| 340 | 3× | for id in card_ids { |
| 341 | 3× | n += sqlx::query("UPDATE project_cards SET archived_ms = $1 WHERE id = $2 AND project_id = $3 AND archived_ms IS NULL") |
| 342 | 3× | .bind(now) |
| 343 | 3× | .bind(id) |
| 344 | 3× | .bind(project_id) |
| 345 | 3× | .execute(&mut *tx) |
| 346 | 3× | .await? |
| 347 | 3× | .rows_affected(); |
| 348 | | } |
| 349 | 3× | sqlx::query("UPDATE projects SET updated_ms = $1 WHERE id = $2").bind(now).bind(project_id).execute(&mut *tx).await?; |
| 350 | 3× | tx.commit().await?; |
| 351 | 3× | Ok(n) |
| 352 | 3× | } |
| 353 | |
|
| 354 | |
|
| 355 | |
|
| 356 | |
|
| 357 | 3× | pub async fn restore_card(pool: &Pool, project_id: i64, card_id: i64) -> sqlx::Result<bool> { |
| 358 | 3× | let mut tx = pool.begin().await?; |
| 359 | 3× | let pos: i64 = sqlx::query_scalar( |
| 360 | 3× | "SELECT COALESCE(MAX(position), -1) + 1 FROM project_cards WHERE column_id = (SELECT column_id FROM project_cards WHERE id = $1)", |
| 361 | 3× | ) |
| 362 | 3× | .bind(card_id) |
| 363 | 3× | .fetch_one(&mut *tx) |
| 364 | 3× | .await?; |
| 365 | 3× | let now = now_ms(); |
| 366 | | |
| 367 | 3× | let r = sqlx::query( |
| 368 | 3× | "UPDATE project_cards SET archived_ms = NULL, iteration_id = NULL, moved_ms = $1, position = $2 \ |
| 369 | 3× | WHERE id = $3 AND project_id = $4 AND archived_ms IS NOT NULL", |
| 370 | 3× | ) |
| 371 | 3× | .bind(now) |
| 372 | 3× | .bind(pos) |
| 373 | 3× | .bind(card_id) |
| 374 | 3× | .bind(project_id) |
| 375 | 3× | .execute(&mut *tx) |
| 376 | 3× | .await?; |
| 377 | 3× | sqlx::query("UPDATE projects SET updated_ms = $1 WHERE id = $2").bind(now).bind(project_id).execute(&mut *tx).await?; |
| 378 | 3× | tx.commit().await?; |
| 379 | 3× | Ok(r.rows_affected() == 1) |
| 380 | 3× | } |
| 381 | |
|
| 382 | |
|
| 383 | 8× | pub fn older_than(card: &Card, days: i64, now: i64) -> bool { |
| 384 | 8× | card.moved_ms < now - days * DAY_MS |
| 385 | 8× | } |
| 386 | |
|
| 387 | |
|
| 388 | 52× | pub async fn iterations(pool: &Pool, project_id: i64) -> sqlx::Result<Vec<Iteration>> { |
| 389 | 52× | sqlx::query_as("SELECT * FROM project_iterations WHERE project_id = $1 ORDER BY start_ms DESC, id DESC") |
| 390 | 52× | .bind(project_id) |
| 391 | 52× | .fetch_all(pool) |
| 392 | 52× | .await |
| 393 | 52× | } |
| 394 | |
|
| 395 | |
|
| 396 | 2× | pub async fn start_iteration(pool: &Pool, project_id: i64, name: &str, start_ms: i64, end_ms: i64) -> sqlx::Result<Option<Iteration>> { |
| 397 | 2× | let mut tx = super::begin_write(pool).await?; |
| 398 | 2× | let open: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM project_iterations WHERE project_id = $1 AND closed_ms IS NULL") |
| 399 | 2× | .bind(project_id) |
| 400 | 2× | .fetch_one(&mut *tx) |
| 401 | 2× | .await?; |
| 402 | 2× | if open > 0 { |
| 403 | 1× | return Ok(None); |
| 404 | 1× | } |
| 405 | 1× | let it: Iteration = |
| 406 | 1× | sqlx::query_as("INSERT INTO project_iterations (project_id, name, start_ms, end_ms) VALUES ($1, $2, $3, $4) RETURNING *") |
| 407 | 1× | .bind(project_id) |
| 408 | 1× | .bind(name) |
| 409 | 1× | .bind(start_ms) |
| 410 | 1× | .bind(end_ms) |
| 411 | 1× | .fetch_one(&mut *tx) |
| 412 | 1× | .await?; |
| 413 | 1× | sqlx::query("UPDATE projects SET updated_ms = $1 WHERE id = $2").bind(now_ms()).bind(project_id).execute(&mut *tx).await?; |
| 414 | 1× | tx.commit().await?; |
| 415 | 1× | Ok(Some(it)) |
| 416 | 2× | } |
| 417 | |
|
| 418 | |
|
| 419 | |
|
| 420 | 54× | pub fn done_column(columns: &[Column]) -> Option<i64> { |
| 421 | 54× | columns.iter().find(|c| c.takes_finished).or(columns.last()).map(|c| c.id) |
| 422 | 54× | } |
| 423 | |
|
| 424 | |
|
| 425 | |
|
| 426 | 2× | pub async fn close_iteration(pool: &Pool, project_id: i64, iteration_id: i64, done: Option<i64>) -> sqlx::Result<Option<u64>> { |
| 427 | 2× | let mut tx = super::begin_write(pool).await?; |
| 428 | 2× | let now = now_ms(); |
| 429 | 2× | let closed = sqlx::query("UPDATE project_iterations SET closed_ms = $1 WHERE id = $2 AND project_id = $3 AND closed_ms IS NULL") |
| 430 | 2× | .bind(now) |
| 431 | 2× | .bind(iteration_id) |
| 432 | 2× | .bind(project_id) |
| 433 | 2× | .execute(&mut *tx) |
| 434 | 2× | .await?; |
| 435 | 2× | if closed.rows_affected() == 0 { |
| 436 | 1× | return Ok(None); |
| 437 | 1× | } |
| 438 | 1× | let mut n = 0; |
| 439 | 1× | if let Some(done) = done { |
| 440 | 1× | n = sqlx::query( |
| 441 | 1× | "UPDATE project_cards SET archived_ms = $1, iteration_id = $2 WHERE project_id = $3 AND column_id = $4 AND archived_ms IS NULL", |
| 442 | 1× | ) |
| 443 | 1× | .bind(now) |
| 444 | 1× | .bind(iteration_id) |
| 445 | 1× | .bind(project_id) |
| 446 | 1× | .bind(done) |
| 447 | 1× | .execute(&mut *tx) |
| 448 | 1× | .await? |
| 449 | 1× | .rows_affected(); |
| 450 | 0× | } |
| 451 | 1× | sqlx::query("UPDATE projects SET updated_ms = $1 WHERE id = $2").bind(now).bind(project_id).execute(&mut *tx).await?; |
| 452 | 1× | tx.commit().await?; |
| 453 | 1× | Ok(Some(n)) |
| 454 | 2× | } |
| 455 | |
|
| 456 | |
|
| 457 | |
|
| 458 | |
|
| 459 | 11× | pub async fn done_times(pool: &Pool, it: &Iteration, done: Option<i64>) -> sqlx::Result<Vec<i64>> { |
| 460 | 11× | sqlx::query_scalar( |
| 461 | 11× | "SELECT moved_ms FROM project_cards WHERE project_id = $1 AND (iteration_id = $2 \ |
| 462 | 11× | OR ($3 AND iteration_id IS NULL AND column_id = $4 AND (archived_ms IS NULL OR archived_ms >= $5))) ORDER BY moved_ms", |
| 463 | 11× | ) |
| 464 | 11× | .bind(it.project_id) |
| 465 | 11× | .bind(it.id) |
| 466 | 11× | .bind(it.closed_ms.is_none()) |
| 467 | 11× | .bind(done.unwrap_or(-1)) |
| 468 | 11× | .bind(it.start_ms) |
| 469 | 11× | .fetch_all(pool) |
| 470 | 11× | .await |
| 471 | 11× | } |
| 472 | |
|
| 473 | |
|
| 474 | |
|
| 475 | |
|
| 476 | 14× | pub fn burn_up(times: &[i64], start_ms: i64, end_ms: i64, now: i64) -> Vec<usize> { |
| 477 | 14× | let days = ((end_ms - start_ms + DAY_MS - 1) / DAY_MS).max(1); |
| 478 | 14× | let upto = ((now.min(end_ms - 1) - start_ms) / DAY_MS + 1).clamp(0, days); |
| 479 | 112× | (0..upto).map(|d| times.iter().filter(|&&t| t < start_ms + (d + 1) * DAY_MS).count()).collect() |
| 480 | 14× | } |
| 481 | |
|
| 482 | |
|
| 483 | 11× | pub fn iteration_days(it: &Iteration) -> i64 { |
| 484 | 11× | ((it.end_ms - it.start_ms + DAY_MS - 1) / DAY_MS).max(1) |
| 485 | 11× | } |
| 486 | |
|
| 487 | |
|
| 488 | |
|
| 489 | |
|
| 490 | 3× | pub async fn set_archive_after(pool: &Pool, project_id: i64, column_id: i64, days: Option<i64>) -> sqlx::Result<()> { |
| 491 | 3× | sqlx::query("UPDATE project_columns SET archive_after_days = $1 WHERE id = $2 AND project_id = $3") |
| 492 | 3× | .bind(days) |
| 493 | 3× | .bind(column_id) |
| 494 | 3× | .bind(project_id) |
| 495 | 3× | .execute(pool) |
| 496 | 3× | .await?; |
| 497 | 3× | touch(pool, project_id).await |
| 498 | 3× | } |
| 499 | |
|
| 500 | |
|
| 501 | |
|
| 502 | | pub const FINISHED_KEPT_DAYS: i64 = 14; |
| 503 | |
|
| 504 | |
|
| 505 | | pub const NEVER: i64 = -1; |
| 506 | |
|
| 507 | |
|
| 508 | |
|
| 509 | |
|
| 510 | |
|
| 511 | |
|
| 512 | 208× | pub async fn archive_due(pool: &Pool, now: i64) -> sqlx::Result<u64> { |
| 513 | 208× | let set = sqlx::query( |
| 514 | 208× | "UPDATE project_cards SET archived_ms = $1 WHERE archived_ms IS NULL AND EXISTS \ |
| 515 | 208× | (SELECT 1 FROM project_columns c WHERE c.id = project_cards.column_id AND c.archive_after_days >= 0 \ |
| 516 | 208× | AND project_cards.moved_ms < $1 - c.archive_after_days * $2)", |
| 517 | 208× | ) |
| 518 | 208× | .bind(now) |
| 519 | 208× | .bind(DAY_MS) |
| 520 | 208× | .execute(pool) |
| 521 | 208× | .await?; |
| 522 | 207× | let finished = sqlx::query( |
| 523 | 207× | "UPDATE project_cards SET archived_ms = $1 WHERE archived_ms IS NULL AND moved_ms < $2 \ |
| 524 | 207× | AND EXISTS (SELECT 1 FROM project_columns c WHERE c.id = project_cards.column_id AND c.archive_after_days IS NULL) \ |
| 525 | 207× | AND ((kind = 'issue' AND EXISTS (SELECT 1 FROM issues i WHERE i.repo_id = project_cards.repo_id \ |
| 526 | 207× | AND i.number = project_cards.number AND i.state = 'closed')) \ |
| 527 | 207× | OR (kind = 'proposal' AND EXISTS (SELECT 1 FROM proposals p WHERE p.repo_id = project_cards.repo_id \ |
| 528 | 207× | AND p.number = project_cards.number AND p.state IN ('closed', 'landed'))))", |
| 529 | 207× | ) |
| 530 | 207× | .bind(now) |
| 531 | 207× | .bind(now - FINISHED_KEPT_DAYS * DAY_MS) |
| 532 | 207× | .execute(pool) |
| 533 | 207× | .await?; |
| 534 | 206× | Ok(set.rows_affected() + finished.rows_affected()) |
| 535 | 206× | } |
| 536 | |
|
| 537 | |
|
| 538 | 0× | pub async fn shift_card(pool: &Pool, project_id: i64, card_id: i64, by: i64) -> sqlx::Result<()> { |
| 539 | 0× | let Some(card): Option<Card> = sqlx::query_as("SELECT * FROM project_cards WHERE id = $1 AND project_id = $2") |
| 540 | 0× | .bind(card_id) |
| 541 | 0× | .bind(project_id) |
| 542 | 0× | .fetch_optional(pool) |
| 543 | 0× | .await? |
| 544 | | else { |
| 545 | 0× | return Ok(()); |
| 546 | | }; |
| 547 | 0× | let col: Vec<Card> = sqlx::query_as("SELECT * FROM project_cards WHERE column_id = $1 ORDER BY position, id") |
| 548 | 0× | .bind(card.column_id) |
| 549 | 0× | .fetch_all(pool) |
| 550 | 0× | .await?; |
| 551 | 0× | let Some(i) = col.iter().position(|c| c.id == card_id) else { return Ok(()) }; |
| 552 | 0× | let j = i as i64 + by; |
| 553 | 0× | if j < 0 || j as usize >= col.len() { |
| 554 | 0× | return Ok(()); |
| 555 | 0× | } |
| 556 | 0× | let mut ids: Vec<i64> = col.iter().map(|c| c.id).collect(); |
| 557 | 0× | ids.swap(i, j as usize); |
| 558 | 0× | for (pos, id) in ids.iter().enumerate() { |
| 559 | 0× | sqlx::query("UPDATE project_cards SET position = $1 WHERE id = $2").bind(pos as i64).bind(id).execute(pool).await?; |
| 560 | | } |
| 561 | 0× | touch(pool, project_id).await |
| 562 | 0× | } |
| 563 | |
|
| 564 | 3× | pub async fn remove_card(pool: &Pool, project_id: i64, card_id: i64) -> sqlx::Result<()> { |
| 565 | 3× | sqlx::query("DELETE FROM project_cards WHERE id = $1 AND project_id = $2").bind(card_id).bind(project_id).execute(pool).await?; |
| 566 | 3× | touch(pool, project_id).await |
| 567 | 3× | } |
| 568 | |
|
| 569 | |
|
| 570 | |
|
| 571 | 2× | pub async fn set_takes_finished(pool: &Pool, project_id: i64, column_id: Option<i64>) -> sqlx::Result<()> { |
| 572 | 2× | let mut tx = super::begin_write(pool).await?; |
| 573 | 2× | sqlx::query("UPDATE project_columns SET takes_finished = FALSE WHERE project_id = $1").bind(project_id).execute(&mut *tx).await?; |
| 574 | 2× | if let Some(c) = column_id { |
| 575 | 1× | sqlx::query("UPDATE project_columns SET takes_finished = TRUE WHERE id = $1 AND project_id = $2") |
| 576 | 1× | .bind(c) |
| 577 | 1× | .bind(project_id) |
| 578 | 1× | .execute(&mut *tx) |
| 579 | 1× | .await?; |
| 580 | 1× | } |
| 581 | 2× | tx.commit().await?; |
| 582 | 2× | touch(pool, project_id).await |
| 583 | 2× | } |
| 584 | |
|
| 585 | |
|
| 586 | |
|
| 587 | |
|
| 588 | 37× | pub async fn finished(pool: &Pool, repo_id: i64, number: i64) -> sqlx::Result<()> { |
| 589 | 37× | let cards: Vec<(i64, i64, i64)> = sqlx::query_as( |
| 590 | 37× | "SELECT c.id, c.project_id, c.column_id FROM project_cards c |
| 591 | 37× | WHERE c.repo_id = $1 AND c.number = $2 AND c.kind <> 'note' AND c.archived_ms IS NULL", |
| 592 | 37× | ) |
| 593 | 37× | .bind(repo_id) |
| 594 | 37× | .bind(number) |
| 595 | 37× | .fetch_all(pool) |
| 596 | 37× | .await?; |
| 597 | 37× | for (card, project, column) in cards { |
| 598 | 3× | let to: Option<i64> = |
| 599 | 3× | sqlx::query_scalar("SELECT id FROM project_columns WHERE project_id = $1 AND takes_finished ORDER BY position LIMIT 1") |
| 600 | 3× | .bind(project) |
| 601 | 3× | .fetch_optional(pool) |
| 602 | 3× | .await?; |
| 603 | 3× | if let Some(to) = to.filter(|to| *to != column) { |
| 604 | 2× | move_card(pool, project, card, to).await?; |
| 605 | 1× | } |
| 606 | | } |
| 607 | 37× | Ok(()) |
| 608 | 37× | } |
| 609 | |
|
| 610 | 106× | pub async fn placements(pool: &Pool, repo_id: i64, number: i64) -> sqlx::Result<Vec<(Project, String)>> { |
| 611 | 106× | let rows: Vec<(i64, String, Option<i64>)> = sqlx::query_as( |
| 612 | 106× | "SELECT c.project_id, col.name, c.archived_ms FROM project_cards c JOIN project_columns col ON col.id = c.column_id WHERE c.repo_id = $1 AND c.number = $2 AND c.kind <> 'note'", |
| 613 | 106× | ) |
| 614 | 106× | .bind(repo_id) |
| 615 | 106× | .bind(number) |
| 616 | 106× | .fetch_all(pool) |
| 617 | 106× | .await?; |
| 618 | 106× | let mut out = Vec::new(); |
| 619 | 106× | for (pid, col, archived) in rows { |
| 620 | 5× | let col = if archived.is_some() { format!("{col}, archived") } else { col }; |
| 621 | 5× | if let Some(p) = sqlx::query_as::<_, Project>("SELECT * FROM projects WHERE id = $1").bind(pid).fetch_optional(pool).await? { |
| 622 | 5× | out.push((p, col)); |
| 623 | 5× | } |
| 624 | | } |
| 625 | 106× | Ok(out) |
| 626 | 106× | } |