1use std::path::Path;
2
3use chrono::{DateTime, Utc};
4use crate_meta::CrateMeta;
5use kellnr_common::crate_data::CrateData;
6use kellnr_common::crate_overview::CrateOverview;
7use kellnr_common::cratesio_prefetch_msg::CratesioPrefetchMsg;
8use kellnr_common::index_metadata::IndexMetadata;
9use kellnr_common::normalized_name::NormalizedName;
10use kellnr_common::original_name::OriginalName;
11use kellnr_common::prefetch::Prefetch;
12use kellnr_common::publish_metadata::PublishMetadata;
13use kellnr_common::version::Version;
14use kellnr_common::webhook::{Webhook, WebhookEvent, WebhookQueue};
15use sea_orm::prelude::async_trait::async_trait;
16
17use crate::error::DbError;
18use crate::{AuthToken, CrateSummary, DocQueueEntry, Group, User, crate_meta};
19
20pub type DbResult<T> = Result<T, DbError>;
21#[derive(Debug, PartialEq, Eq)]
22pub enum PrefetchState {
23 UpToDate,
24 NeedsUpdate(Prefetch),
25 NotFound,
26}
27
28#[async_trait]
29pub trait DbProvider: Send + Sync {
30 async fn get_last_updated_crate(&self) -> DbResult<Option<(OriginalName, Version)>>;
31 async fn authenticate_user(&self, name: &str, pwd: &str) -> DbResult<User>;
32 async fn increase_download_counter(
33 &self,
34 crate_name: &NormalizedName,
35 crate_version: &Version,
36 ) -> DbResult<()>;
37 async fn increase_cached_download_counter(
38 &self,
39 crate_name: &NormalizedName,
40 crate_version: &Version,
41 ) -> DbResult<()>;
42 async fn validate_session(&self, session_token: &str) -> DbResult<(String, bool)>;
43 async fn add_session_token(&self, name: &str, session_token: &str) -> DbResult<()>;
44 async fn add_crate_user(&self, crate_name: &NormalizedName, user: &str) -> DbResult<()>;
45 async fn add_owner(&self, crate_name: &NormalizedName, owner: &str) -> DbResult<()>;
46 async fn is_download_restricted(&self, crate_name: &NormalizedName) -> DbResult<bool>;
47 async fn change_download_restricted(
48 &self,
49 crate_name: &NormalizedName,
50 restricted: bool,
51 ) -> DbResult<()>;
52 async fn is_crate_user(&self, crate_name: &NormalizedName, user: &str) -> DbResult<bool>;
53 async fn is_owner(&self, crate_name: &NormalizedName, user: &str) -> DbResult<bool>;
54 async fn get_crate_id(&self, crate_name: &NormalizedName) -> DbResult<Option<i64>>;
55 async fn get_crate_owners(&self, crate_name: &NormalizedName) -> DbResult<Vec<User>>;
56 async fn get_crate_users(&self, crate_name: &NormalizedName) -> DbResult<Vec<User>>;
57 async fn get_crate_versions(&self, crate_name: &NormalizedName) -> DbResult<Vec<Version>>;
58 async fn delete_session_token(&self, session_token: &str) -> DbResult<()>;
59 async fn delete_user(&self, user_name: &str) -> DbResult<()>;
60 async fn change_pwd(&self, user_name: &str, new_pwd: &str) -> DbResult<()>;
61 async fn change_read_only_state(&self, user_name: &str, state: bool) -> DbResult<()>;
62 async fn change_admin_state(&self, user_name: &str, state: bool) -> DbResult<()>;
63 async fn crate_version_exists(&self, crate_id: i64, version: &str) -> DbResult<bool>;
64 async fn get_max_version_from_id(&self, crate_id: i64) -> DbResult<Version>;
65 async fn get_max_version_from_name(&self, crate_name: &NormalizedName) -> DbResult<Version>;
66 async fn update_max_version(&self, crate_id: i64, version: &Version) -> DbResult<()>;
67 async fn add_auth_token(&self, name: &str, token: &str, user: &str) -> DbResult<()>;
68 async fn get_user_from_token(&self, token: &str) -> DbResult<User>;
69 async fn get_user(&self, name: &str) -> DbResult<User>;
70 async fn get_auth_tokens(&self, user_name: &str) -> DbResult<Vec<AuthToken>>;
71 async fn delete_auth_token(&self, id: i32) -> DbResult<()>;
72 async fn delete_owner(&self, crate_name: &str, owner: &str) -> DbResult<()>;
73 async fn delete_crate_user(&self, crate_name: &str, user: &str) -> DbResult<()>;
74 async fn add_user(
75 &self,
76 name: &str,
77 pwd: &str,
78 salt: &str,
79 is_admin: bool,
80 is_read_only: bool,
81 ) -> DbResult<()>;
82 async fn get_users(&self) -> DbResult<Vec<User>>;
83 async fn add_group(&self, name: &str) -> DbResult<()>;
84 async fn get_group(&self, name: &str) -> DbResult<Group>;
85 async fn get_groups(&self) -> DbResult<Vec<Group>>;
86 async fn delete_group(&self, name: &str) -> DbResult<()>;
87 async fn add_group_user(&self, group_name: &str, user: &str) -> DbResult<()>;
88 async fn delete_group_user(&self, group_name: &str, user: &str) -> DbResult<()>;
89 async fn get_group_users(&self, group_name: &str) -> DbResult<Vec<User>>;
90 async fn is_group_user(&self, group_name: &str, group: &str) -> DbResult<bool>;
91 async fn add_crate_group(&self, crate_name: &NormalizedName, group: &str) -> DbResult<()>;
92 async fn delete_crate_group(&self, crate_name: &NormalizedName, group: &str) -> DbResult<()>;
93 async fn get_crate_groups(&self, crate_name: &NormalizedName) -> DbResult<Vec<Group>>;
94 async fn is_crate_group(&self, crate_name: &NormalizedName, group: &str) -> DbResult<bool>;
95 async fn is_crate_group_user(&self, crate_name: &NormalizedName, user: &str) -> DbResult<bool>;
96 async fn get_total_unique_crates(&self) -> DbResult<u32>;
97 async fn get_total_crate_versions(&self) -> DbResult<u32>;
98 async fn get_total_downloads(&self) -> DbResult<u64>;
99 async fn get_top_crates_downloads(&self, top: u32) -> DbResult<Vec<(String, u64)>>;
100 async fn get_total_unique_cached_crates(&self) -> DbResult<u64>;
101 async fn get_total_cached_crate_versions(&self) -> DbResult<u64>;
102 async fn get_total_cached_downloads(&self) -> DbResult<u64>;
103 async fn get_crate_summaries(&self) -> DbResult<Vec<CrateSummary>>;
104 async fn add_doc_queue(
105 &self,
106 krate: &NormalizedName,
107 version: &Version,
108 path: &Path,
109 ) -> DbResult<()>;
110 async fn delete_doc_queue(&self, id: i64) -> DbResult<()>;
111 async fn get_doc_queue(&self) -> DbResult<Vec<DocQueueEntry>>;
112 async fn delete_crate(&self, krate: &NormalizedName, version: &Version) -> DbResult<()>;
113 async fn get_crate_meta_list(&self, crate_name: &NormalizedName) -> DbResult<Vec<CrateMeta>>;
114 async fn update_last_updated(&self, id: i64, last_updated: &DateTime<Utc>) -> DbResult<()>;
115 async fn search_in_crate_name(
116 &self,
117 contains: &str,
118 cache: bool,
119 ) -> DbResult<Vec<CrateOverview>>;
120 async fn get_crate_overview_list(
121 &self,
122 limit: u64,
123 offset: u64,
124 cache: bool,
125 ) -> DbResult<Vec<CrateOverview>>;
126 async fn get_crate_data(&self, crate_name: &NormalizedName) -> DbResult<CrateData>;
127 async fn add_empty_crate(&self, name: &str, created: &DateTime<Utc>) -> DbResult<i64>;
128 async fn add_crate(
129 &self,
130 pub_metadata: &PublishMetadata,
131 cksum: &str,
132 created: &DateTime<Utc>,
133 owner: &str,
134 ) -> DbResult<i64>;
135 async fn update_docs_link(
136 &self,
137 crate_name: &NormalizedName,
138 version: &Version,
139 docs_link: &str,
140 ) -> DbResult<()>;
141 async fn add_crate_metadata(
142 &self,
143 pub_metadata: &PublishMetadata,
144 created: &str,
145 crate_id: i64,
146 ) -> DbResult<()>;
147 async fn get_prefetch_data(&self, crate_name: &str) -> DbResult<Prefetch>;
148 async fn is_cratesio_cache_up_to_date(
149 &self,
150 crate_name: &NormalizedName,
151 if_none_match: Option<String>,
152 if_modified_since: Option<String>,
153 ) -> DbResult<PrefetchState>;
154 async fn add_cratesio_prefetch_data(
155 &self,
156 crate_name: &OriginalName,
157 etag: &str,
158 last_modified: &str,
159 description: Option<String>,
160 indices: &[IndexMetadata],
161 ) -> DbResult<Prefetch>;
162 async fn get_cratesio_index_update_list(&self) -> DbResult<Vec<CratesioPrefetchMsg>>;
163 async fn unyank_crate(&self, crate_name: &NormalizedName, version: &Version) -> DbResult<()>;
164 async fn yank_crate(&self, crate_name: &NormalizedName, version: &Version) -> DbResult<()>;
165 async fn register_webhook(&self, webhook: Webhook) -> DbResult<String>;
166 async fn delete_webhook(&self, id: &str) -> DbResult<()>;
167 async fn get_webhook(&self, id: &str) -> DbResult<Webhook>;
168 async fn get_all_webhooks(&self) -> DbResult<Vec<Webhook>>;
169 async fn add_webhook_queue(
173 &self,
174 event: WebhookEvent,
175 payload: serde_json::Value,
176 ) -> DbResult<()>;
177 async fn get_pending_webhook_queue_entries(
179 &self,
180 timestamp: DateTime<Utc>,
181 ) -> DbResult<Vec<WebhookQueue>>;
182 async fn update_webhook_queue(
183 &self,
184 id: &str,
185 last_attempt: DateTime<Utc>,
186 next_attempt: DateTime<Utc>,
187 ) -> DbResult<()>;
188 async fn delete_webhook_queue(&self, id: &str) -> DbResult<()>;
189}
190
191pub mod mock {
192 use chrono::DateTime;
193 use mockall::predicate::*;
194 use mockall::*;
195
196 use super::*;
197
198 mock! {
199 pub Db {}
200
201 #[async_trait]
202 impl DbProvider for Db {
203
204 async fn get_last_updated_crate(&self) -> DbResult<Option<(OriginalName, Version)>> {
205 unimplemented!()
206 }
207
208 async fn authenticate_user(&self, _name: &str, _pwd: &str) -> DbResult<User> {
209 unimplemented!()
210 }
211
212 async fn increase_download_counter(
213 &self,
214 _crate_name: &NormalizedName,
215 _crate_version: &Version,
216 ) -> DbResult<()> {
217 unimplemented!()
218 }
219
220 async fn increase_cached_download_counter(
221 &self,
222 _crate_name: &NormalizedName,
223 _crate_version: &Version,
224 ) -> DbResult<()> {
225 unimplemented!()
226 }
227
228 async fn validate_session(&self, _session_token: &str) -> DbResult<(String, bool)> {
229 unimplemented!()
230 }
231
232 async fn add_session_token(&self, _name: &str, _session_token: &str) -> DbResult<()> {
233 unimplemented!()
234 }
235
236 async fn add_crate_user(&self, crate_name: &NormalizedName, user: &str) -> DbResult<()> {
237 unimplemented!()
238 }
239
240 async fn add_owner(&self, _crate_name: &NormalizedName, _owner: &str) -> DbResult<()> {
241 unimplemented!()
242 }
243
244 async fn is_download_restricted(&self, crate_name: &NormalizedName) -> DbResult<bool> {
245 unimplemented!()
246 }
247
248 async fn change_download_restricted(
249 &self,
250 crate_name: &NormalizedName,
251 restricted: bool,
252 ) -> DbResult<()> {
253 unimplemented!()
254 }
255
256 async fn is_crate_user(&self, _crate_name: &NormalizedName, _user: &str) -> DbResult<bool> {
257 unimplemented!()
258 }
259
260 async fn is_owner(&self, _crate_name: &NormalizedName, _user: &str) -> DbResult<bool> {
261 unimplemented!()
262 }
263
264 async fn get_crate_id(&self, _crate_name: &NormalizedName) -> DbResult<Option<i64>> {
265 unimplemented!()
266 }
267
268 async fn get_crate_owners(&self, _crate_name: &NormalizedName) -> DbResult<Vec<User>> {
269 unimplemented!()
270 }
271
272 async fn get_crate_users(&self, _crate_name: &NormalizedName) -> DbResult<Vec<User>> {
273 unimplemented!()
274 }
275
276 async fn get_crate_versions(&self, _crate_name: &NormalizedName) -> DbResult<Vec<Version>> {
277 unimplemented!()
278 }
279
280 async fn delete_session_token(&self, _session_token: &str) -> DbResult<()> {
281 unimplemented!()
282 }
283
284 async fn delete_user(&self, _user_name: &str) -> DbResult<()> {
285 unimplemented!()
286 }
287
288 async fn change_pwd(&self, _user_name: &str, _new_pwd: &str) -> DbResult<()> {
289 unimplemented!()
290 }
291
292 async fn change_read_only_state(&self, _user_name: &str, _state: bool) -> DbResult<()> {
293 unimplemented!()
294 }
295
296 async fn change_admin_state(&self, _user_name: &str, _state: bool) -> DbResult<()> {
297 unimplemented!()
298 }
299
300 async fn crate_version_exists(&self, _crate_id: i64, _version: &str) -> DbResult<bool> {
301 unimplemented!()
302 }
303
304 async fn get_max_version_from_id(&self, crate_id: i64) -> DbResult<Version> {
305 unimplemented!()
306 }
307
308 async fn get_max_version_from_name(&self, crate_name: &NormalizedName) -> DbResult<Version> {
309 unimplemented!()
310 }
311
312 async fn update_max_version(&self, crate_id: i64, version: &Version) -> DbResult<()> {
313 unimplemented!()
314 }
315
316 async fn add_auth_token(&self, _name: &str, _token: &str, _user: &str) -> DbResult<()> {
317 unimplemented!()
318 }
319
320 async fn get_user_from_token(&self, _token: &str) -> DbResult<User> {
321 unimplemented!()
322 }
323
324 async fn get_user(&self, _name: &str) -> DbResult<User> {
325 unimplemented!()
326 }
327
328 async fn get_auth_tokens(&self, _user_name: &str) -> DbResult<Vec<AuthToken>> {
329 unimplemented!()
330 }
331
332 async fn delete_auth_token(&self, _id: i32) -> DbResult<()> {
333 unimplemented!()
334 }
335
336 async fn delete_owner(&self, _crate_name: &str, _owner: &str) -> DbResult<()> {
337 unimplemented!()
338 }
339
340 async fn delete_crate_user(&self, crate_name: &str, user: &str) -> DbResult<()>{
341 unimplemented!()
342 }
343
344 async fn add_user(&self, _name: &str, _pwd: &str, _salt: &str, _is_admin: bool, _is_read_only: bool) -> DbResult<()> {
345 unimplemented!()
346 }
347
348 async fn get_users(&self) -> DbResult<Vec<User>> {
349 unimplemented!()
350 }
351
352 async fn get_total_unique_crates(&self) -> DbResult<u32> {
353 unimplemented!()
354 }
355
356 async fn get_total_crate_versions(&self) -> DbResult<u32> {
357 unimplemented!()
358 }
359
360 async fn get_top_crates_downloads(&self, _top: u32) -> DbResult<Vec<(String, u64)>> {
361 unimplemented!()
362 }
363
364 async fn get_total_unique_cached_crates(&self) -> DbResult<u64> {
365 unimplemented!()
366 }
367
368 async fn get_total_cached_crate_versions(&self) -> DbResult<u64> {
369 unimplemented!()
370 }
371
372 async fn get_total_cached_downloads(&self) -> DbResult<u64> {
373 unimplemented!()
374 }
375
376 async fn get_crate_summaries(&self) -> DbResult<Vec<CrateSummary >> {
377 unimplemented!()
378 }
379
380 async fn add_doc_queue(&self, krate: &NormalizedName, version: &Version, path: &Path) -> DbResult<()>{
381 unimplemented!()
382 }
383
384 async fn delete_doc_queue(&self, id: i64) -> DbResult<()>{
385 unimplemented!()
386 }
387
388 async fn get_doc_queue(&self) -> DbResult<Vec<DocQueueEntry>> {
389 unimplemented!()
390 }
391
392 async fn delete_crate(&self, krate: &NormalizedName, version: &Version) -> DbResult<()> {
393 unimplemented!()
394 }
395
396 async fn get_total_downloads(&self) -> DbResult<u64>{
397 unimplemented!()
398 }
399
400 async fn get_crate_meta_list(&self, crate_name: &NormalizedName) -> DbResult<Vec<CrateMeta>>{
401 unimplemented!()
402 }
403
404 async fn update_last_updated(&self, id: i64, last_updated: &DateTime<Utc>) -> DbResult<()>{
405 unimplemented!()
406 }
407
408 async fn search_in_crate_name(&self, contains: &str, cache: bool) -> DbResult<Vec<CrateOverview>> {
409 unimplemented!()
410 }
411
412 async fn get_crate_overview_list(&self, limit: u64, offset: u64, cache: bool) -> DbResult<Vec<CrateOverview >> {
413 unimplemented!()
414 }
415
416 async fn get_crate_data(&self, crate_name: &NormalizedName) -> DbResult<CrateData> {
417 unimplemented!()
418 }
419
420 async fn add_empty_crate(&self, name: &str, created: &DateTime<Utc>) -> DbResult<i64> {
421 unimplemented!()
422 }
423
424 async fn add_crate(&self, pub_metadata: &PublishMetadata, sha256: &str, created: &DateTime<Utc>, owner: &str) -> DbResult<i64> {
425 unimplemented!()
426 }
427
428 async fn update_docs_link(&self, crate_name: &NormalizedName, version: &Version, docs_link: &str) -> DbResult<()> {
429 unimplemented!()
430 }
431
432 async fn add_crate_metadata(&self, pub_metadata: &PublishMetadata, created: &str, crate_id: i64,) -> DbResult<()> {
433 unimplemented!()
434 }
435
436 async fn get_prefetch_data(&self, crate_name: &str) -> DbResult<Prefetch> {
437 unimplemented!()
438 }
439
440 async fn is_cratesio_cache_up_to_date(&self, crate_name: &NormalizedName, etag: Option<String>, last_modified: Option<String>) -> DbResult<PrefetchState> {
441 unimplemented!()
442 }
443
444 async fn add_cratesio_prefetch_data(
445 &self,
446 crate_name: &OriginalName,
447 etag: &str,
448 last_modified: &str,
449 description: Option<String>,
450 indices: &[IndexMetadata],
451 ) -> DbResult<Prefetch> {
452 unimplemented!()
453 }
454
455 async fn get_cratesio_index_update_list(&self) -> DbResult<Vec<CratesioPrefetchMsg>> {
456 unimplemented!()
457 }
458
459 async fn unyank_crate(&self, crate_name: &NormalizedName, version: &Version) -> DbResult<()> {
460 unimplemented!()
461 }
462
463 async fn yank_crate(&self, crate_name: &NormalizedName, version: &Version) -> DbResult<()> {
464 unimplemented!()
465 }
466
467 async fn add_group(&self, name: &str) -> DbResult<()> {
468 unimplemented!()
469 }
470 async fn get_group(&self, name: &str) -> DbResult<Group>{
471 unimplemented!()
472 }
473 async fn get_groups(&self) -> DbResult<Vec<Group>>{
474 unimplemented!()
475 }
476 async fn delete_group(&self, name: &str) -> DbResult<()>{
477 unimplemented!()
478 }
479 async fn add_group_user(&self, group_name: &str, user: &str) -> DbResult<()>{
480 unimplemented!()
481 }
482 async fn delete_group_user(&self, group_name: &str, user: &str) -> DbResult<()>{
483 unimplemented!()
484 }
485 async fn get_group_users(&self, group_name: &str) -> DbResult<Vec<User>> {
486 unimplemented!()
487 }
488 async fn is_group_user(&self, group_name: &str, user: &str) -> DbResult<bool> {
489 unimplemented!()
490 }
491
492 async fn add_crate_group(&self, crate_name: &NormalizedName, group: &str) -> DbResult<()>{
493 unimplemented!()
494 }
495 async fn delete_crate_group(&self, crate_name: &NormalizedName, group: &str) -> DbResult<()>{
496 unimplemented!()
497 }
498 async fn get_crate_groups(&self, crate_name: &NormalizedName) -> DbResult<Vec<Group>>{
499 unimplemented!()
500 }
501 async fn is_crate_group(&self, crate_name: &NormalizedName, group: &str) -> DbResult<bool>{
502 unimplemented!()
503 }
504
505 async fn is_crate_group_user(&self, crate_name: &NormalizedName, user: &str) -> DbResult<bool>{
506 unimplemented!()
507 }
508
509 async fn register_webhook(
510 &self,
511 webhook: Webhook
512 ) -> DbResult<String> {
513 unimplemented!()
514 }
515 async fn delete_webhook(&self, id: &str) -> DbResult<()> {
516 unimplemented!()
517 }
518 async fn get_webhook(&self, id: &str) -> DbResult<Webhook> {
519 unimplemented!()
520 }
521 async fn get_all_webhooks(&self) -> DbResult<Vec<Webhook>> {
522 unimplemented!()
523 }
524 async fn add_webhook_queue(&self, event: WebhookEvent, payload: serde_json::Value) -> DbResult<()> {
525 unimplemented!()
526 }
527 async fn get_pending_webhook_queue_entries(&self, timestamp: DateTime<Utc>) -> DbResult<Vec<WebhookQueue>> {
528 unimplemented!()
529 }
530 async fn update_webhook_queue(&self, id: &str, last_attempt: DateTime<Utc>, next_attempt: DateTime<Utc>) -> DbResult<()> {
531 unimplemented!()
532 }
533 async fn delete_webhook_queue(&self, id: &str) -> DbResult<()> {
534 unimplemented!()
535 }
536 }
537 }
538}