| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | | use super::{now_ms, Pool}; |
| 5 | | use sqlx::FromRow; |
| 6 | |
|
| 7 | |
|
| 8 | | #[derive(Clone, Debug, Default, PartialEq, Eq, FromRow)] |
| 9 | | pub struct Policy { |
| 10 | | pub scope: String, |
| 11 | | pub key: String, |
| 12 | | pub updates: Option<bool>, |
| 13 | | pub channel: Option<String>, |
| 14 | | pub window_from: Option<i64>, |
| 15 | | pub window_to: Option<i64>, |
| 16 | | pub dry_run: Option<bool>, |
| 17 | | } |
| 18 | |
|
| 19 | | impl Policy { |
| 20 | | |
| 21 | 7× | pub fn is_empty(&self) -> bool { |
| 22 | 7× | self.updates.is_none() && self.channel.is_none() && self.window_from.is_none() && self.dry_run.is_none() |
| 23 | 7× | } |
| 24 | | } |
| 25 | |
|
| 26 | | #[derive(Clone, Debug, PartialEq, Eq, FromRow)] |
| 27 | | pub struct Group { |
| 28 | | pub id: i64, |
| 29 | | |
| 30 | | pub owner: String, |
| 31 | | pub name: String, |
| 32 | | |
| 33 | | |
| 34 | | pub percent: Option<i64>, |
| 35 | | pub created_ms: i64, |
| 36 | | } |
| 37 | |
|
| 38 | 60× | pub async fn policies(pool: &Pool) -> sqlx::Result<Vec<Policy>> { |
| 39 | 60× | sqlx::query_as("SELECT * FROM runner_update_policies").fetch_all(pool).await |
| 40 | 60× | } |
| 41 | |
|
| 42 | 0× | pub async fn policy(pool: &Pool, scope: &str, key: &str) -> sqlx::Result<Policy> { |
| 43 | 0× | let p: Option<Policy> = sqlx::query_as("SELECT * FROM runner_update_policies WHERE scope = $1 AND key = $2") |
| 44 | 0× | .bind(scope) |
| 45 | 0× | .bind(key) |
| 46 | 0× | .fetch_optional(pool) |
| 47 | 0× | .await?; |
| 48 | 0× | Ok(p.unwrap_or(Policy { scope: scope.into(), key: key.into(), ..Policy::default() })) |
| 49 | 0× | } |
| 50 | |
|
| 51 | |
|
| 52 | 7× | pub async fn set_policy(pool: &Pool, p: &Policy) -> sqlx::Result<()> { |
| 53 | 7× | sqlx::query("DELETE FROM runner_update_policies WHERE scope = $1 AND key = $2").bind(&p.scope).bind(&p.key).execute(pool).await?; |
| 54 | 7× | if p.is_empty() { |
| 55 | 0× | return Ok(()); |
| 56 | 7× | } |
| 57 | 7× | sqlx::query( |
| 58 | 7× | "INSERT INTO runner_update_policies (scope, key, updates, channel, window_from, window_to, dry_run) |
| 59 | 7× | VALUES ($1, $2, $3, $4, $5, $6, $7)", |
| 60 | 7× | ) |
| 61 | 7× | .bind(&p.scope) |
| 62 | 7× | .bind(&p.key) |
| 63 | 7× | .bind(p.updates) |
| 64 | 7× | .bind(&p.channel) |
| 65 | 7× | .bind(p.window_from) |
| 66 | 7× | .bind(p.window_to) |
| 67 | 7× | .bind(p.dry_run) |
| 68 | 7× | .execute(pool) |
| 69 | 7× | .await?; |
| 70 | 7× | Ok(()) |
| 71 | 7× | } |
| 72 | |
|
| 73 | 64× | pub async fn groups(pool: &Pool) -> sqlx::Result<Vec<Group>> { |
| 74 | 64× | sqlx::query_as("SELECT * FROM runner_groups ORDER BY owner, name").fetch_all(pool).await |
| 75 | 64× | } |
| 76 | |
|
| 77 | 7× | pub async fn group(pool: &Pool, id: i64) -> sqlx::Result<Option<Group>> { |
| 78 | 7× | sqlx::query_as("SELECT * FROM runner_groups WHERE id = $1").bind(id).fetch_optional(pool).await |
| 79 | 7× | } |
| 80 | |
|
| 81 | 3× | pub async fn create_group(pool: &Pool, owner: &str, name: &str, percent: Option<i64>) -> sqlx::Result<Group> { |
| 82 | 3× | sqlx::query_as("INSERT INTO runner_groups (owner, name, percent, created_ms) VALUES ($1, $2, $3, $4) RETURNING *") |
| 83 | 3× | .bind(owner) |
| 84 | 3× | .bind(name) |
| 85 | 3× | .bind(percent) |
| 86 | 3× | .bind(now_ms()) |
| 87 | 3× | .fetch_one(pool) |
| 88 | 3× | .await |
| 89 | 3× | } |
| 90 | |
|
| 91 | 1× | pub async fn set_percent(pool: &Pool, id: i64, percent: Option<i64>) -> sqlx::Result<()> { |
| 92 | 1× | sqlx::query("UPDATE runner_groups SET percent = $1 WHERE id = $2").bind(percent).bind(id).execute(pool).await?; |
| 93 | 1× | Ok(()) |
| 94 | 1× | } |
| 95 | |
|
| 96 | |
|
| 97 | 1× | pub async fn delete_group(pool: &Pool, id: i64) -> sqlx::Result<()> { |
| 98 | 1× | sqlx::query("DELETE FROM runner_group_members WHERE group_id = $1").bind(id).execute(pool).await?; |
| 99 | 1× | sqlx::query("DELETE FROM runner_update_policies WHERE scope = 'group' AND key = $1").bind(id.to_string()).execute(pool).await?; |
| 100 | 1× | sqlx::query("DELETE FROM runner_groups WHERE id = $1").bind(id).execute(pool).await?; |
| 101 | 1× | Ok(()) |
| 102 | 1× | } |
| 103 | |
|
| 104 | |
|
| 105 | 60× | pub async fn members(pool: &Pool) -> sqlx::Result<Vec<(i64, i64)>> { |
| 106 | 60× | sqlx::query_as("SELECT group_id, runner_id FROM runner_group_members").fetch_all(pool).await |
| 107 | 60× | } |
| 108 | |
|
| 109 | |
|
| 110 | 3× | pub async fn set_members(pool: &Pool, group: i64, runners: &[i64]) -> sqlx::Result<()> { |
| 111 | 3× | sqlx::query("DELETE FROM runner_group_members WHERE group_id = $1").bind(group).execute(pool).await?; |
| 112 | 3× | for r in runners { |
| 113 | 2× | sqlx::query("INSERT INTO runner_group_members (group_id, runner_id) VALUES ($1, $2)").bind(group).bind(r).execute(pool).await?; |
| 114 | | } |
| 115 | 3× | Ok(()) |
| 116 | 3× | } |
| 117 | |
|
| 118 | |
|
| 119 | 2× | pub async fn report(pool: &Pool, runner: i64, note: &str) -> sqlx::Result<()> { |
| 120 | 2× | sqlx::query("UPDATE flow_runners SET update_note = $1, update_ms = $2 WHERE id = $3") |
| 121 | 2× | .bind(note) |
| 122 | 2× | .bind(now_ms()) |
| 123 | 2× | .bind(runner) |
| 124 | 2× | .execute(pool) |
| 125 | 2× | .await?; |
| 126 | 2× | Ok(()) |
| 127 | 2× | } |
| 128 | |
|
| 129 | |
|
| 130 | | #[derive(Clone, Debug, PartialEq, Eq, FromRow)] |
| 131 | | pub struct Hold { |
| 132 | | pub version: String, |
| 133 | | |
| 134 | | pub runner: String, |
| 135 | | pub why: String, |
| 136 | | pub at_ms: i64, |
| 137 | | } |
| 138 | |
|
| 139 | |
|
| 140 | 1× | pub async fn hold(pool: &Pool, version: &str, runner: &str, why: &str) -> sqlx::Result<()> { |
| 141 | 1× | sqlx::query("INSERT INTO runner_update_holds (version, runner, why, at_ms) VALUES ($1, $2, $3, $4) ON CONFLICT (version) DO NOTHING") |
| 142 | 1× | .bind(version) |
| 143 | 1× | .bind(runner) |
| 144 | 1× | .bind(why) |
| 145 | 1× | .bind(now_ms()) |
| 146 | 1× | .execute(pool) |
| 147 | 1× | .await?; |
| 148 | 1× | Ok(()) |
| 149 | 1× | } |
| 150 | |
|
| 151 | 17× | pub async fn holds(pool: &Pool) -> sqlx::Result<Vec<Hold>> { |
| 152 | 17× | sqlx::query_as("SELECT * FROM runner_update_holds ORDER BY at_ms DESC").fetch_all(pool).await |
| 153 | 17× | } |
| 154 | |
|
| 155 | 17× | pub async fn held(pool: &Pool, version: &str) -> sqlx::Result<Option<Hold>> { |
| 156 | 17× | sqlx::query_as("SELECT * FROM runner_update_holds WHERE version = $1").bind(version).fetch_optional(pool).await |
| 157 | 17× | } |
| 158 | |
|
| 159 | |
|
| 160 | 2× | pub async fn lift(pool: &Pool, version: &str) -> sqlx::Result<bool> { |
| 161 | 2× | Ok(sqlx::query("DELETE FROM runner_update_holds WHERE version = $1").bind(version).execute(pool).await?.rows_affected() > 0) |
| 162 | 2× | } |