| 1 |
|
| 2 |
|
| 3 |
use super::{now_ms, Pool}; |
| 4 |
use hub_api::Visibility; |
| 5 |
use sqlx::FromRow; |
| 6 |
|
| 7 |
#[derive(Clone, Debug, FromRow)] |
| 8 |
pub struct RepoRow { |
| 9 |
pub id: i64, |
| 10 |
pub owner: String, |
| 11 |
pub name: String, |
| 12 |
pub description: String, |
| 13 |
pub default_bookmark: String, |
| 14 |
pub visibility: String, |
| 15 |
pub created_ms: i64, |
| 16 |
pub updated_ms: i64, |
| 17 |
#[sqlx(default)] |
| 18 |
pub git_mirror: bool, |
| 19 |
#[sqlx(default)] |
| 20 |
pub git_mirror_state: String, |
| 21 |
} |
| 22 |
|
| 23 |
impl RepoRow { |
| 24 |
pub fn visibility(&self) -> Visibility { |
| 25 |
Visibility::parse(&self.visibility).unwrap_or(Visibility::Private) |
| 26 |
} |
| 27 |
|
| 28 |
pub fn slug(&self) -> String { |
| 29 |
format!("{}/{}", self.owner, self.name) |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
#[derive(Clone, Debug, FromRow)] |
| 34 |
pub struct PushRow { |
| 35 |
pub id: i64, |
| 36 |
pub at_ms: i64, |
| 37 |
pub reference: String, |
| 38 |
pub ok: bool, |
| 39 |
pub reason: String, |
| 40 |
pub old_state: String, |
| 41 |
pub new_state: String, |
| 42 |
pub heads: i64, |
| 43 |
pub changes: i64, |
| 44 |
pub pusher: String, |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
#[derive(Clone, Debug)] |
| 49 |
pub struct PushOutcome { |
| 50 |
pub reference: String, |
| 51 |
pub ok: bool, |
| 52 |
pub reason: String, |
| 53 |
pub old_state: String, |
| 54 |
pub new_state: String, |
| 55 |
pub heads: i64, |
| 56 |
pub changes: i64, |
| 57 |
} |
| 58 |
|
| 59 |
pub async fn list(pool: &Pool) -> sqlx::Result<Vec<RepoRow>> { |
| 60 |
sqlx::query_as("SELECT * FROM repositories ORDER BY updated_ms DESC, owner, name").fetch_all(pool).await |
| 61 |
} |
| 62 |
|
| 63 |
pub async fn get_by_id(pool: &Pool, id: i64) -> sqlx::Result<Option<RepoRow>> { |
| 64 |
sqlx::query_as("SELECT * FROM repositories WHERE id = ?").bind(id).fetch_optional(pool).await |
| 65 |
} |
| 66 |
|
| 67 |
pub async fn list_by_owner(pool: &Pool, owner: &str) -> sqlx::Result<Vec<RepoRow>> { |
| 68 |
sqlx::query_as("SELECT * FROM repositories WHERE owner = ? ORDER BY updated_ms DESC, name").bind(owner).fetch_all(pool).await |
| 69 |
} |
| 70 |
|
| 71 |
pub async fn get(pool: &Pool, owner: &str, name: &str) -> sqlx::Result<Option<RepoRow>> { |
| 72 |
sqlx::query_as("SELECT * FROM repositories WHERE owner = ? AND name = ?").bind(owner).bind(name).fetch_optional(pool).await |
| 73 |
} |
| 74 |
|
| 75 |
pub async fn create(pool: &Pool, owner: &str, name: &str, description: &str, visibility: Visibility) -> sqlx::Result<RepoRow> { |
| 76 |
let now = now_ms(); |
| 77 |
sqlx::query_as( |
| 78 |
"INSERT INTO repositories (owner, name, description, visibility, created_ms, updated_ms) VALUES (?, ?, ?, ?, ?, ?) RETURNING *", |
| 79 |
) |
| 80 |
.bind(owner) |
| 81 |
.bind(name) |
| 82 |
.bind(description) |
| 83 |
.bind(visibility.as_str()) |
| 84 |
.bind(now) |
| 85 |
.bind(now) |
| 86 |
.fetch_one(pool) |
| 87 |
.await |
| 88 |
} |
| 89 |
|
| 90 |
pub async fn delete(pool: &Pool, id: i64) -> sqlx::Result<()> { |
| 91 |
sqlx::query("DELETE FROM repositories WHERE id = ?").bind(id).execute(pool).await?; |
| 92 |
Ok(()) |
| 93 |
} |
| 94 |
|
| 95 |
pub async fn set_description(pool: &Pool, id: i64, description: &str) -> sqlx::Result<()> { |
| 96 |
sqlx::query("UPDATE repositories SET description = ? WHERE id = ?").bind(description).bind(id).execute(pool).await?; |
| 97 |
Ok(()) |
| 98 |
} |
| 99 |
|
| 100 |
pub async fn set_default_bookmark(pool: &Pool, id: i64, bookmark: &str) -> sqlx::Result<()> { |
| 101 |
sqlx::query("UPDATE repositories SET default_bookmark = ? WHERE id = ?").bind(bookmark).bind(id).execute(pool).await?; |
| 102 |
Ok(()) |
| 103 |
} |
| 104 |
|
| 105 |
pub async fn set_visibility(pool: &Pool, id: i64, visibility: Visibility) -> sqlx::Result<()> { |
| 106 |
sqlx::query("UPDATE repositories SET visibility = ? WHERE id = ?").bind(visibility.as_str()).bind(id).execute(pool).await?; |
| 107 |
Ok(()) |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
pub async fn record_push(pool: &Pool, id: i64, pusher: Option<(i64, &str)>, results: &[PushOutcome]) -> sqlx::Result<()> { |
| 112 |
let now = now_ms(); |
| 113 |
let mut tx = pool.begin().await?; |
| 114 |
for r in results { |
| 115 |
sqlx::query("INSERT INTO pushes (repo_id, at_ms, reference, ok, reason, old_state, new_state, heads, changes, account_id, pusher) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)") |
| 116 |
.bind(id) |
| 117 |
.bind(now) |
| 118 |
.bind(&r.reference) |
| 119 |
.bind(r.ok) |
| 120 |
.bind(&r.reason) |
| 121 |
.bind(&r.old_state) |
| 122 |
.bind(&r.new_state) |
| 123 |
.bind(r.heads) |
| 124 |
.bind(r.changes) |
| 125 |
.bind(pusher.map(|p| p.0).filter(|id| *id > 0)) |
| 126 |
.bind(pusher.map(|p| p.1).unwrap_or("")) |
| 127 |
.execute(&mut *tx) |
| 128 |
.await?; |
| 129 |
} |
| 130 |
if results.iter().any(|r| r.ok) { |
| 131 |
sqlx::query("UPDATE repositories SET updated_ms = ? WHERE id = ?").bind(now).bind(id).execute(&mut *tx).await?; |
| 132 |
} |
| 133 |
tx.commit().await |
| 134 |
} |
| 135 |
|
| 136 |
pub async fn pushes(pool: &Pool, id: i64, limit: i64) -> sqlx::Result<Vec<PushRow>> { |
| 137 |
sqlx::query_as("SELECT id, at_ms, reference, ok, reason, old_state, new_state, heads, changes, pusher FROM pushes WHERE repo_id = ? ORDER BY at_ms DESC, id DESC LIMIT ?") |
| 138 |
.bind(id) |
| 139 |
.bind(limit) |
| 140 |
.fetch_all(pool) |
| 141 |
.await |
| 142 |
} |
| 143 |
|
| 144 |
pub async fn set_git_mirror(pool: &Pool, id: i64, on: bool) -> sqlx::Result<()> { |
| 145 |
sqlx::query("UPDATE repositories SET git_mirror = ? WHERE id = ?").bind(on).bind(id).execute(pool).await?; |
| 146 |
Ok(()) |
| 147 |
} |
| 148 |
|
| 149 |
pub async fn set_git_mirror_state(pool: &Pool, id: i64, state: &str) -> sqlx::Result<()> { |
| 150 |
sqlx::query("UPDATE repositories SET git_mirror_state = ? WHERE id = ?").bind(state).bind(id).execute(pool).await?; |
| 151 |
Ok(()) |
| 152 |
} |