/ backend / src / db / manifest_repository.rs
manifest_repository.rs
  1  use sqlx::Transaction;
  2  use uuid::Uuid;
  3  
  4  use crate::{models::manifest::Manifest, registry_error::RegistryResult};
  5  
  6  use super::DB;
  7  
  8  pub async fn insert(
  9      transaction: &mut Transaction<'_, DB>,
 10      repository: &str,
 11      blob_id: Uuid,
 12      tag: Option<&str>,
 13      digest: &str,
 14      content_type_top: &str,
 15      content_type_sub: &str,
 16  ) -> RegistryResult<Manifest> {
 17      Ok(sqlx::query_as!(
 18          Manifest,
 19          r#"
 20  INSERT INTO manifest(repository, tag, blob_id, digest, content_type_top, content_type_sub)
 21  VALUES              ($1,         $2,  $3,      $4,     $5,               $6)
 22  RETURNING id, repository, tag, blob_id, digest, content_type_top, content_type_sub, created_at
 23          "#,
 24          repository,
 25          tag,
 26          blob_id,
 27          digest,
 28          content_type_top,
 29          content_type_sub,
 30      )
 31      .fetch_one(&mut **transaction)
 32      .await?)
 33  }
 34  
 35  pub async fn find_by_repository_and_tag(
 36      transaction: &mut Transaction<'_, DB>,
 37      repository: &str,
 38      tag: Option<&str>,
 39  ) -> RegistryResult<Option<Manifest>> {
 40      Ok(sqlx::query_as!(
 41          Manifest,
 42          r#"
 43  SELECT id, repository, tag, blob_id, digest, content_type_top, content_type_sub, created_at
 44  FROM manifest
 45  WHERE repository = $1 AND tag = $2
 46      "#,
 47          repository,
 48          tag
 49      )
 50      .fetch_optional(&mut **transaction)
 51      .await?)
 52  }
 53  
 54  pub async fn find_first_by_repository_and_digest(
 55      transaction: &mut Transaction<'_, DB>,
 56      repository: &str,
 57      digest: &str,
 58  ) -> RegistryResult<Option<Manifest>> {
 59      Ok(sqlx::query_as!(
 60          Manifest,
 61          r#"
 62  SELECT m.id, m.repository, m.tag, m.blob_id, m.digest, m.content_type_top, m.content_type_sub, m.created_at
 63  FROM manifest m
 64  WHERE m.repository = $1 AND m.digest = $2
 65          "#,
 66          repository,
 67          digest
 68      )
 69      .fetch_optional(&mut **transaction)
 70      .await?)
 71  }
 72  
 73  pub async fn find_by_repository_and_digest(
 74      transaction: &mut Transaction<'_, DB>,
 75      repository: &str,
 76      digest: &str,
 77  ) -> RegistryResult<Vec<Manifest>> {
 78      Ok(sqlx::query_as!(
 79          Manifest,
 80          r#"
 81  SELECT m.id, m.repository, m.tag, m.blob_id, m.digest, m.content_type_top, m.content_type_sub, m.created_at
 82  FROM manifest m
 83  WHERE m.repository = $1 AND m.digest = $2
 84          "#,
 85          repository,
 86          digest
 87      )
 88      .fetch_all(&mut **transaction)
 89      .await?)
 90  }
 91  
 92  pub async fn find_all_by_repository(
 93      transaction: &mut Transaction<'_, DB>,
 94      repository: &str,
 95  ) -> RegistryResult<Vec<Manifest>> {
 96      Ok(sqlx::query_as!(
 97          Manifest,
 98          r#"
 99  SELECT id, repository, tag, blob_id, digest, content_type_top, content_type_sub, created_at
100  FROM manifest
101  WHERE repository = $1
102  ORDER BY tag ASC
103          "#,
104          repository
105      )
106      .fetch_all(&mut **transaction)
107      .await?)
108  }
109  
110  pub async fn find_all_by_repository_max(
111      transaction: &mut Transaction<'_, DB>,
112      repository: &str,
113      n: i64,
114  ) -> RegistryResult<Vec<Manifest>> {
115      Ok(sqlx::query_as!(
116          Manifest,
117          r#"
118  SELECT id, repository, tag, blob_id, digest, content_type_top, content_type_sub, created_at
119  FROM manifest
120  WHERE repository = $1
121  ORDER BY tag ASC
122  LIMIT $2
123          "#,
124          repository,
125          n
126      )
127      .fetch_all(&mut **transaction)
128      .await?)
129  }
130  
131  pub async fn find_all_by_repository_last(
132      transaction: &mut Transaction<'_, DB>,
133      repository: &str,
134      last: &str,
135  ) -> RegistryResult<Vec<Manifest>> {
136      Ok(sqlx::query_as!(
137          Manifest,
138          r#"
139  SELECT id, repository, tag, blob_id, digest, content_type_top, content_type_sub, created_at
140  FROM manifest
141  WHERE repository = $1 AND tag > $2
142  ORDER BY tag ASC
143          "#,
144          repository,
145          last,
146      )
147      .fetch_all(&mut **transaction)
148      .await?)
149  }
150  
151  pub async fn find_all_by_repository_last_max(
152      transaction: &mut Transaction<'_, DB>,
153      repository: &str,
154      last: &str,
155      n: i64,
156  ) -> RegistryResult<Vec<Manifest>> {
157      Ok(sqlx::query_as!(
158          Manifest,
159          r#"
160  SELECT id, repository, tag, blob_id, digest, content_type_top, content_type_sub, created_at
161  FROM manifest
162  WHERE repository = $1 AND tag > $2
163  ORDER BY tag ASC
164  LIMIT $3
165          "#,
166          repository,
167          last,
168          n
169      )
170      .fetch_all(&mut **transaction)
171      .await?)
172  }
173  
174  pub async fn find_all_by_blob_id_and_repository(
175      transaction: &mut Transaction<'_, DB>,
176      repository: &str,
177      blob_id: Uuid,
178  ) -> RegistryResult<Vec<Manifest>> {
179      Ok(sqlx::query_as!(
180          Manifest,
181          r#"
182  SELECT id, repository, tag, blob_id, digest, content_type_top, content_type_sub, created_at
183  FROM manifest
184  WHERE blob_id = $1 AND repository = $2
185          "#,
186          blob_id,
187          repository
188      )
189      .fetch_all(&mut **transaction)
190      .await?)
191  }
192  
193  pub async fn delete_manifest(
194      transaction: &mut Transaction<'_, DB>,
195      id: Uuid,
196  ) -> RegistryResult<()> {
197      sqlx::query_as!(
198          Manifest,
199          r#"
200  DELETE
201  FROM manifest
202  WHERE id = $1
203      "#,
204          id
205      )
206      .execute(&mut **transaction)
207      .await?;
208  
209      Ok(())
210  }
211  
212  pub async fn delete_tag(
213      transaction: &mut Transaction<'_, DB>,
214      name: &str,
215      tag: &str,
216  ) -> RegistryResult<()> {
217      sqlx::query_as!(
218          Manifest,
219          r#"
220  UPDATE manifest
221  SET tag = NULL
222  WHERE repository = $1 AND tag = $2
223          "#,
224          name,
225          tag
226      )
227      .execute(&mut **transaction)
228      .await?;
229  
230      Ok(())
231  }